c++ - 如何初始化 C++ 中的字符串集?

标签 c++ string set

我在声明一个字符串集的时候有几个词要初始化。

...
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/

相关文章:

Python - 比较两个集合列表

java - 当集合大小超过 500.000 时,处理速度明显变慢

javascript - Ngxs - 将 Sets 这样的对象添加到存储中可以吗?

c++ - Dev C,math.h 问题

Python - 在嵌套循环中删除一行中的第一个单词

python - 如何只使字符串的一部分大写?

java - 输出: object@1b6d3586 java

C++ 字符串编码 UTF8/unicode

c++ - 程序得到 "Expression: string subscript out of range"

c++ - FLTK 等待事件关闭窗口