【问题标题】:Vector. Checking to see if it contains "Key". C++ [duplicate]向量。检查它是否包含“密钥”。 C++ [重复]
【发布时间】:2012-11-24 17:36:51
【问题描述】:

可能重复:
How to find an item in a std::vector?

嘿,就像标题暗示的那样,我想检查向量是否包含字符串“Key”。我一直在谷歌上四处寻找,我在矢量库中找不到任何东西。任何人都可以帮我解决这个问题。提前致谢。

【问题讨论】:

  • 这就是我们有算法的原因 :) 它们适用于任何容器(以及更多容器),而不必为每个容器重新实现它们。 en.cppreference.com/w/cpp/algorithm/find
  • “向量?”什么载体?我在这里看不到任何矢量。
  • @chris 很好的例子:)。

标签: c++ vector contain


【解决方案1】:

您可以为此使用std::find。假设你有一个充满std::stringsstd::vector

#include <algorithm> // for std::find

std::vector<std::string> v = ....;
std::vector<std::string>::const_iterator it = std::find(v.begin(), v.end(), "Key");
bool found = it != v.end();

【讨论】:

  • 嘿,我遇到了一个错误。不允许初始化数据成员。
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多