2014-02-14 44 views
0

如何通過矢量搜索特定的字符串?那搜索會返回什麼?在矢量中搜索字符串?

我設置以下名稱作爲搜索的用戶輸入。

find(user_list.begin(), user_list.end(), name) 

我不知道如何實現這個到一個布爾函數,因爲我不知道什麼值查找將返回。

功能是

bool BBoard::user_exists(const string &name, const string &pass) const{} 

,我得到了很多的錯誤,現在它是如何不能比的。

+1

嗯,這就是[文件](HTTP:// en.cppreference.com/w/cpp/algorithm/find)。 – jrok

+0

_「我不知道find會返回什麼值」_ - 可以通過引用[documentation](http://en.cppreference.com/w/cpp/algorithm/find)輕鬆解決。 –

+0

std :: find返回迭代器:std :: vector :: iterator,則if(res_itr!= user_list.end()){std :: cout << * res_itr; } – marcinj

回答

2

find函數返回一個迭代器,該迭代器指向您的user_list中匹配的第一個條目。你不要張貼了大量的代碼,但如果你的代碼如下所示:

#include <vector> 
#include <string> 
#include <algorithm> 

std::vector<std::string> user_list; 

然後你可以使用find如下:

std::vector<std::string>::iterator i = find(user_list.begin(), 
    user_list.end(), name) 
if (i == user_list.end()) { 
    // Not found 
} else { 
    // Found, *i is your string 
}