2012-12-06 35 views
1

可能重複:
How to find an item in a std::vector?矢量。檢查它是否包含「鑰匙」。 C++

嘿就像標題所暗示我想檢查一下,如果向量包含字符串「密鑰」。我一直在谷歌環顧四周,我無法找到矢量庫中的任何東西。任何人都可以幫助我解決這個問題。提前致謝。

+5

這就是爲什麼我們的算法:)他們任何容器(及以上)的工作,而不是重新實現他們的每一個。 http://en.cppreference.com/w/cpp/algorithm/find – chris

+0

「矢量?」什麼矢量?我在這裏沒有看到任何矢量。 –

+0

@chris很好的例子:)。 – Pendo826

回答

4

您可以使用std::find。假設你有一個std::vector充滿std::strings

#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(); 
+0

嘿,我得到一個錯誤。數據成員初始化是不允許的。 – Pendo826