Problem compiling stl map insert - c++

I'm having trouble compiling the following code:
typedef std::map<mUUID, block_ptr_t> BlockMap;
BlockMap _store;
std::pair< BlockMap::iterator, bool > it;
it = _store.insert(hint, std::make_pair(entry.block_uid, block));
The error is:
error: no match for ‘operator=’ in ‘it = BlockStore::_store.std::map<_Key, _Tp, _Compare, _Alloc>::insert [with _Key = mUUID, _Tp = Block*, _Compare = std::less<mUUID>, _Alloc = std::allocator<std::pair<const mUUID, Block*> >](lb, ((const std::pair<const mUUID, Block*>&)(& std::pair<const mUUID, Block*>(((const std::pair<mUUID, Block*>&)((const std::pair<mUUID, Block*>*)(& std::make_pair(_T1, _T2) [with _T1 = mUUID, _T2 = Block*](block))))))))’
/usr/include/c++/4.4/bits/stl_pair.h:68: note: candidates are: std::pair<std::_Rb_tree_iterator<std::pair<const mUUID, Block*> >, bool>& std::pair<std::_Rb_tree_iterator<std::pair<const mUUID, Block*> >, bool>::operator=(const std::pair<std::_Rb_tree_iterator<std::pair<const mUUID, Block*> >, bool>&)
It seems to be related to the assignment because if I don't assign it to "it" it compiles without error.

The version of insert that takes a hint returns only an iterator, not a pair.

First of all, your example is incomplete - _store is defined nowhere.
Second, it seems you are trying to assign an iterator to a std::pair - why do you need a pair, anyway?
The following should work, provided that _store is of type BlockMap:
BlockMap::iterator it;
it = _store.insert(std::make_pair(entry.block_uid, block));

Related

Map Error in C++

I am using GCC 4.6 compiler and when I build my code (ns3) I get the error:
In file included from /usr/include/c++/4.4/map:60,
from ../src/internet-stack/tcp-typedefs.h:23,
from ../src/internet-stack/mp-tcp-socket-impl.cc:16:
/usr/include/c++/4.4/bits/stl_tree.h: In member function ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = unsigned int, _Key = unsigned int, _Val = std::pair<const unsigned int, unsigned int>, _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]’:
/usr/include/c++/4.4/bits/stl_map.h:553: instantiated from ‘void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = unsigned int, _Key = unsigned int, _Tp = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]’
../src/internet-stack/mp-tcp-socket-impl.cc:1536: instantiated from here
/usr/include/c++/4.4/bits/stl_tree.h:1324: error: invalid type argument of ‘unary *’
This is because I used a map:
map<uint32_t, uint32_t> dis;
in which I inserted like this:
dis.insert(p,t);
I can't figure out a way to resolve this. Thanks for the help.
I think you are not using the insert method like you are supposed to do. I am not sure because i can't see the declaration of the arguments that you are using, but the compiler is saying that you are using the method:
void insert (InputIterator first, InputIterator last)
so the arguments may be iterators of the same container. Here the first argument designates the begin of the container to be copied and the last argument marks the end of this range (not including the last element).
These are the others options you have:
pair<iterator,bool> insert (const value_type& val);
This is the most usual and i guess you want to use this function, it inserts the element val into the map. Here val is some variable with type pair<T1,T2> where T1 and T2 are the arguments that you put in when creating the map (in your case unsigned int, unsigned int). You can use the function make_pair(first, second) of the std.
iterator insert (iterator position, const value_type& val);
The last one is like the previous but it tells to the map that the position of the new element may be near position. But this is just a hint for perfomance purpose since map is a tree with a well-defined order.
So your code might be something like dis.insert(make_pair(p,t));.
Regards.
You use libs from GCC 4.4 with GCC 4.6 - this could be the problem.
Try to pass 4.6 headers to compiler (like -I/usr/include/c++/4.6)

find struct in vector

