Inserting item into map, which holds 2 more map - c++

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;

Related

Boost::multi_index_container with disparate key and element types

Is it possible to use different type data structures for keys and elements in a boost::multi_index_container? GCC lets me compile something like this:
struct StructA {
std::string name;
};
struct StructB {
int primary;
int secondary;
};
using mic =
multi_index_container<
StructA,
indexed_by<
composite_key<
StructB,
member< StructB, int, StructB::primary >,
member< StructB, int, StructB::secondary >
>>>;
First, I'm not sure if the above code violates any sort of multi_index_container usage. It certainly compiles with different element (StructA) and composite index key (StructB). Second, how do I insert/emplace into such a container? Third, how do I access elements in such a container?
How do you think this is supposed to work?

Insert Pair as a key in map using c++

I want to know how can insert pair in map using c++, here's my code:
map< pair<int, string>, int> timeline;
I tried to insert it using:
timeline.insert(pair<pair<int, string> , int>(make_pair(12, "str"), 33);
//and
timeline.insert(make_pair(12, "str"), 33);
but I got error
\main.cpp|66|error: no matching function for call to 'std::map<std::pair<int, std::basic_string<char> >, int&>::insert(std::pair<int, const char*>, int)'|
std::map::insert expects std::map::value_type as its argument, i.e. std::pair<const std::pair<int, string>, int>. e.g.
timeline.insert(make_pair(make_pair(12, "str"), 33));
or simpler as
timeline.insert({{12, "str"}, 33});
If you want to construct element in-place you can also use std::map::emplace, e.g.
timeline.emplace(make_pair(12, "str"), 33);
LIVE
When in doubt, simplify.
auto key = std::make_pair(12, "str");
auto value = 33;
timeline.insert(std::make_pair(key, value));
Simply use the traditional way:
timeline[key] = value;
For instertion and retrival of pair:
timeline[{1,"stackOverFlow"}] = 69;
for(auto i: timeline)
{
cout<< i.first.first;
cout<< i.first.second;
cout<< i.second;
}

map of map initialization

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.

c++ efficient data structure for bidirectional random access

I have elements a and b of two sets A and B. Now these are related to each other (0..1:n cardinality) so each a has at most one partner in B and each b can have several (at least one) associations to items in A.
A is a set of integer pairs and B are integers.
Is there efficient way to store such a "bi-directional" map?
A simple approach would be to use two maps:
map<pair<unsigned int, unsigned int>, unsigned int> AtoB
map<unsigned int, vector<pair<unsigned int, unsigned int> > > BtoA
But perhaps there is good way to deal with this more efficiently.
Thanks for your help
Boost contains two libraries to deal with this: Boost.Bimap and Boost.MultiIndex. The former is specific to the problem of bijective ("bidirectional") maps, while the second is more general and implements something akin to an in-memory database with arbitrary indexes.
Given that your unsigned int keys don't uniquely map to your pairs, I think MultiIndex is more in order. It's a long time since I've last used this library, but looking at the tutorial, you would need something like
struct YourData {
unsigned key;
std::pair<unsigned, unsigned> value;
};
typedef multi_index_container<
YourData,
indexed_by<
ordered_non_unique<member<YourData, unsigned, &YourData::key> >,
ordered_unique<member<YourData, std::pair<unsigned, unsigned>,
&YourData::value> >
>
> YourContainer;
If you don't want to use Boost, then you can at least simplify your current setup by replacing the
map<unsigned int, vector<pair<unsigned int, unsigned int> > >
by an std::multimap<unsigned, std::pair<unsigned, unsigned>>.
How about boost::bimap? http://www.boost.org/doc/libs/1_47_0/libs/bimap/doc/html/index.html I think it's for you.
Map and Multimap haves an efficiency of O(log n), so, I think is the best way to store your data. I suggest to use
map<pair<unsigned int, unsigned int>, unsigned int> AtoB
multimap<pair<unsigned int, unsigned int>, unsigned int> BtoA

Can I use std::pair as a key in std::multimap?

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));