Getting objects through iterators and putting in a Map

Hi

I have objects of classes in a vector. While iterating through the vector i want to insert each object from the iterator to a map, with some value. How can i do that.

Regards
Litu
What does your current code look like?

Otherwise, the general solution would be to use a for loop (assuming you're using the iterators directly, not through a range-based for loop) and the map's insert function (assuming you don't want to overwrite duplicate elements in the map).

http://www.cplusplus.com/reference/map/map/insert/

-Albatross
map<Class, int> mMyClass;

for (auto it = vClass.begin(); it != vClass.end(); ++it)
{
mMyClass.insert(make_pair(it, it->XYZ));
}

This does not work, objects are in vClass vector, need to insert those object to mMyClass as key
Wait, is meant to be a map of objects to... one of their members? Why?

Assuming that's not the case, you're forgetting to dereference your iterator here: make_pair(it

There should be an asterisk just before it.

-Albatross
I have objects of classes in a vector. While iterating through the vector i want to insert each object from the iterator to a map, with some value. How can i do that.
for (auto const& elt: my_vector) my_map[elt] = some_value;
This map

map<Class, int> mMyClass

needs to have the individual object and one of the parameter of the object as value, because some work needs to be done on its value and get the object.

binary 'operator' : no operator defined which takes a left-hand operand of type 'type' (or there is no acceptable conversion)

Getting this error when i put *befor it.

Regards
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
#include <map>

struct A {

    std::string name ;
    int number = 0 ;

    // make A LessThanComparable: https://en.cppreference.com/w/cpp/named_req/LessThanComparable
    // we can now use objects of type A as the keys of a map with default key compare
    friend bool operator< ( const A& first, const A& second ) {

         return first.name < second.name ; // compare names (case sensitive)
    }
};

int main() {

    // create a vector of A
    const std::vector<A> vec { { "zero", 0 }, { "one", 1 }, { "two", 2 }, { "three", 3 }, { "four", 4 } } ;

    {
        // define an empty map with A as the key and int as the mapped value
        std::map< A, int > map ;

        // iterate through the vector and insert (copies of) the objects into the map
        // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
        for( const A& a : vec ) map.emplace( a, a.number ) ;

        // print out the contents of the map
        for( const auto& pair : map ) {

            std::cout << "key A{" << pair.first.name << ',' << pair.first.number
                      << "}  mapped value: " << pair.second << '\n' ;
        }
    }

    std::cout << "\n---------\n\n" ;

    {
        // define an empty map with A as the key and int as the mapped value
        std::map< A, int > map ;

        // iterate through the vector and insert (copies of) the objects into the map
        for( auto iter = vec.begin() ; iter != vec.end() ; ++iter )
            map.insert( std::make_pair( *iter, iter->number ) ) ;

        // print out the contents of the map
        for( auto iter = map.begin() ; iter != map.end() ; ++iter ) {

            std::cout << "key A{" << iter->first.name << ',' << iter->first.number
                      << "}  mapped value: " << iter->second << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/600cb378db308e3d
why do we have to overload "<" operator, i dont have to do any comparison
Last edited on
You personally don't, but std::map does. That's how it keeps stuff ordered such that lookups can happen efficiently: by comparing the keys.

-Albatross
So, that means whenever we need to put an object in a map as a key we need to overload the "<" operator
or provide some comparisonn function
just as with std::unordered_map you need a hash function
we are low on magic lately.

> needs to have the individual object and one of the parameter of the object as
> value, because some work needs to be done on its value and get the object.
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
Topic archived. No new replies allowed.