在没有开始和结束的向量中循环

Iterating through a vector without begin and end

本文关键字:向量 循环 结束 开始      更新时间:2023-10-16

我有一个关键字向量,我需要迭代它。

我的尝试:

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