Associative containers

hey there.
i want to learn how to use maps in a programm.
i want to learn how to initialize maps and then to iterate over them.
in order to learn this, im going by the third example given in
http://en.wikipedia.org/wiki/Associative_containers

i only changed some values in the given example:

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 <string>
#include <map>

using namespace std;
 
int main()
{
    map <int, float> polymap
     { 
      {1  , 2.1   },
      {100, 15.11 },
     };
 
    // Iterate over the map and print out all key/value pairs.
    for (const auto& element : data)
    {
        cout << "key= " << element.first;
        cout << "value= " << element.second << '\n';
    }
 
    return 0;
} 


if i try to compile that, some errors occur. even if i try to compile the original code given in the wikipedia article, the same errors still occur.

error1: data{ expected an ";"
error2: for (const auto& element : data) Error: Cannot deduce ´auto´ type (initializer required)
error3: for (const auto& element : data) Error: reference variable "element" requires an initializer.
error4: for (const auto& element : data) Error: expected an expression.

I want to clone the code in the given example in order to be able to use maps in my program, but since there are errors that i cant understand, so i cant do so.

another problem is, that i cant understand, what the following line means:
const auto& element : data

what ist the ":" good for?
i would be happy if i could "understand" this line and not only "using" it.

greetings rasputinxiaoshitou
Last edited on
Line 16 is what is known as a "for each" loop. What that does is, behind the scenes, create an iterator to parse through all of the object to the right of the colon (in this case data, but it really should be polymap). It then sets the object left of the colon (remove the ampersand) to each successive value in the object to the right as it iterates through all elements.

In fact, I believe those are the only two errors- replace data with polymap, and auto& with auto.
hey. thanks for your reply.
I can understand it know!
C++11 provides a range based for loop, but the older versions doesn´t. I´m using visual studio 2010, so that might be the reason that the example isn´t working on my platform, isn´t it?
Where can I get a new version for Visual Studio?
greetings
http://www.microsoft.com/en-us/download/details.aspx?id=34673

Visual Studio 2012, if I recall, is the most recent free version that you can get.
Ispil wrote:
remove the ampersand

Removing the ampersand & would make the element variable a copy of the elements in the map. Now it's just a std::pair<int, float> so it might not be noticeable but for larger objects it would affect performance unnecessarily.
Last edited on
The 2013 Express edition is available.
http://www.microsoft.com/en-us/download/details.aspx?id=43733
Hey, thank you guys.
I´m using Windows 7. Is there any free Version of Visual Studio newer than vs2010 that may be installed on my system?
greetings rasputinxiaoshitou
The link I posted should work fine for Windows 7, I recall having it installed on a Windows 7 machine I used.
Topic archived. No new replies allowed.