Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

How to initialize a string set in C++?

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.

Any help would be appreciated.

question from:https://stackoverflow.com/questions/12333783/how-to-initialize-a-string-set-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using 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]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...