简体   繁体   中英

finding element in map with list c++

I have a multimap with an integer(x) and a list(list).

std::map<int,std::list<int> > my_map;

Say mylist1 consists of elements 100,200,300 and I map it to integer 1

mylist2 consists of elements 99,199,299 and I map it to integer 2

my_map.insert(pair<int,std::list<int> > (1, mylist1));
my_map.insert(pair<int,std::list<int> > (2, mylist2));

Now given an element say 200, how can i return the value 1(saying that element 200 belongs to a list that is mapped to integer element 1?

You should use elements of the lists as keys and an integer as value. Given your example:

std::multimap<int, int> my_map;
my_map.insert(std::make_pair(100, 1));
my_map.insert(std::make_pair(200, 1));
my_map.insert(std::make_pair(300, 1));

my_map.insert(std::make_pair(99, 2));
my_map.insert(std::make_pair(199, 2));
my_map.insert(std::make_pair(299, 2));

then just use std::multimap<int,int>::equal_range function

As @NirMH told - what was the problem just with iterating over the map ? I assume you are using C++98 standard compiler. Here is the simple code for you ( http://rextester.com/WVBE96570 ):

#include <iostream>
#include <map>
#include <list>
#include <algorithm>

int main()
{
    std::map<int, std::list<int> > my_map;
    std::list<int> l1;
    l1.push_back(10);
    l1.push_back(20);

    std::list<int> l2;
    l2.push_back(30);
    l2.push_back(40);

    my_map[1] = l1;
    my_map[2] = l2;

    int value_to_find = 40;
    int list_index = -1;
    for(std::map<int, std::list<int> >::const_iterator p_int_list_pair = my_map.begin(),
        end = my_map.end(); p_int_list_pair != end; ++p_int_list_pair)
    {
        const int& curr_index = p_int_list_pair->first;
        const std::list<int>& curr_list = p_int_list_pair->second;
        std::list<int>::const_iterator curr_list_end = curr_list.end();
        if(std::find(curr_list.begin(), curr_list_end, value_to_find) != curr_list_end)
        {
            list_index = curr_index;
            break;
        }
    }

    std::cout << list_index;
}

Pseudo-Code:

Iterate over my_map
   For every iteration, search the value (list<int>) for your target integer
   If found, return the my_map iterator first item

You can use the find method from algorithm header to optimize the search.

Here's my C++11 take on it:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <utility>

using value_type = int;
using list_type = ::std::list<value_type>;
using key_type = int;
using map_type = ::std::map<key_type, list_type>;
using pair_type = map_type::value_type;


list_type my_list_1;
list_type my_list_2;
map_type my_map;


map_type::const_iterator find_key_of_first_list_containing (const value_type x, const map_type & map)
{
    auto map_cend = map.cend();
    for (auto map_cit = map.cbegin(); map_cit != map_cend; ++map_cit)
    {
        auto list = map_cit->second;
        auto list_cend = list.cend();
        auto list_cit = ::std::find(list.cbegin(), list_cend, x);
        if (list_cit != list_cend)
            return map_cit;
    }
    return map_cend;
}


int main (int, char **)
{
    my_list_1.push_back(100);
    my_list_1.push_back(200);
    my_list_1.push_back(300);

    my_list_2.push_back(99);
    my_list_2.push_back(199);
    my_list_2.push_back(299);

    my_map.insert(pair_type(1, my_list_1));
    my_map.insert(pair_type(2, my_list_2));

    auto i = find_key_of_first_list_containing(200, my_map);

    if (i != my_map.end())
        ::std::cout << "Found at: " << i->first << "\n";
    else
        ::std::cout << "Not found!\n";

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM