Find a value of an element in vector in a std::map < int , std::vector <int >>

Related searches

Is there a prettier way to get a value from inside a vector in a map without using loops? For example , How to get the key of the vector which has an element of value 50?

std::map < int , std::vector < int >> myMap;
std::vector < int > a= {10,20,30};
std::vector < int > b= {50,60,70};

Can I get the key(s) if the value is in more than one vector ? For Example if 50 Is in both vectors ?

std::vector < int > a= {10,20,50};
std::vector < int > b= {50,60,70};

Is there a prettier way to get a value from inside a vector in a map without using loops?

You cannot avoid looping the map. But you can avoid writing the loop by using standard algorithm: std::find_if.

I mean more efficient

If you want the lookup to be efficient, then your choice of data structure is not good. You could instead use following:

std::unordered_multimap<int, int> myMap{
    {10, 1000},
    {20, 1000},
    {30, 1000},
    {50, 2000},
    {60, 2000},
    {70, 2000},

With this, the lookup has asymptotically constant complexity instead of linear.

How to search by value in a Map, Now we want to find all the elements with value 6 i.e.. at :: 6 is :: 6 bool findByValue(std::vector<K> & vec, std::map<K, V> mapOfElemen,� In this article will discuss how to search for all the elements in map with given value. Map internally store elements in Key-Value pair. In which keys are unique but values can be duplicate. There are member functions to search pairs by key i.e. std::map::find(). But there is no direct function to search for all the elements with given value.

Here works for every map, value and inner container:

#include <iostream>

#include <map>
#include <vector>
#include <algorithm>
#include <iterator>
#include <optional>

template <typename ValueT, typename MapT>
std::optional<typename MapT::key_type> find_key_for_value_contained_in_intern_container(const ValueT& value, const MapT& map) {
    auto key_it = std::find_if(std::begin(map), std::end(map), [&value] (const auto & element) {
        return std::find(std::begin(element.second), std::end(element.second), value) != std::end(element.second);
    return (key_it != std::end(map)) ? key_it->first : std::optional<typename MapT::key_type>();

int main() {
    std::map<int, std::vector<int >> myMap;
    std::vector<int> a = {10, 20, 30};
    std::vector<int> b = {50, 60, 70};
    myMap.insert({1000, a});
    myMap.insert({2000, b});

    int value_to_search{50};

    auto key = find_key_for_value_contained_in_intern_container(value_to_search, myMap);

    if (key)
    std::cout << "Value " << value_to_search << " is at key " << *key << "\n";

Searching in a map using std::map functions in C++, find() is used to search for the key-value pair and accepts the “key” in its argument to find it. This function returns the pointer to the element if the element is found, else it returns the pointer pointing to the last position of map i.e “map. end()” . Finding an element in vector using STL Algorithm std::find() Basically we need to iterate over all the elements of vector and check if given elements exists or not. This can be done in a single line using std::find i.e. // Check if element 22 exists in vector std::vector<int>::iterator it = std::find(vecOfNums.begin(), vecOfNums.end(), 22);

If you need to be efficient to find the key from the value, and conversely, you could use two maps. If needed, you can encapsulate that into a single class. You are then paying for efficiency of search with space. Something along the lines of:

class DoubleMap {
   DoubleMap() {}

   void insert(int key, std::vector<int> value) {
        myMap1.insert({key, value});
        myMap2.insert({value, key});

   std::vector<int> getValue (int key) {return myMap1[key];}
   int getKey(std::vector<int> value) {return myMap2[value];}

private : 
std::map <int, std::vector<int>> myMap1;
std::map<std::vector<int>, int> myMap2;

I wouldn't advise this kind of heavy class unless you really need the efficiency of search in both directions, though.

map::find - C++ Reference, Return value. An iterator to the element, if an element with specified key is found, or map::end otherwise. If the map object is const-qualified, the function returns a� Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end. Two keys are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the elements are passed as arguments).

map - C++ Reference, template < class Key, // map::key_type class T, // map::mapped_type class In a map, the key values are generally used to sort and uniquely identify the� The map::find() is a built-in function in C++ STL which returns an iterator or a constant iterator that refers to the position where the key is present in the map. If the key is not present in the map container, it returns an iterator or a constant iterator which refers to map.end().

std::map::find, Finds an element with key that compares equivalent to the value x . These templates only participate in overload resolution if the type Compare:: is_transparent� Return Value : An iterator to the first element in the range that compares equal to val. If no elements match, the function returns last. Examples: Input : 10 20 30 40 Output : Element 30 found at position : 2 (counting from zero) Input : 8 5 9 2 7 1 3 10 Output : Element 4 not found.

Index of an element in a vector can be found with the help of the two predefined functions in stl: find(): Used to find the position of element in the vector. distance(): Used to find distance between base iterater of vector and iterator return by find() function.

  • use std::find? Internally, it will result in some kind of loop for the vector and binary search if searching for the key of a map
  • Is there a way to write something like , myMap.find the key of the vector which contains a certain value ?
  • Maps find things based on the key, not the value. If you want to look at the values, then the map doesn't help with that.
  • You can have the vector also as a key of a map, but this helps only if you are comparing against the complete vector and not a single element of a vector. As this it keeps using any kind of loop iterating over the vectors content which is already implemented by std::find
  • What do you mean by "prettier"? You cannot avoid a loop here. You can hide it behind some helper function if you want. Is that what you mean by "prettier"?
  • but here , I will add the complexity of the loop which will unwrap the argument vector elements ?
  • @HazemAbaza Copying the vector into your map also has linear complexity.
  • This is perfect , As it will also solve the problem of repeated values across vectors Thanks@eerorika
  • so what will be the case If the value is in two vectors ?
  • It takes the first it can find. BTW please refrain from completly changing the question, because your problems changes. Its likely it will make it less helpful for other readers.