lsalamon
lsalamon

Reputation: 8174

How can I insert values at map to list pair?

My container is like this:

map<DWORD, list<pair<string,LARGE_INTEGER>>> map_to_list_items;

This code fails to compile:

map<DWORD, list<pair<string,LARGE_INTEGER>>>::iterator iter_map_to_list_items = map_list_items.find(dwThreadID);
if ( iter_map_to_list_items == map_to_list_items.end() )
{
    map_to_list_itens.insert ( pair<DWORD,pair<string,LARGE_INTEGER>>(dwTheadID, (string("Start"), m_TimePRE)));
}

Upvotes: 1

Views: 202

Answers (3)

Tacet
Tacet

Reputation: 1421

map<DWORD, list<pair<string,LARGE_INTEGER>>> map_to_list_itens;

So you have map, with value of type

list<pair<string,LARGE_INTEGER>>

after you are trying to add to map (as value / "second argument") just a pair (another thing, wrong type).

( pair<DWORD,pair<string,LARGE_INTEGER>>(dwTheadID, (string("Start"), m_TimePRE)));

If you have

 map<T1, list<pair<T2,T3> > > my_map;

you should adding to list otherwise. Like here:

 my_map[T1_object].push_back(make_pair(T2_object, T3_object)); //or
 my_map[T1_object].push_back(T2_T3_pair_object);
 //or in C++11
 my_map[T1_object].emplace_back(T2,T3);

More about it, you can read in reference, map, list. I suggest to start with easier example to understand how it works, like here.

Upvotes: 1

Mr.C64
Mr.C64

Reputation: 42924

My understanding is that you have two variables like this:

map<DWORD, list<pair<string, LARGE_INTEGER>>> map_to_list_items;
map<DWORD, list<pair<string, LARGE_INTEGER>>> map_list_tempos_threads;

The first thing you can do to simplify your code is to use the new C++11's auto keyword, instead of explicitly typing the whole cumbersome iterator name:

auto it = map_list_tempos_threads.find(dwThreadID);

(Note that you may have some typos in your code, since you had dwTheadID instead of dwThreadID, and ..._itens instead of ..._items).

Then, you can simply use std::map::operator[] overload to insert the new item in the map, if it was not there:

if (it == map_to_list_items.end())
{
    //
    // Insert the new list<pair<string, LARGE_INTEGER>> in the map,
    // assuming:
    //   -  key: dwThreadID
    //   -  string: "Start"
    //   -  LARGE_INTEGER: m_TimePRE
    //

    list<pair<string, LARGE_INTEGER>> l;    
    string s = "Start";
    l.push_back(make_pair(s, m_TimePRE))     // I'm assuming m_TimePRE is a LARGE_INTEGER
    map_to_list_items[dwThreadID] = move(l); // Move the list into the map
}

EDIT:

This is a much shorter version for the insertion code:

map_to_list_items[dwThreadID].emplace_back("Start", m_TimePRE);

The key point is that, if dwThreadID (the "key") is not in the map, a default-constructed "value" (i.e. list<pair<string, LARGE_INTEGER>>) is created in the map by the operator[] overload, and a reference to it is returned.
Then, list::emplace_back() is called on that reference, and the new ("Start", m_TimePRE) pair is added to the (previously empty, since default-constructed) list.

With this form there is much less to type, but several operations happen "under the hood"; instead, in the first form, the code and its logical steps are more explicit.

Upvotes: 1

Grozz
Grozz

Reputation: 8425

You are trying to insert pair<string, LARGE_INTEGER> as list<pair<string, LARGE_INTEGER>>.

I.e. you have a map<D, list<pair<S, L>> which looks like this:

D1 -> list [pair1, pair2, pair3, ...]

D2 -> list [pair4, ...]

What you are trying to do:

D3 -> pairX,

What you should do:

D3 -> list[pairX]

Upvotes: 1

Related Questions