如何在 cpp 中获取特定向量行的大小
How do you get the size of a specific vector row in cpp?
我不知道为什么,但由于某种原因它不起作用。有没有其他方法可以轻松获取行的大小,这个似乎效率不高。
vector< vector<string> >::iterator row;
vector<string>::iterator col;
for (row = vector.begin(); row != vector.end(); row++)
{
std::ostringstream value;
value << vector[*row].size(); //error
cout << "The length is" << value << endl;
value.str("");
value.clear();
}
错误:与"运算符 []"不匹配(操作数类型为 "std::vector>>"和 'std::vector>') val <<vec[*row].size(); ^
你是你以错误的方式迭代器.. 顺便说一下,更改:
value << vector[*row].size();
跟:
value << *row.size();
/* or value << row->size(); */
关于你的问题,看看这个例子:
typedef vector<vector<string>> Matrix;
/* specific index */
const int index = 3;
Matrix matrix;
Matrix::iterator row = matrix.begin() + index;
cout << row->size();
将
迭代器视为一种通用指针。 一般规则是,如果您的迭代器it
vector<X>::iterator
,则*it
是一个X&
。
正如您在程序中所写的那样,row
的类型是 vector<vector<string>>::iterator
. 这意味着*row
是一个vector<string>&
,你可以调用它的size()
函数:通过(*row).size()
或更简单的row->size()
。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 不同大小的向量数组的大小 cpp
- yaml-cpp到std::向量迭代的怪异行为
- 如何在 CPP 中使用 int 迭代器打印数组/列表的向量?
- 如何将对象的向量从类返回到主.cpp
- 动态增长的向量,在 cpp 中声明为静态
- 在cpp中声明向量数组
- 如何在 cpp 中获取特定向量行的大小
- 每当我输入长度为4的字符串时,下面的cpp代码就会崩溃(向量中的malloc失败)
- cpp多维向量
- 尝试访问向量时出现分段错误 - cpp
- ndk中的标准与cpp向量问题
- 循环终止条件错误的 cpp 向量
- C 向量排序 .h .cpp中的单独文件
- C++11 initializer_list构造函数,带有自定义向量类的头和cpp文件
- 包含不同大小的向量的结构,cpp
- cpp中的向量到非向量赋值
- cpp 向量中的内存何时解除分配