Solved

Deleting an entry from a multimap

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 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

17 Experts available now in Live!

Get 1:1 Help Now