std::map initialisation

closed account (S879Nwbp)
Hello,

I have a question about the syntax of a map initialization and displaying
iterator values. Sample program

#include <vector>
#include <string>
#include <map>
#include <iostream>

std::vector< std::vector<int> > Vector1(2, std::vector<int>(2));

std::map <int, std::vector< std::vector<int> >::const_iterator> Map1;

int main ()
{

Vector1[0][0] = 3490;
Vector1[0][1] = -1;

Vector1[1][0] = 5383;
Vector1[1][1] = -1;


for (auto it = Vector1.cbegin (); it != Vector1.cend (); ++it)
{

Map1[(*it)[0]] = it; // Can the syntax of this line be explained?

std::cout << (*it) << std::endl; // How do I dereference the iterator
to see values?

}

}
Can the syntax of this line be explained?

Think of iterators as a generalisation of using a pointer to access an array member.

If you think of it as a pointer to an element of Vector1, then *it is the value of that element.

Since you've defined Vector1 to be a vector of vectors, then *it is a vector of ints. This means that (*it)[0] is an integer - it can be either 3490 or 5383, depending on which element of Vector1 it's "pointing" to.

So, Map1[(*it)[0]] = it; is equivalent to either:

Map1[3490] = it;

or:

Map1[5383] = it;

Does that make it clearer?

How do I dereference the iterator to see values?

Remember, Vector1 is a vector of vectors of ints, so *iter is a vector of ints. Having got your vector of ints, you can look at the contents that vector the same way as you'd look at the contents of any other vector of ints.
Last edited on
closed account (S879Nwbp)
Dear MikeyBoy

Thankyou very much for your advice. I actually need to use a structure and was wondering
what changes to the Map1 initialisation would be required;
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <vector>
#include <string>
#include <map>
#include <boost/assign.hpp>

int i;

struct ConeMap
{
    
    int ijk2;
    int tree;
    
};

std::vector<ConeMap> ConeMap1(6), ConeMap2(2);

int main()
{  

    ConeMap1[0].ijk2 = 3490;
    ConeMap1[1].ijk2 = 1288;
    ConeMap1[2].ijk2 = 5383;
    ConeMap1[3].ijk2 = 5383;
    ConeMap1[4].ijk2 = 5746;
    ConeMap1[5].ijk2 = 2341;

    ConeMap1[0].tree = 21;
    ConeMap1[1].tree = 18;
    ConeMap1[2].tree = 31;
    ConeMap1[3].tree = 25;
    ConeMap1[4].tree = 22;
    ConeMap1[5].tree = 35;

    ConeMap2[0].ijk2 = 3490;
    ConeMap2[1].ijk2 = 5383;

    ConeMap2[0].tree = -1;
    ConeMap2[1].tree = -1;

    std::map<int,std::vector<ConeMap>::const_iterator> Map1;

    for(auto it = ConeMap2.cbegin(); it!=ConeMap2.cend(); ++it)
    { 

        Map1[(*it)[0]]=it;// ijk2 is the integer key. in first map we put primary key and iterator to ConeMap2 where that key is

    }

    assert(Map1.size()== ConeMap2.size());// how unique is primary key?

    std::for_each(ConeMap1.cbegin(), ConeMap1.cend(), [&Map1] (const std::vector<ConeMap>& entryA )
    {

        auto itB= Map1.find(entryA.at(0));

//        Map1->next(); ?

        if (itB!=mapB.end()) // if we can make "JOIN"  we do it
        {
            
            auto entryB = itB->second;
            
            ConeMap1[i].tree = entryB->at(1);
            
            std::cout << ConeMap1[i].ijk2 << " " << ConeMap1[i].tree << std::endl;
            

        } else {
        
         std::cout << ConeMap1[i].ijk2 << " " << ConeMap1[i].tree << std::endl;
        
        }

        i++;

   });

} 

Last edited on
If you're going to keep posting more code, please use code tags to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
closed account (S879Nwbp)
Thankyou,

Problem solved Map1[(*it).ijk2]=it;
Last edited on
You're welcome - glad it helped you!
Last edited on
Topic archived. No new replies allowed.