c++stringset

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.


Solution

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