Reputation: 113
I want to initialise a list of lists (as in Python) in C++. Suppose the list is: [['a','b'],['c','d']]
New to C++, and mostly worked in Python so not sure what to do.
std::vector<vector<string>> dp {{'a',b'}};
I have tried this but doesn't seem to work.
no matching function for call to 'std::vector<std::vector<std::__cxx11::basic_string<char> > >::vector(<brace-enclosed initializer list>)'|
Upvotes: 0
Views: 2723
Reputation: 118097
It looks similar in C++, but string literals should be surrounded by "
not '
(which is for character literals).
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::vector<std::string>> dp{{"hello", "world"},
{"goodbye", "for", "now"}};
for(const auto& v : dp) {
for(const std::string& s : v) {
std::cout << s << " ";
}
std::cout << "\n";
}
}
Output:
hello world
goodbye for now
Upvotes: 11
Reputation: 3983
Are you after something like this?
std::vector<std::vector<std::string>> dp = {
{"a", "b"},
{"c", "d"},
{"e","f"}
};
Upvotes: 0