Skip to content

Wrong solution to Exercise 1.3.5.2.3 #115

@Neev-Kataria

Description

@Neev-Kataria

Exercise 1.3.5.2.3

Using ms.erase(v) to delete just one copy of value v from a multiset ms that may contain 0, 1, or more copies of v. (note the 0)

Given solution

ms.erase(ms.find(v));

Issue

std::multiset::find returns end() when ms has 0 copies of v.
std::multiset::erase exhibits undefined behaviour when the pos passed to it is end().

Correct solution

auto temp = ms.find(v);
if (temp != ms.end()) ms.erase(temp);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions