初始化C++字符串向量的向量
Initialize a vector of vector of strings in C++
我正在尝试在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
}
^
任何帮助将不胜感激。谢谢!
另一种方式:
#include <string>
#include <vector>
int main()
{
std::vector<std::vector<std::string>> foo(2);
}
将名称放在初始化器列表中:
#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
};
}
相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>