Public Group

# getting arbitrarily located elements in a multimap

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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::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::size_type,int>>(const std::pair::size_type,int>> &)' being compiled with [ _Ty1=const int, _Ty2=std::pair *, _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 * ] 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 * ] 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::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::size_type>>(const std::pair::size_type>> &)' being compiled with [ _Ty1=const int, _Ty2=std::pair *, _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 * ] 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 * ]
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]

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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 nilknc:\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::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?

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by nilknI already showed you the definition for PotentialRepeatingSeg in my second post.

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

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632168
• Total Posts
3004544

×