簡體   English   中英

C ++ 11實驗,為什么我不能使用一些功能?

[英]C++11 experiments, why can't I use some of the features?

我目前正在概述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) ) {
        //...
    }

我明白了,

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

我需要包含特殊標題嗎?

適用范圍

我的第二個(也是最后一個)問題更怪異,因為它不能依賴於我可能沒有包含的黑魔法隱藏標題。

以下代碼:

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

給我以下錯誤:

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

我希望我的問題不是你們的主題,也不是你們的新手,你們會幫助我找出錯誤的地方:D

它適用於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;
}

結果如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM