小编Hen*_*erg的帖子

C++在向量中找到

我试图做的功能将返回在向量中发现事件的postition.但我的返回值始终为0,我确信有匹配.

这是代码:

int findInItemvector(vector<Item> vec, string name)
{
    for(vector<Item>::iterator it = vec.begin(); it < vec.end(); it++)
    {
        if(it->getName() == name)
        {
            return it - vec.begin(); 
            break;
        }
        else
        {
            return 0;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl vector find

0
推荐指数
2
解决办法
2092
查看次数

标签 统计

c++ ×1

find ×1

iterator ×1

stl ×1

vector ×1