Aquarius_Girl
Aquarius_Girl

Reputation: 22916

How to pass a structure to a STL map?

typedef struct
{
    pthread_t threadId;
    int       acceptSocketD;
    char      *message;
} threadData;

map <unsigned int, threadData> serverPortNumberThreadId;
map <unsigned int, threadData> :: iterator serverPortNumberThreadIdIter;

usage:

threadData obj; 
obj.threadId      = 0;
obj.acceptSocketD = 0;
obj.message       = "Excuse Me, please!";

serverPortNumberThreadId.insert (3490, obj);

error:

error: no matching function for call to ‘std::map<unsigned int, threadData>::insert(int, threadData&)’
/usr/include/c++/4.5/bits/stl_map.h:500:7: note: candidates are: std::pair<typename std::map<_Key, _Tp, _Compare, _Alloc>::_Rep_type::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::map<_Key, _Tp, _Compare, _Alloc>::value_type&) [with _Key = unsigned int, _Tp = threadData, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, threadData> >, typename std::map<_Key, _Tp, _Compare, _Alloc>::_Rep_type::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, threadData>]
/usr/include/c++/4.5/bits/stl_map.h:540:7: note:                 std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::iterator, const std::map<_Key, _Tp, _Compare, _Alloc>::value_type&) [with _Key = unsigned int, _Tp = threadData, _Compare = std::less<unsigned int>, _Alloc = std::allocator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, threadData> >, std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, threadData>]
tcpClient.cpp: In function ‘int main(int, char**)’

Upvotes: 6

Views: 19607

Answers (4)

Steve Jessop
Steve Jessop

Reputation: 279255

You need a pair for the insert function, because in common with other containers insert takes the value_type, which in the case of map is a pair -- each entry has a key and a mapped value. The value_type represents one entry in the container and hence includes both.

You could write serverPortNumberThreadId[3490] = obj; instead, if you prefer the look of it. Behavior is sometimes different (insert does nothing if the key already exists, whereas this code overwrites it, but unless you're relying on that behavior of insert already this makes no difference to you). Performance might be slightly different in terms of the number of threadData objects created/copied/assigned.

Upvotes: 3

BruceAdi
BruceAdi

Reputation: 1989

 //always overwrites the old value
 serverPortNumberThreadId[3490]=obj;
 //only insert a new one
 serverPortNumberThreadId.insert(std::make_pair(3490, obj));

Upvotes: 3

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22094

You have to send a pair to insert function and not key,value.

mymap.insert ( pair<unsigned int,threadData>(3490, obj) );

Upvotes: 1

BЈовић
BЈовић

Reputation: 64223

You need to insert the value into map:

serverPortNumberThreadId.insert ( std::make_pair( 3490, obj) );

For other ways to insert into map, see the map::insert() reference page.

Upvotes: 8

Related Questions