我在声明一个字符串集的时候有几个词要初始化。
...
using namespace std;
set<string> str;
/*str has to contain some names like "John", "Kelly", "Amanda", "Kim".*/
我不想每次都使用 str.insert("Name");
。
任何帮助将不胜感激。
最佳答案
使用 C++11:
std::set<std::string> str = {"John", "Kelly", "Amanda", "Kim"};
否则:
std::string tmp[] = {"John", "Kelly", "Amanda", "Kim"};
std::set<std::string> str(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));
关于c++ - 如何初始化 C++ 中的字符串集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12333783/