如何在 C++ 中初始化一个字符串集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12333783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:14  来源:igfitidea点击:

How to initialize a string set in C++?

c++stringset

提问by Crocode

I have a few words to be initialized while declaring a string set.

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

...
using namespace std;
set<string> str;

/*str has to contain some names like "John", "Kelly", "Amanda", "Kim".*/

I don't want to use str.insert("Name");each time.

我不想str.insert("Name");每次都用。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by orlp

Using C++11:

使用 C++11:

std::set<std::string> str = {"John", "Kelly", "Amanda", "Kim"};

Otherwise:

除此以外:

std::string tmp[] = {"John", "Kelly", "Amanda", "Kim"};
std::set<std::string> str(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));

回答by Drew Dormann

In C++11

在 C++11 中

Use initializer lists.

使用初始化列表

set<string> str { "John", "Kelly", "Amanda", "Kim" };

In C++03(I'm voting up @john's answer. It's very close what I would have given.)

在 C++03 中(我投票支持@john 的答案。它非常接近我给出的答案。)

Use the std::set( InputIterator first, InputIterator last, ...)constructor.

使用std::set( InputIterator first, InputIterator last, ...)构造函数。

string init[] = { "John", "Kelly", "Amanda", "Kim" };
set<string> str(init, init + sizeof(init)/sizeof(init[0]) );

回答by john

There's lots of ways you can do this, here's one

有很多方法可以做到这一点,这是一种

string init[] = { "John", "Kelly", "Amanda", "Kim" };
set<string> str(init, init + 4);

回答by CyberGuy

if you are not c++0x:

如果你不是 c++0x:

You should look at boost::assign

你应该看看 boost::assign

http://www.boost.org/doc/libs/1_39_0/libs/assign/doc/index.html#list_of

http://www.boost.org/doc/libs/1_39_0/libs/assign/doc/index.html#list_of

Also take a look at:

另请看:

Using STL/Boost to initialize a hard-coded set<vector<int> >

使用 STL/Boost 初始化硬编码 set<vector<int> >

#include <boost/assign/list_of.hpp> 
#include <vector>
#include <set>

using namespace std;
using namespace boost::assign;

int main()
{
    set<int>  A = list_of(1)(2)(3)(4);

    return 0; // not checked if compile
}

回答by oldrinb

There's multiple ways to do this. Using C++11, you can try either...

有多种方法可以做到这一点。使用 C++11,你可以尝试...

std::set<std::string> set {
  "John", "Kelly", "Amanda", "Kim"
};

... which uses an initializer list, or std::beginand std::end...

... 使用初始化列表,或std::beginstd::end...

std::string vals[] = { "John", "Kelly", "Amanda", "Kim" };
std::set<std::string> set(std::begin(vals), std::end(vals));

回答by Moshe Gottlieb

Create an array of strings(C array) and initialize the set with it's values (array pointers as iterators):
std::string values[] = { "John", "Kelly", "Amanda", "Kim" };
std::set s(values,values + sizeof(values)/sizeof(std::string));

创建一个字符串数组(C 数组)并用它的值(数组指针作为迭代器)初始化该集合:
std::string values[] = { "John", "Kelly", "Amanda", "Kim" };
std::set s(values,values + sizeof(values)/sizeof(std::string));

回答by Hakeem

the easiest way

最简单的方法

#define str string
....
set<str> so;
so.insert("John");
so.insert("Kelly");
so.insert("Amanda");
so.insert("Kim");