Solved

Deleting an entry from a multimap

Posted on 2000-04-11
3
430 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now