在没有开始和结束的向量中循环
Iterating through a vector without begin and end
我有一个关键字向量,我需要迭代它。
我的尝试:
bool isKeyword(string s)
{
return find(keywords, keywords + 10, s ) != keywords + 10;
}
然而,这适用于数组,但不适用于向量。如何更改+10以遍历向量?我需要这个,因为我不能使用结束和开始,因为我没有C++11支持。
上面代码的错误:
error: no matching function for call to 'find(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >::size_type, std::string&)'|
像这样使用begin()
和end()
:
find(keywords.begin(), keywords.end(), s )
这里有一个例子:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // std::find
using namespace std;
bool isKeyword(string& s, std::vector<string>& keywords)
{
return (find(keywords.begin(), keywords.end(), s ) != keywords.end());
}
int main()
{
vector<string> v;
string s = "Stackoverflow";
v.push_back(s);
if(isKeyword(s, v))
cout << "foundn";
else
cout << "not foundn";
return 0;
}
正如其他人所说,此应用程序不需要C++11
。
参考std::find
。
相关文章:
- C++:如何循环通过向量中的整数元素
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- <string> 使用 for 循环写入向量
- C++ 制作/命名不初始化的向量(在 while 循环中)
- C++ - 使用基于范围的 for 循环将字符值分配给向量中的字符串不会分配值
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 布尔值向量的基于范围 for 循环
- C++用于循环跳过向量的元素?
- 修改向量中的字符串?(不使用循环)
- 从列表/向量制作嵌套 for 循环的最佳方法是什么?
- 特征获取索引数组,其中向量中的值为真(不需要循环)
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 向量循环抛出2个错误
- 使用向量循环时,是否有错误
- EXC_BAD_INSTRUCTION对于C++中的向量循环为反向
- 向量循环的启动值
- 通过列表向量循环
- 使用指针向量循环其他向量
- 在自包含向量循环中删除时"矢量迭代器不兼容"