If i have a nested map as a value to a multimap how can i insert values into the inner map?

map with vector as key c++
multimap c++
c++ map
map of map c++
multimap example
vector map c++
map vs multimap c++
multimap vs map of vectors

I am trying to implement a dictionary where i have an outer map (as a multimap) with the key being the word being searched and the value being the inner map, which will have several pairs itself with different values which all can map to the outer multimap key.

for example: the word Distinct has several meanings depending if its a noun, verb, adjective, or pronoun ==> noun- A keyword in this program adverb- Uniquely. Written "distinctly" etc.

I am thinking of mapping the word Distinct as the key to the outer multimap and mapping the part of speech as the key to the inner map with the definitions as the inner map's values.

so far i declared the multimap as this:

typedef map<string, string> valMap;

multimap<string,valMap> myMultMap;

and i have tried adding values by using insert() like this:

myMultMap.insert("Diction", valMap.insert(pair<string,string>("fun", "first Value"));

I am just learning maps and still unsure as to the inner working of iterators in the maps. Any help would be greatly appreciated.

So first create the variable you need to insert:

valMap tmp;
tmp.insert(make_pair("fun", "first Value"));

Then insert this tmp into the main map:

myMultMap.insert(make_pair("Diction", move(tmp)));

Multimap in C++ Standard Template Library (STL), If i have a nested map as a value to a multimap how can i insert values into the inner map? - c++. If i have a nested map as a value to a multimap how can i insert values into the inner map? I am trying to implement a dictionary where i have an outer map (as a multimap) with the key being the word being searched and the value being the inner map, which will have several pairs itself with different values which all can map to the outer multimap key.

First, create the inner map and store it into a variable. Then use insert to add it into the outer map.

Introduction to Maps Container in STL in C++, First, to initialize (or assign or insert) elements, you have to pass key/value pairs, The inner lists define the key and the value of each element; the outer list When processing the elements, you again have to deal with key/value pairs. an example for using a map, which can be used as a so-called associative array. With a map, but not a multimap, values can be accessed and inserted using the subscript operator. Simply using a key as a subscript creates an entry--the default element is used as the associated value. Assigning to the result of the subscript changes the associated binding.

multimap::insert returns an iterator to the stored entry. You can use this iterator to insert items into inserted map without creating any temporary variables. It is also a good idea to use emplace forwarding arguments directly to entry constructor instead of insert to avoid creation of temporaries:

using t_Map = ::std::map<::std::string, ::std::string>;
using t_Multimap = ::std::multimap<::std::string, t_Map>;
t_Multimap multimap{};
auto const p_entry
{
    multimap.emplace
    (
        ::std::piecewise_construct
    ,   ::std::forward_as_tuple("Diction")
    ,   ::std::forward_as_tuple()
    )
};
auto & map{p_entry->second};
map.emplace
(
    ::std::piecewise_construct
,   ::std::forward_as_tuple("fun")
,   ::std::forward_as_tuple("first Value")
);

What is a Multimap in Java?, We had to associate a key with several values, and perform various operations. Should we use a map of vectors, or is a multimap more appropriate? have an entry corresponding to the queried event ID, operator[] will insert it for us of this piece of code reflects the nestedness of a vector inside a map. Nested Maps in C++ Map is a class in the Standard Template Libarary (STL) in c++, using which a multi-dimensional array could be created. According to cplusplus.com, maps are a kind of associative container that stores elements formed by the combination of a key value and a mapped value .

with key of type Key_type and value of type value_type. Member type value_type is the type of the elements contained in the container, defined in multimap as pair<const key_type,mapped_type> (see multimap member types). Return value In the versions returning a value, this is an iterator pointing to the newly inserted element in the multiset.

can store more than one value against a key. Both the keys and the values are stored in a collection, and considered to be alternates for Map<K, List<V>> or Map<K, Set<V>> (standard JDK Collections Framework). I have tried to put each element of this multimap into a normal map because it's removing duplicates by itself. The problem is that it doesn't keep the most recently added duplicate :c . c++ algorithm dictionary c++11 data-structures. share | improve this

Map is a sorted associative array of unique keys and associated data. The elements of the map are internally sorted from lowest to highest key value. not support [], therefore we cannot use syntax "m[].insert( make_pair())" we used in Example 2 to insert element. typedef multimap<int, string> innerMap; Map of vectors or multimap? Even if the map of vectors is maybe more natural to think of at first, the multimap leads to simpler code as soon as we need to iterate over the data . This advantage of the multimap come from the fact that it is not a nested structure, contrary to the map of vector.

Comments
  • Or maybe consider using a `map<pair<string, string>, string> instead. That way, you have a combined key instead of nested maps. It make some operations easier and other harder. It really depends on how the data is used. In production code, I would probably hide those details inside a class (or two)
  • wow it worked thanks so much! if i didnt use typedef map<string, string> valMap; but instead just declared the valMap how would i be able to accomplish the same? or is it i would have to use typedef to do it? so asking for clarity. and im guessing to access it i would need to iterate the inner map then the outer in a loop?
  • Obviously, without the typedef, you would wrote map<string, string> tmp and the remaining of the code won't change.
  • I'm just using the typedef because it's easy and it makes sure that it's the same type that we put inside the multimap.
  • do you mean something like ' myMultMap.insert(pair<string,map<string, string>>("Distinct", not sure how to insert here));'? thank you for your quick reply!