C++中檢查vector是否包含給定元素的幾種方式詳解
概述
在編碼中經(jīng)常會遇到一種場景,就是要在數(shù)組或列表中查找某個元素是否存在,其實對于這種線性操作,自己實現(xiàn)一個循環(huán)來檢查是非常簡單的事情,那既然這樣,為啥還要專門寫篇博客來分享呢?
一個最重要的原因就是我們原本就可以用更簡潔直觀高效的方式去替代手寫for循環(huán),這個方式就是使用C++標準庫函數(shù)。
再啰嗦幾句。
通常在面試的時候,為了考察面試者的編碼功底,會讓其從頭實現(xiàn)某些基礎(chǔ)的算法,但是在實際開發(fā)中,很多東西都有現(xiàn)成的封裝。只有把語言、標準庫“雙劍合璧”才能算是真正的C++。而且據(jù)C++標準委員會的安排,今后C++也會更側(cè)重于擴充庫而不是擴充語言,可見其分量的輕重了。
總之,C++標準庫是一個非常強大的東東,并且實現(xiàn)這些標準庫的人都是些非常牛逼的頂級程序員,性能都是最優(yōu)的。所以不要總想著所有事情都親力親為的去寫一遍,既浪費時間,寫出來的東西可能還很菜。那么,這里就通過一些簡單的示例來演示如何通過標準庫函數(shù)來實現(xiàn)。
正文
如何檢查vector中是否包含給定元素。
std::count
最簡單的方式是對vector中的指定元素進行計數(shù),如果count不為零,表示該元素存在,那么std::count可以很容易實現(xiàn)。
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 1, 20, 2, 6, 3, 7 };
int key = 6;
if (std::count(v.begin(), v.end(), key))
std::cout << "Element found";
else
std::cout << "Element not found";
return 0;
}
std::find
相比第一種方式,std::find()算法能夠更快速的查找給定范圍內(nèi)的值,因為std::count()會變量整個容器以獲得元素計數(shù),而find()在找到匹配元素后就立即停止搜索。
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 1, 20, 2, 6, 3, 7 };
int key = 6;
if (std::find(v.begin(), v.end(), key) != v.end())
std::cout << "Element found";
else
std::cout << "Element not found";
return 0;
}
std::find_if
和find相似的還有一個叫 std::find_if()算法,如果查找需要滿足某些條件,那么推薦使用該方法。這里我們可以結(jié)合lambda來使用,非常簡潔。
比如,要查找列表中是否有元素能被5整除
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
auto lst = {1,4,9,5,11};
if(std::find_if(lst.begin(),lst.end(),[](auto v){
if(v%5 ==0)
return true;
else
return false;
}) != lst.end()){
std::cout << "Element found";
}
else{
std::cout << "Element not found";
}
return 0;
}
C++ 11 - std::any_of
該算法與std::find_if相似,但不是返回滿足條件的的序列中第一個元素的迭代器,而是如果任何元素滿足條件后返回true,否則返回false。
#include <iostream>
#include <vector>
#include <algorithm>
struct compare
{
int key;
compare(int const &i): key(i) { }
bool operator()(int const &i)
{
return (i == key);
}
};
int main()
{
std::vector<int> v = { 4, 7, 5, 2, 6, 9 };
int key = 6;
if (std::any_of(v.begin(), v.end(), compare(key)))
std::cout << "Element found";
else
std::cout << "Element not found";
return 0;
}
或者直接修改find_of的案例:
int main()
{
auto lst = {1,4,9,5,11};
if(std::any_of(lst.begin(),lst.end(),[](int v){
if(v%5 ==0)
return true;
else
return false;
})){
std::cout << "Element found";
}
else{
std::cout << "Element not found";
}
return 0;
}
C++ 11 - std::none_of
該算法剛好和any_of相反,如果給定的條件在范圍內(nèi)所有元素都返回false,則返回true,如果至少一個元素返回true,則返回false。
直接修改上述示例,將if條件改成“非”就可以了,不再贅述。
std::binary_search
最后一種方法,如果vector是有序的,那么可以考慮使用這種算法,如果在給定范圍內(nèi)找到元素,則返回true,否則返回false。該方式是采用二分法查找,時間復雜度為O(log(n)),速度比較快。
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7 };
int key = 4;
if (std::binary_search(v.begin(), v.end(), key))
std::cout << "Element found";
else
std::cout << "Element not found";
return 0;
}
OK,以上所有方法都可以實現(xiàn)我們想要的結(jié)果,只是在不同情況下選擇一種自己喜歡的方式即可。
附:
C++接口文檔:https://en.cppreference.com/w/
C++算法庫介紹:https://en.cppreference.com/w/cpp/algorithm
到此這篇關(guān)于C++中檢查vector是否包含給定元素的幾種方式的文章就介紹到這了,更多相關(guān)C++檢查vector是否包含給定元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt重寫QStackedWidget模擬實現(xiàn)home界面滑動效果
這篇文章主要為大家詳細介紹了Qt如何通過重寫QStackedWidget模擬實現(xiàn)home界面滑動效果,文中的實現(xiàn)過程講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-11-11
opencv4.5.4+VS2022開發(fā)環(huán)境搭建的實現(xiàn)
本文主要介紹了opencv4.5.4+VS2022開發(fā)環(huán)境搭建的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
C語言中字符串常用函數(shù)strcat與strcpy的用法介紹
以下是對C語言中字符串常用函數(shù)strcat與strcpy的使用方法進行了詳細的分析介紹,需要的朋友可以參考下2013-07-07
C++實現(xiàn)LeetCode(156.二叉樹的上下顛倒)
這篇文章主要介紹了C++實現(xiàn)LeetCode(156.二叉樹的上下顛倒),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C/C++讀寫注冊表中二進制數(shù)據(jù)(代碼示例)
這篇文章主要介紹了使用Windows API 函數(shù)中的RegOpenKeyEx()函數(shù)和RegSetValueEx()函數(shù)來實現(xiàn)對注冊表某項寫入二進制鍵值,需要的朋友可以參考下2020-02-02

