Jump to content
  • Advertisement
Sign in to follow this  

getting arbitrarily located elements in a multimap

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

If you intended to correct an error in the post then please contact us.

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<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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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?

Anyways, thanks for the reply.

Share this post


Link to post
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 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?

Share this post


Link to post
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 5
b is now 42
All the values for b are:
42
6
7
8

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!