I want to use 2 integer numbers as a key in multimap. So I wonder is a structure like
std::multimap<std::pair<int, int>, MyClass> M;
M.insert(std::pair<int,int>(X1,Y1), MyClassObject);
possible in C++? When compiling I get the following error message:
C:\1\1-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK_________\..\main.cpp:199:
error: no matching function for
call to 'std::multimap<std::pair<int, int>, MyClass, std::less<std::pair<int, int> >,
std::allocator<std::pair<const std::pair<int, int>, MyClass> > >::insert(std::pair<int, int>,
MyClass)'
insert takes a pair of key,value, so you need
M.insert(std::make_pair(std::make_pair(X1,Y1),MyClassObject));
Related
I have a vector of a map of int and float
std::vector<std::map<int, float>> datad;
And I want to iterate over the maps in vector and inside over pairs in map
for (std::vector<std::map<int, float>>::iterator currentMap = datad.begin(); currentMap < datad.end(); ++currentMap)
{
for (std::map<int, float>::iterator it = currentMap->begin(); it < currentMap->end(); ++it)
{/*some code here*/}
}
But in the second loop g++ compiler gives an error:
no match for ‘operator<’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const int, float> >’ and ‘std::map<int, float>::iterator’ {aka ‘std::_Rb_tree_iterator<std::pair<const int, float> >’})
But isn't it looks like the types
std::_Rb_tree_iterator<std::pair<const int, float>>
std::_Rb_tree_iterator<std::pair<const int, float>>
are the same?
Yes, I see that compiler says the currentMap->end() have the type std::map<int, float>::iterator but...it is aka _Rb_tree_iterator...
maybe
static_cast<std::_Rb_tree_iterator<std::pair<const int, float>>>(currentMap->end())
What is the easiest way to continue using iterators and fix the problem?
PS. static_cast don't work, it gives
no match for ‘operator<’ (operand types are ‘std::map<int, float>::iterator’ {aka ‘std::_Rb_tree_iterator<std::pair<const int, float> >’} and ‘std::map<int, float>::iterator’ {aka ‘std::_Rb_tree_iterator<std::pair<const int, float> >’})
PSS. I've also tried auto but it also doesn't work (the same error as the first one)
std::map's iterators are bidirectional iterators, not random access iterators. Therefore they do not provide relational comparison operators. You can only equality-compare them.
So replace < with !=, which is the standard way of writing iterator loops. Or even better, replace the loop with a range-for loop:
for (auto& currentMap : datad)
{
for (auto& el : currentMap)
{
/* Use el here as reference to the (pair) element of the map. */
}
}
I'm trying to update a key in a nested map if it exists or insert it if it doesn't. I'm trying to use an iterator with lower_bound to make this process efficient.
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> maps;
cache::iterator iter(maps[command[1]].lower_bound(command[2]));
if (iter == maps[command[1]].end() || command[2] < iter->first) {
maps[command[1]].insert(iter, std::make_pair(command[2], command[3]));
} else {
iter->second = command[3];
}
I'm getting the following compile time error:
no member named 'lower_bound' in 'std::unordered_map<std::basic_string<char>, std::basic_string<char>, std::hash<std::string>, std::equal_to<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > > >'
As the name suggests, unordered_map is not ordered in any particular way. Because lower_bound methods and functions refer to the order of elements, they make sense only on ordered data. That's why there is no such method for unordered_map.
Many benchmarks on many compilers have shown that an std::map, with fewer that a thousand elements, is significantly faster than std::unordered_map. This means that you should consider switching to an std::map, or use the following:
maps[command[1]].insert_or_assign(command[2], command[3]);
I am trying to initialize a map of map but I am unsure what mistake I am doing. Below is the sample code.
static std::map<std::string, std::map<std::string,std::string>> _ScalingMapVolume ={
{"AA",{"busy_timeout","test"}},
{"BB",{"cache_size","10000"}}
};
The error I am getting is;
error: no match for call to ‘(std::_Select1st<std::pair<const std::basic_string<char>, std::basic_string<char> > >) (const char&)’
{"busy_timeout","test"} is not the value of a map, but a pair. You need {{"busy_timeout","test"}}.
Your code should look like this:
static std::map<std::string, std::map<std::string, std::string>> _ScalingMapVolume = {
{"AA", {{"busy_timeout", "test"}}},
{"BB", {{"cache_size", "10000"}}}
};
init = {{"AA", {"busy_timeout", "test"}}, ...}
You are missing one set of braces, since the value_type of the map is std::pair<const std::string, std::map<std::string, std::string>>. The value_type of the mapped_type is
std::pair<const std::string, std::string>. So you need to use it that way.
I am trying to insert an item, into a map, which holds two other map.
map< map<int, int> , map<int, int> > GC;
map<int, int> A;
A.insert(pair<int,int>(1,1));
map<int, int>:: iterator p1 = A.begin();
map<int, int> B;
B.insert(pair<int,int>(2,3));
map<int, int>:: iterator p2 = B.begin();
GC.insert( pair< map<int,int>, map<int,int> > (*p1, *p2) );
Well, as presumed its not working.
How to do it?
EDIT:
It gives the following error:
E:\CB\test.cpp|20|error: no matching function for call to 'std::pair<std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >, std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > > >::pair(std::pair<const int, int>&, std::pair<const int, int>&)'
I am not sure what are trying to achieve here .....
Your key and value needs to be an map object in order to work...
In that case only possibility is
GC.insert( pair< map<int,int>, map<int,int> > (A,B) );
I think you really want a Node class with a list/vector of Edge objects. Each Edge object would contain the edge weight and a pointer to the Node that edge connected to.
There may be better ways to create a graph than I have described, but this should get you thinking in the right direction.
map<keyValue, VertexIndexValue>, map<(EdgeWeight1,VertexIndexValue1), (EdgeWeight2,VertexIndexValue2)......>
I think what you want is to use a structure as the second element of your map. It gives you the chance to hold whatever you want in the map without it getting confusing.
struct sMapValue
{
int mVertIndex;
map<int, int> mVertEdgeMap;
};
map< int, sMapValue> GC;
I have an STL map that I want to iterate through, and can't seem to get the code to work. The code is:
//PowerupInfo is a struct defined in this class's header file
std::map<std::string, PowerupInfo> powerups;
...populate powerups
std::map<std::string, PowerupInfo>::iterator iter;
for (iter = powerups.begin(); iter != powerups.end(); iter++) {
return iter->second.type ;
}
The error message I get is:
error: no match for 'operator=' in 'iter = (((const std::map<std::string, PowerupInfo, std::less<std::string>, std::allocator<std::pair<const std::string, PowerupInfo> > >)((const PowerupList)this)) + 24u)->std::map<_Key, _Tp, _Compare, _Alloc>::begin with _Key = std::string, _Tp = PowerupInfo, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, PowerupInfo> >'|
note: candidates are: std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >& std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >::operator=(const std::_Rb_tree_iterator<std::pair<const std::string, PowerupInfo> >&)|
So I would normally assume that the problem has to do with setting iter equal to something it doesn't like, as it's not finding a match for 'operator='. But why? Why wouldn't that assignment work?
EDIT:
Turns out the method WAS const, causing the reference to powerups to be const as well, causing the error. I was just doing a bad job reading my own code. Thanks guys!
Your map name is poweruplist not powerups (You are using this name in the for loop). If this is not the cause of the error, then it looks like you are for loop is in a function which accepts the map by const reference (or is a const member function of a class). In that case your type of iterator should be const_iterator and not iterator.
Reformatting error code to make it readable:
error: no match for 'operator=' in
'iter =
((
(const std::map<std::string, PowerupInfo>*)((const PowerupList*)this)
)
+ 24u
)->std::map<std::string, PowerupInfo>::begin()'
Does not look the error message to the code you supplied.
Please cut and past the code. Otherwise it is meaningless.