我正在尝试在C ++中创建字符串向量的向量,但我没有得到;这就是我的代码:
#include <vector>
#include <string>
int main() {
std::vector<std::vector<std::string>> foo =
{
std::vector<std::string> ex,
std::vector<std::string> bar,
};
}
当尝试使用GCC进行编译时,会给出以下输出:
example.cpp: In function ‘int main()’:
example.cpp:6:28: error: expected primary-expression before ‘ex’
std::vector<std::string> ex,
^~
example.cpp:6:28: error: expected ‘}’ before ‘ex’
example.cpp:5:2: note: to match this ‘{’
{
^
example.cpp:6:28: error: could not convert ‘{<expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >’
std::vector<std::string> ex,
^~
example.cpp: At global scope:
example.cpp:9:1: error: expected declaration before ‘}’ token
}
^
任何帮助将不胜感激。谢谢!
答案 0 :(得分:3)
另一种方式:
#include <string>
#include <vector>
int main()
{
std::vector<std::vector<std::string>> foo(2);
}
答案 1 :(得分:1)
将名称拖放到初始化程序列表中
#include <vector>
#include <string>
int main() {
std::vector<std::vector<std::string>> foo =
{
std::vector<std::string>{}, // <- no name + added curly braces
std::vector<std::string>{} // <- no comma
};
}