C++ 如何使用C+初始化无序集+;(g+;+;编译器)?

C++ 如何使用C+初始化无序集+;(g+;+;编译器)?,c++,initialization,unordered-set,C++,Initialization,Unordered Set,在尝试用g编译示例代码时,我发现了以下错误++ #include <iostream> #include <unordered_set> int main () { std::unordered_set<std::string> second ( {"red","green","blue"} ); // init list std::unordered_set<std::string> second1 {"red", "gree

在尝试用g编译示例代码时,我发现了以下错误++

#include <iostream>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> second ( {"red","green","blue"} );    // init list
   std::unordered_set<std::string> second1 {"red", "green", "blue"}; // Marcelo Cantos
   std::unordered_set<std::string> second2{{"red", "green", "blue"}}; // Marcelo Cantos
   std::unordered_set<std::string> second3 = {"red", "green", "blue"};// Marcelo Cantos   
   return 0;

}

$ gcc -o wp -Wall -Wextra test_set.cpp 
test_set.cpp:5:45: error: expected expression
   std::unordered_set<std::string> second ( {"red","green","blue"} )...
                                        ^
test_set.cpp:6:43: error: expected ';' at end of declaration
   std::unordered_set<std::string> second1 {"red", "green", "blue"};
                                          ^
                                          ;
test_set.cpp:7:43: error: expected ';' at end of declaration
   std::unordered_set<std::string> second2{{"red", "green", "blue"}};
                                          ^
                                          ;
test_set.cpp:8:36: error: non-aggregate type 'std::unordered_set<std::string>' cannot be initialized with an initializer list
   std::unordered_set<std::string> second3 = {"red", "green", "blue"};
                                   ^         ~~~~~~~~~~~~~~~~~~~~~~~~
4 errors generated.


$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
#包括
#包括
int main(){
无序设置第二({“红色”、“绿色”、“蓝色”});//初始化列表
无序的第二集{“红”、“绿”、“蓝”};//马塞洛州
无序的第二集{{“红色”、“绿色”、“蓝色”};//马塞洛州
std::无序_set second3={“红色”、“绿色”、“蓝色”};//Marcelo Cantos
返回0;
}
$gcc-o wp-Wall-Wextra测试集.cpp
test_set.cpp:5:45:错误:应为表达式
无序设置秒({“红色”、“绿色”、“蓝色”})。。。
^
测试集.cpp:6:43:错误:应为“;”在申报结束时
无序的第二组{“红色”、“绿色”、“蓝色”};
^
;
测试集.cpp:7:43:错误:应为“;”在申报结束时
无序的第二组{{“红色”、“绿色”、“蓝色”};
^
;
test_set.cpp:8:36:错误:无法使用初始值设定项列表初始化非聚合类型“std::unordered_set”
无序设置第二个3={“红色”、“绿色”、“蓝色”};
^         ~~~~~~~~~~~~~~~~~~~~~~~~
产生了4个错误。
$g++--版本
配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr--gxx include dir=/usr/include/c++/4.2.1
苹果LLVM 5.1版(clang-503.0.40)(基于LLVM 3.4svn)
目标:x86_64-apple-darwin13.3.0
线程模型:posix

通过使用编译器选项“-std=c++11”解决

$g++-o测试集-Wall-Wextra-std=c++11测试集.cpp对我来说很有用

Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
您可以尝试以下替代形式之一,所有这些都适用于clang 6.0:

…second{"red", "green", "blue"};
…second{{"red", "green", "blue"}};
…second = {"red", "green", "blue"};
对我有用

Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
您可以尝试以下替代形式之一,所有这些都适用于clang 6.0:

…second{"red", "green", "blue"};
…second{{"red", "green", "blue"}};
…second = {"red", "green", "blue"};

通过使用编译器选项“-std=c++11”解决


通过使用编译器选项“-std=c++11”解决


您可能应该编译,即使用
g++-Wall-Wextra-std=c++11 test\u set.cpp-o wp
#include
-std=c++11工作!谢谢你,巴兹尔。太快了!请不要编辑您的问题并添加“[已解决]”。它使堆栈溢出作为“Q&a”站点的目的无效。添加您的答案作为答案;你甚至可以自己“接受”它。谢谢Jongware。我已经解决了这个问题。您可能应该使用
g++-Wall-Wextra-std=c++11 test\u set.cpp-o wp
\include
-std=c++11进行编译!谢谢你,巴兹尔。太快了!请不要编辑您的问题并添加“[已解决]”。它使堆栈溢出作为“Q&a”站点的目的无效。添加您的答案作为答案;你甚至可以自己“接受”它。谢谢Jongware。我已经解决了这个问题,上面所有的问题只有在编译时使用-std=c++11时才有效。为每种情况编辑时都会显示错误消息。@Rock:啊,是的。我现在已经习惯了在C++11中工作,我并没有想到要问。所有这些都只在编译时使用-std=C++11时才起作用。为每种情况编辑时都会显示错误消息。@Rock:啊,是的。我现在已经习惯了在C++11中工作,我没有想到要问。