int nCount = std::count(strVec.begin(), strVec.end(), target);
if (nCount > 0)
{
std::cout << "method2: find " << target << " exists." << std::endl;
}

#include <iostream>
#include <vector>
#include <string>

// 为了便于示例,声明全局容器
std::vector<std::string> strVec;

void methods(const std::string& target)
{
// 方法一:遍历容器,查找相等元素判断是否存在
{
for (const auto& item : strVec)
{
if (item == target)
{
std::cout << "method1: find " << target << " exists." << std::endl;
break;
}
}
}
// 方法二:获取元素个数,通过个数判断是否存在
{
int nCount = std::count(strVec.begin(), strVec.end(), target);
if (nCount > 0)
{
std::cout << "method2: find " << target << " exists." << std::endl;
}
}
// 方法三:查询元素迭代器,通过迭代器有效性判断是否存在
{
auto iter = std::find(strVec.begin(), strVec.end(), target);
if (iter != strVec.end())
{
std::cout << "method3: find " << target << " exists." << std::endl;
}
}
// 方法四:查询相等元素的迭代器,通过迭代器有效性判断是否存在
{
auto iter = std::find_if(strVec.begin(), strVec.end(), [&](const std::string& item)->bool
{ return (item == target); });
if (iter != strVec.end())
{
std::cout << "method4: find " << target << " exists." << std::endl;
}
}
}

int main()
{
strVec = { "C", "C++", "Java", "Python", "Lua", "Sql" };
// 场景1:查找Ruby
std::cout << "Find Ruby" << std::endl;
methods("Ruby");
// 场景2:查找C++
std::cout << "Find C++" << std::endl;
methods("C++");

system("pause");
return 0;
}

// result
/*
Find Ruby
Find C++
method1: find C++ exists.
method2: find C++ exists.
method3: find C++ exists.
method4: find C++ exists.
*/