C++ 11实验,为什么我不能使用一些功能?

Max*_*ime 6 c++ c++11

我目前正在概述C++ 11的新功能,并且由于目前尚未理解的原因,其中一些不能编译.我使用gcc版本4.6.0 20100703(实验性)(GCC),因此根据GNU GCC常见问题解答,我支持所有功能.我尝试使用std = c ++ 0x和std = gnu ++ 0x标志进行编译.

非成员begin()&end()

例如,我不想在一段代码中使用非成员begin()和end():

#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) ) {
        //...
    }
Run Code Online (Sandbox Code Playgroud)

我明白了,

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
Run Code Online (Sandbox Code Playgroud)

我需要包含特殊标题吗?

适用范围

我的第二个(也是最后一个)问题更怪异,因为它不能依赖于我可能没有包含的黑魔法隐藏标题.

以下代码:

for ( auto kv : alias )
    cout << kv.first << " ~ " << kv.second << endl;
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

rangeFor/main.cc:15:17: error: expected initializer before ‘:’ token
Run Code Online (Sandbox Code Playgroud)

我希望我的问题不是你的问题,也不是你们的新手,你们会帮助我找出错误的地方:D

fsa*_*ues 5

它适用于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;
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

# /opt/gcc-4.6.1/bin/g++-4.6 --std=c++0x test.cc -o test && ./test
key ~ value
key ~ value
Run Code Online (Sandbox Code Playgroud)