Solved

Deleting an entry from a multimap

Posted on 2000-04-11
3
433 Views
Last Modified: 2010-05-18
Using Borland BCB4.0

I need two things:
1) Reason why the following program fails to find the entries made to the multimap "ITEM#1"
2) A way to delete a single entry (not a whole key) from the multimap "ITEM#2"

#include <condefs.h>
#include <conio>
#include <iostream>
#include <map>
#pragma hdrstop

class A
{
    public:
        A(int v = 0){val = v;}
        int getVal(){return val;}
    private:
        int val;
};

int main()
{
    A a1(1), a2(2), a3(3);
    std::multimap<const char*, A*> myMap;

    myMap.insert(std::make_pair("x", &a1) );
    myMap.insert(std::make_pair("x", &a2) );
    myMap.insert(std::make_pair("x", &a3) );

    typedef std::multimap<const char*, A*>::const_iterator mapIterator;
    std::pair<mapIterator,mapIterator> keyMatches;

    // Search for matches to key "x"; should find a1, a2 and a3
    keyMatches = myMap.equal_range("x");

    if ( keyMatches.first == myMap.end() )
        std::cout << "no matches found" << '\n';  // ITEM#1
    else
        for (mapIterator mi = keyMatches.first; mi != keyMatches.second; ++mi)
            std::cout << ( (mi->second)->getVal() ) << '\n';
            // Expected output:
            // 1
            // 2
            // 3


    // Erase map entry &a2 here (preserving entries &a1 and &a3
    // ?
    // ITEM#2
    // ?


    // Search for matches to key "x"; should find only a1 and a3

    keyMatches = myMap.equal_range("x");

    if ( keyMatches.first == myMap.end() )
        std::cout << "no matches found" << '\n';
    else
        for (mapIterator mi = keyMatches.first; mi != keyMatches.second; ++mi)
            std::cout << ( (mi->second)->getVal() ) << '\n';
            // Expected output:
            // 1
            // 3

    getch();
    return 0;
}
0
Comment
Question by:shadow66
3 Comments
 
LVL 9

Accepted Solution

by:
jasonclarke earned 50 total points
ID: 2707264
The reason why the find will fail is
because you are comparing const char*.  The equal_ranges will just call operator < on the key, this will obviously fail because the pointers will be different.

The simple solution would be to use std::string instead of const char*.

If you really want this to work, however you need to add your own less than operator to the definition, i.e. do something like:

class StringLessThan
{
public:
    bool operator()(const char* s1, const char* s2) const
    {
        return (strcmp(s1, s2) < 0);
    }
};

and then declare the multimap like:

std::multimap<const char*, A*, StringLessThan> myMap;

to erase a single element just call the erase method with a (non-const) iterator that points to the element you want to remove. So to remove the middle of your 3 elements:

std::multimap<const char*, A*, StringLessThan>::iterator mi = myMap.equal_range(str).first;
myMap.erase(++mi);

0
 
LVL 5

Expert Comment

by:proskig
ID: 2707303
You can also use std::string instead of char*
0
 

Author Comment

by:shadow66
ID: 2711974
Thanks for the quick answer, Jason. Stroustrop's book made it clear how to erase an entire key, but not how to erase a single entry (comparing pointers was my bonehead).

I decided to go with std::string all around, and it works great.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question