user3483187
user3483187

Reputation: 33

How to initialize a: set<pair<string, string>>?

in c++, so I need to use the following:

set<pair<string, string>> info;

where the set info has pairs of strings within it. How can I initialize this info variable? I don't need to initialize it to anything specific. But just for the sake of initialization?

For example:

int i = 0;

Upvotes: 3

Views: 2974

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

std::set and std::string both have default constructors which properly initialize them as empty. So you don't have to do anything special, your declaration is sufficient.

set<pair<string, string>> info;

If you want to initialize the set with some initial elements, and you are using C++11, you can do as follows:

set<pair<string,string>> info = {
    {"one","two"},
    {"three","four"},
    {"five","six"}
};

That will initialize the set with 3 pairs of strings. If you are not using C++11, just use set::insert.

Upvotes: 8

Related Questions