C ++ 11, why can't I use some functions?

I am currently reviewing the new features of C ++ 11 and for some unknown reason, some of them do not compile. I am using gcc version 4.6.0 20100703 (experimental) (GCC), so according to the GNU GCC FAQ, all the functions I tried are supported . I tried to compile both flags std = C ++ 0x and std = gnu ++ 0x.

Non member begin () and end ()

For example, I will not use non member begin () and end () in a piece of code, for example:

#include <iostream> #include <map> #include <utility> #include <iterator> using namespace std; int main ( ) { map < string, string > alias; alias.insert ( pair < string, string > ( "ll", "ls -al" ) ); // ... Other inserts auto it = begin(alias); while ( it != end(alias) ) { //... } 

And I get

 nonMemberBeginEnd//main.cc:15:24: error: 'begin' was not declared in this scope nonMemberBeginEnd//main.cc:15:24: error: unable to deduce 'auto' from '<expression error>' // Ok, this one is normal. nonMemberBeginEnd//main.cc:16:26: error: 'end' was not declared in this scope 

Should special headers be included?

For range

My second (and last) question is stranger, because it cannot depend on the hidden title of black magic, which I may not have included.

The following code:

 for ( auto kv : alias ) cout << kv.first << " ~ " << kv.second << endl; 

Give me the following errors:

 rangeFor/main.cc:15:17: error: expected initializer before ':' token 

I hope my questions are off topic or too new for you, and you will help me find out what is wrong: D

+6
source share
1 answer

Powered by gcc 4.6.1:

 #include <iostream> #include <map> #include <string> int main(int argc, char** argv) { std::map<std::string, std::string> alias = {{"key", "value"}}; for (auto kv: alias) std::cout << kv.first << " ~ " << kv.second << std::endl; auto it = begin(alias); while (it != end(alias) ) { std::cout << (*it).first << " ~ " << (*it).second << std::endl; it++; } return EXIT_SUCCESS; } 

And the result:

 # /opt/gcc-4.6.1/bin/g++-4.6 --std=c++0x test.cc -o test && ./test key ~ value key ~ value 
+5
source

Source: https://habr.com/ru/post/900552/


All Articles