How to use find for the inner map

Jan 5, 2017 at 11:53pm
Hello Everyone,
I need a help in using "find" for the inner map.
My 2D map is like this
 
  std::map<int, pair<int, double> > my2DMap;


and I am using "find" for the outer map like below and it works fine:

1
2
3
4
5
6
my2DMap.insert(pair<int, pair<int, double> >(x, pair<int, double>(y, z)));

std::map<int, pair<int, double> >::iterator itv = my2DMap.find(x);
if (itv == my2DMap.end()){
........
}

But I am not able to use "find" for the inner map to search for "y".
Is there any way I can use "find" to search the integer "y" without using an additional map for the inner key-value pair (int-double in my case)?

Thanks.
Jan 6, 2017 at 12:23am
That is not a "2D map". Each key has exactly one value. For key "x", the value either has "y" as the integer component, or does not.

How about std::map<pair<int,int>, double>
Jan 6, 2017 at 2:54am
You can reach the "inner map" or the value element of the key-value pair in the following way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <map>
#include <tuple>

int main()
{
    std::map<int, std::pair<int, double> > my2DMap;

    my2DMap.insert (std::make_pair(1, std::make_pair(2, 3.4)));
    my2DMap.insert (std::make_pair(5, std::make_pair(6, 7.8)));

    auto elem = my2DMap.find(5);

    if(elem != my2DMap.end())
    {
       std::cout << elem -> second.second << '\n';
    }
    else
    {
        std::cout << "Key not found \n";
    }
}
Jan 6, 2017 at 11:43am
Thanks Keskiverto.
I tried like how you mentioned as below, but it is searching the whole pair of (x, y):
1
2
3
myMap.insert(std::make_pair(std::make_pair(x, y), z));
    
auto elem = myMap.find(std::make_pair(x, y));

But I want to find first int (x), if it is available, find the next int (y), if that is available then update the value of the double (z). If int (y) is not found then insert (y, z) into the map.
If x is not available then insert (x,y,z) into the map.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (myMap.find(x) = true)
{
    if (myMap.find(y) = true)
    {
        update z;
    }
    else
    {
        insert (y, z);
    }
}
else
{
insert (x, y, z);
}


Thanks Gunnerfunner. I am struggling to find the next int in the inner pair (2 or 6 in the example you have showed). How can I find it?

Many thanks.
Jan 6, 2017 at 11:52am
Jan 6, 2017 at 12:40pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <map>
#include <tuple>

int main()
{
    std::map<int, std::pair<int, double> > my2DMap;

    my2DMap.insert (std::make_pair(1, std::make_pair(2, 3.4)));
    my2DMap.insert (std::make_pair(5, std::make_pair(6, 7.8)));

    int searchVal = 9;
    bool match = false;

    for (auto itr = my2DMap.begin(); itr != my2DMap.end(); itr++)
    {
        if(itr->second.first == searchVal)
        {
            std::cout << "Key is: " << itr->first << " and int's double pair is: " << itr->second.second << '\n';
            match = true;
        }
    }
    if (match == false)
    {
        std::cout << "Not found\n";
    }
}
Jan 6, 2017 at 4:26pm
Thank you so much Gunnerfunner.
Jan 6, 2017 at 5:17pm
But I want to find first int (x), if it is available, find the next int (y), if that is available then update the value of the double (z). If int (y) is not found then insert (y, z) into the map.

Indeed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <map>

int main()
{
    std::map<std::pair<int, int>, double> my2DMap;

    // these insert
    my2DMap[ std::make_pair(1, 2) ] = 3.4;
    my2DMap[ std::make_pair(5, 6) ] = 7.8;

    for ( auto & itr : my2DMap ) {
        std::cout << "x=" << itr.first.first << " y="  << itr.first.second << " z=" << itr.second << '\n';
    }
    std::cout << '\n'; 

    // this updates
    my2DMap[ std::make_pair(1, 2) ] = 4.2;
    
    for ( auto & itr : my2DMap ) {
        std::cout << "x=" << itr.first.first << " y="  << itr.first.second << " z=" << itr.second << '\n';
    }
}
Jan 8, 2017 at 2:27pm
Thanks a lot Keskiverto.
Topic archived. No new replies allowed.