getting arbitrarily located elements in a multimap

Started by
6 comments, last by snk_kid 18 years, 12 months ago
I'm having trouble switching my code from using a map, to a multimap. I've never used a multimap before, so I'm having some stupid little problems. My main concern, is simply how to get an element from the multimap given the element's key. Apparently, the class doesn't implement a [] operator, so I switched my code to this: PotentialRepeatingSeg* repSeg = (_potentialRepeatingSegs.find(quotient))->second; But, this just throws a few compiler errors which occur in the STL itself, not my own code, so I'm having quite some difficulty debugging them. Here's they are:
Quote: c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(41) : error C2440: 'initializing' : cannot convert from 'const std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> ' with [ _Ty1=std::vector<short>::size_type, _Ty2=int ] and [ _Ty1=int, _Ty2=int ] No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called c:\Documents and Settings\Owner\My Documents\Code\Projects\Pummill Comp, Problem 3\repdec.cpp(69) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<int,std::pair<std::vector<_Ty>::size_type,int>>(const std::pair<int,std::pair<std::vector<_Ty>::size_type,int>> &)' being compiled with [ _Ty1=const int, _Ty2=std::pair<int,int> *, _Ty=short ] c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(41) : error C2439: 'std::pair<_Ty1,_Ty2>::second' : member could not be initialized with [ _Ty1=const int, _Ty2=std::pair<int,int> * ] c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(51) : see declaration of 'std::pair<_Ty1,_Ty2>::second' with [ _Ty1=const int, _Ty2=std::pair<int,int> * ] c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(41) : error C2440: 'initializing' : cannot convert from 'const std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> ' with [ _Ty1=int, _Ty2=std::vector<short>::size_type ] and [ _Ty1=int, _Ty2=int ] No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called c:\Documents and Settings\Owner\My Documents\Code\Projects\Pummill Comp, Problem 3\repdec.cpp(81) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<int,std::pair<int,std::vector<_Ty>::size_type>>(const std::pair<int,std::pair<int,std::vector<_Ty>::size_type>> &)' being compiled with [ _Ty1=const int, _Ty2=std::pair<int,int> *, _Ty=short ] c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(41) : error C2439: 'std::pair<_Ty1,_Ty2>::second' : member could not be initialized with [ _Ty1=const int, _Ty2=std::pair<int,int> * ] c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(51) : see declaration of 'std::pair<_Ty1,_Ty2>::second' with [ _Ty1=const int, _Ty2=std::pair<int,int> * ]
Changing the above line to this: PotentialRepeatingSeg* repSeg = (*_potentialRepeatingSegs.find(quotient)).second; where I manually dereference the iterator yields the same errors (as I would expect it to). So my question: just how can I grab an element's value from a multimap given it's key? I'm pretty much stumped here, so any help/suggestions/comments are very welcome! Thank you all! [Edited by - nilkn on May 21, 2005 9:13:29 PM]
Advertisement
find, lower_bound, and upper_bound are probably what you want. They all return iterators. If whatever you looked for wasn't found, it should return the map's end iterator. Otherwise iterator->first should be the key you searched for, and iterator->second should be it's value.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I'm already using multimap::find() in this line:

PotentialRepeatingSeg* repSeg = _potentialRepeatingSegs.find(quotient)->second;

But it throws all of those errors. I'm assuming that it's not typical to get these errors when using multimap::find?

The value type is a typedef for a pair (typedef std::pair<int, int> PotentialRepeatingSeg;), could this have something to do with the errors?

Anyways, thanks for the reply.
From the very little info you've given us everything appears to be fine but these may give you some clues:

Quote:Original post by nilkn

c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(41) : error C2440: 'initializing' : cannot convert from
'const
std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> '
with

.......

No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\Documents and Settings\Owner\My Documents\Code\Projects\Pummill Comp, Problem 3\repdec.cpp(81) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<int,std::pair<std::vector<_Ty>::size_type,int>>
.....



We need more info like what is the type of PotentialRepeatingSeg, is it a type alias for another pair because it seems like you have a multimap that maps integers to a pair of size_t & integer was that intended? and where is that code located, a constant member function?
An example:

#include <iostream>#include <string>#include <iterator>#include <map>int main() {  typedef std::multimap<std::string, int> map_type;  typedef map_type::iterator iterator;  typedef map_type::value_type pair;  map_type map;    map.insert(pair("a", 1));  map.insert(pair("a", 2));  map.insert(pair("a", 3));  map.insert(pair("a", 4));    map.insert(pair("b", 5));  map.insert(pair("b", 6));  map.insert(pair("b", 7));  map.insert(pair("b", 8));    map.insert(pair("c", 9));  map.insert(pair("c", 10));  map.insert(pair("c", 11));  map.insert(pair("c", 12));    iterator i(map.find("b"));    if(i == map.end())   std::cout << "Not found!" << std::endl;  else   {    std::cout << i->first << " was " << i->second << std::endl;    i->second = 42;    std::cout << i->first << " is now " << i->second << std::endl;   }    std::cout << "All the values for b are: " << std::endl;     for(iterator i(map.lower_bound("b")), end(map.upper_bound("b")); i != end; ++i)   std::cout << i->second << std::endl;    return 0; }


Output:
b was 5b is now 42All the values for b are:42678
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Well, I already showed you the definition for PotentialRepeatingSeg in my second post. It's located in the global namespace, and no the function that the offending code is located in isn't const.

I could give you the full source, but that would be overkill and it's so convoluted right now it would do you no good.

What other information do you need?

I apologize if my question is naive, but I'm somewhat inexperienced with STL and totally inexperienced with multimap.
Thanks smart_idiot. I've figured it out. I don't think I was inserting the items properly. Not sure though. But either way, it works now.
Quote:Original post by nilkn
I already showed you the definition for PotentialRepeatingSeg in my second post.


Well i never saw it probably because we posted about the same time.

This topic is closed to new replies.

Advertisement