Reputation: 2623
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
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