I want to find a struct in a vector, but I'm having some troubles. I read several posts about this, but these all search for one element of the struct: I want to be able to compare multiple elements of the struct while searching. My struct and vector are defined as:
struct subscription {
int tournamentid;
int sessionid;
int matchid;
bool operator==(const subscription& m) const {
return ((m.matchid == matchid)&&(m.sessionid==sessionid)&&(m.tournamentid==tournamentid));
}
};
vector<subscription> subscriptions;
Then I want to search for a struct in the vector subscriptions, but since the combination of sessionid and matchid is unique, I need to search for both. Searching for only one will result in multiple results.
subscription match;
match.tournamentid = 54253876;
match.sessionid = 56066789;
match.matchid = 1108;
subscriptions.push_back(match);
it = find(subscriptions.begin(), subscriptions.end(), match);
The find function give the following error during compiling:
main.cpp:245:68: error: no match for ‘operator=’ in ‘it = std::find [with _IIter = __gnu_cxx::__normal_iterator >, _Tp = echo_client_handler::subscription](((echo_client_handler*)this)->echo_client_handler::subscriptions.std::vector<_Tp, _Alloc>::begin with _Tp = echo_client_handler::subscription, _Alloc = std::allocator, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = echo_client_handler::subscription*, ((echo_client_handler*)this)->echo_client_handler::subscriptions.std::vector<_Tp, _Alloc>::end with _Tp = echo_client_handler::subscription, _Alloc = std::allocator, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = echo_client_handler::subscription*, (*(const echo_client_handler::subscription*)(& match)))’
And a WHOLE lot more :) So the operator is not defined correctly, but how should that be done? Can anyone help me? How to search for multiple elements instead of only one element of a struct?
May be you did not specified type for it?
std::vector<subscription>::iterator it =
find(subscriptions.begin(), subscriptions.end(), match);

priority queue in stl

I followed the article to code a hanffman coding method using stl's priority_queue, however I think there are some bugs in the final code or it's not updated. The main problem is the declaration of the priority_queue, I believe it should take three parameters like: priority_queue< node, vector, greater > q, instead of priority_queue, greater > q.
However, even with this changes, the gcc compiler still give the errors like:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = node]’:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:279: instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Distance = long int, _Tp = node, _Compare = std::greater<node>]’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_heap.h:404: instantiated from ‘void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node*, std::vector<node, std::allocator<node> > >, _Compare = std::greater<node>]’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_queue.h:367: instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = node, _Sequence = std::vector<node, std::allocator<node> >, _Compare = std::greater<node>]’ hanffman.cpp:119: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:218: error: no match for ‘operator>’ in ‘__x > __y’
which I don't understand. The full code is available here
You need to define an operator>() for your class because you are using std::greater<> as the comparison in the priority_queue. The only operator I saw in the code was operator<().
You are missing the definition of the > operator for node (you only define the < operator).

Why does this insertion into map<int, int> fails?

