fo_x86
fo_x86

Reputation: 2623

How to build std::vector<std::string> from std::initializer_list<char const*>

What is the correct way to build an std::vector<std::string> from an std::initializer_list<char const*>? I can iterate through the initializer list and manually build up the vector as such:

std::initializer_list<char const*> values;
std::vector<std::string> vec;
for(auto v : values) {
    vec.push_back(v);
}

Seems quite verbose. Suggestions?

Upvotes: 1

Views: 163

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597215

std::vector has a constructor that accepts an iterator range as input, as long as the dereferenced values are convertible to the vector's value_type:

std::vector<std::string> vec(values.begin(), values.end());

Upvotes: 1

Related Questions