我目前正在回顾C++11的新特性,由于目前尚不清楚的原因,其中一些无法编译。我使用的是gcc 4.6.0版本20100703 (实验性) ( GCC ),因此根据GNU GCC常见问题解答,我尝试的所有功能都是supported。我尝试使用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我希望我的问题对你们来说不是离题或太新手,你们能帮我找出问题所在:
发布于 2011-11-02 03:03:10
适用于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 ~ valuehttps://stackoverflow.com/questions/7971368
复制相似问题