Solved

Deleting an entry from a multimap

Posted on 2000-04-11
3
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

733 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