根据字符串向量的大小运行循环

xbo*_*nez 1 c++ vector

我做了一个字符串向量

vector<string> actor_;
Run Code Online (Sandbox Code Playgroud)

然后使用push_back在其中添加元素.

我现在想要显示所有这些,我需要根据向量中的元素数运行循环.为此,我需要运行以下循环:

for (int i = 0; i < (int)actor_.size; i++)
{
}
Run Code Online (Sandbox Code Playgroud)

但是这会返回以下错误:


error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::vector<_Ty>::* )(void) const' to 'int'
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>        There is no context in which this conversion is possible
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 8

size是一个成员函数; 你的意思是:

for (unsigned int i = 0; i < actor_.size(); i++) { }
Run Code Online (Sandbox Code Playgroud)

(这是一个好主意,std::size_t而不是使用unsigned int)

  • @James:只是为了澄清,他从来没有开始使用`unsigned int`.(也许我正在阅读它.)@ xbonez:`size_t`是用于测量某事物大小的标准积分类型,并且很可能比`unsigned`更大.`size()`返回一个`size_t`,所以你应该使用`size_t`来匹配. (2认同)