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