C++ 在没有开始和结束的向量中迭代

C++ 在没有开始和结束的向量中迭代,c++,vector,C++,Vector,我有一个关键字向量,我需要遍历它 我的尝试: 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<

我有一个关键字向量,我需要遍历它

我的尝试:

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&)'|
错误:调用“find(std::vector&,std::vector::size\u 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 << "found\n";
    else
        cout << "not found\n";
    return 0;
}
#包括
#包括
#包括

#包括.

关键字.begin()
。你不需要C++11来实现这一点。为什么是+10?@dlf,大概当前数组有10个元素。无论如何,如果你没有C++11,你可以很容易地制作你自己的免费版本的
begin
end
。您会发现
begin()
end()
以及相应的迭代器在C++11发布之前很久就已经得到了支持。@chris您的评论意味着
begin
end
需要
C++11
。我想这不是你想要的。@G.Samaras,我的评论意味着
begin
end
的免费版本需要C++11。你推断的意思不是我的意思。为什么在不需要复制的情况下按值传递
字符串s
?我只是复制粘贴了OP的原型。很好,我会编辑@C.R。进一步挑剔,引用应该是
常量
,否则你将无法传递临时值。