I used to code in C++ 12 years ago, but left it for other simpler languages due to my job.
I'd like to renew my knowledge and tried to compile the solution proposed here, just to try this new way to iterate on vectors. But ran into a compile error:
expected initializer before ‘:’ token
I didn't know it was possible to avoid explicit declaration of iterators like that in C++
with the use of this (auto && elem : v)
. What version of C++
is it?
b.cpp
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <set>
int main()
{
std::vector<std::pair<std::string, std::string>> v
{ {"handgun", "bullets"},
{"turret", "bullets"}};
std::cout << "Initially: " << std::endl << std::endl;
for (auto && elem : v)
std::cout << elem.first << " " << elem.second << std::endl;
return 0;
}
Compilation
$ cc b.cpp -std=c++0x -o myprog
Errors
b.cpp: In function ‘int main()’:
b.cpp:15: error: expected initializer before ‘:’ token
...
C++ Compilers I use: g++
$ g++ --version
gives
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.