I'm writing a parser using Bison, and am using a map for semantic evaluation.
In one of the functions I have the following code:
map<int, int>* result = new map<int, int>();
map<int, int>::iterator liter;
map<int, int>::iterator riter;
liter = lval.polyMap.begin();
riter = rval.polyMap.begin();
l = liter->first;
r = riter->first;
(*result).insert(l, (booleanCondition()) ? liter->second : -liter->second);
(polyMap is of type map<int, int>).
And this gives me the following errors:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h: In member functio
n `void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(
_II, _II) [with _InputIterator = int, _Key = int, _Val = std::pair<const int, in
t>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::l
ess<int>, _Alloc = std::allocator<std::pair<const int, int> >]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_map.h:397: instantiated
from `void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _Input
Iterator) [with _InputIterator = int, _Key = int, _Tp = int, _Compare = std::les
s<int>, _Alloc = std::allocator<std::pair<const int, int> >]'
polynom.ypp:147: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h:996: error: invali
d type argument of `unary *'
Where polynom.ypp line 147 contains (*result).insert(l, (booleanCondition()) ? liter->second : -liter->second);
What's the problem?
You might want to call (*result).insert(make_pair(l, (booleanCondition()) ? liter->second : -liter->second));

C++ STL:map search by iterator to another map

I'm trying to jump through some hoops to organize data in a special way. I'm including a simplified piece of code that demonstrates my pain.
I can't use boost.
I'm using the latest version of g++ in cygwin.
#include <iostream>
#include <map>
using namespace std;
int main () {
map< int,int > genmap;
map< int,int >::iterator genmapit;
map< map<int,int>::iterator,int > itermap;
// insert something into genmap
genmap.insert (make_pair(1,500) );
// find and return iterator.
genmapit=genmap.find(1);
// insert the iterator/int into itermap. Dies on each of the following 3 versions of this line.
//itermap[genmapit] = 600; // crash
//itermap.insert ( pair< map<int,int>::iterator,int >(genmapit,600) ); // crash
itermap.insert ( make_pair(genmapit,600) ); // crash
return 0;
}
So as you can see, I have 1 simple map, an iterator to that map and another map that has the first argument as an iterator to the first map.
It's clear from this:
Why can't I put an iterator in map?
That I can have an iterator as the second argument. However, the way shown above provides this:
$ make
g++ -c -o main.o main.cpp
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h: In member fun
ction `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp =
std::_Rb_tree_iterator<std::pair<const int, int> >]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h:871: instantiate
d from `std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _All
oc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::i
nsert_unique(const _Val&) [with _Key = std::_Rb_tree_iterator<std::pair<const in
t, int> >, _Val = std::pair<const std::_Rb_tree_iterator<std::pair<const int, in
t> >, int>, _KeyOfValue = std::_Select1st<std::pair<const std::_Rb_tree_iterator
<std::pair<const int, int> >, int> >, _Compare = std::less<std::_Rb_tree_iterato
r<std::pair<const int, int> > >, _Alloc = std::allocator<std::pair<const std::_R
b_tree_iterator<std::pair<const int, int> >, int> >]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_map.h:360: instantiated
from `std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_
Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator, bool> std::
map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [wit
h _Key = std::_Rb_tree_iterator<std::pair<const int, int> >, _Tp = int, _Compare
= std::less<std::_Rb_tree_iterator<std::pair<const int, int> > >, _Alloc = std:
:allocator<std::pair<const std::_Rb_tree_iterator<std::pair<const int, int> >, i
nt> >]'
main.cpp:23: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:227: error: no
match for 'operator<' in '__x < __y'
make: *** [main.o] Error 1
"instantiated from here" tells me nothing and a web search gives me no info on this.
Does STL:map simply not allow for this? I can recode my app to work around this but it will be very inefficient and I would like to get this working. Is there another kind of pointer I can make for a map element I could use?
Thanks for your time.
You can't do this because std::map iterators are not random access iterators so aren't comparable with <.
Instead, you could use pointers to the value_type in the first map as a map key.
You have to learn to read the error messages. In particular look at the message that comes after the long-winded description where the error happened:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:227: error: no
match for 'operator<' in '__x < __y'
Map iterators are not comparable with less-than operator which the map uses by default.
I suppose you can provide a comparison function that compares the pairs pointed to by the iterator, since the iterators themselves cannot be easily compared in a meaningful way.
struct CompareIterator
{
template <class FirstIter, class SecondIter>
bool operator()(FirstIter lhv, SecondIter rhv) const
{
return *lhv < *rhv;
}
};
//usage with map:
map< map<int,int>::iterator,int, CompareIterator > itermap;
std::pair defines operator<. I also used two iterator types, since it might be possible the types are different (iterator and const_iterator)
map<Key, Value>
The map iterator as key element into another map is not possible because map expects operator < to be defined by default to the key. If the Key (in this case map iterator) is not defined then you need to pass a functor as a predicate function that provides the comparison of Key (map iterator).