日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

stl向量_如何检查C ++ STL中向量中是否存在元素?

發(fā)布時(shí)間:2025/3/11 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl向量_如何检查C ++ STL中向量中是否存在元素? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

stl向量

Given a vector and an element to be searched in the vector.

給定一個(gè)向量和要在向量中搜索的元素。

To check whether an elements exists in a vector or not – we use find() function. find() function takes 3 arguments.

要檢查向量中是否存在元素 –我們使用find()函數(shù) 。 find()函數(shù)帶有3個(gè)參數(shù)。

Syntax:

句法:

find(InputIterator first, InputIterator last, const T& element);

Parameters:

參數(shù):

  • InputIterator first – an iterator pointing to the first elements (or elements from where we have to start the searching).

    InputIterator first –指向第一個(gè)元素(或我們必須從其開(kāi)始搜索的元素)的迭代器。

  • InputIterator last – an iterator pointing to the last elements (or elements till then we have to find the element).

    InputIterator last –指向最后一個(gè)元素(或直到現(xiàn)在我們必須找到該元素的元素)的迭代器。

  • element – and elements of same type to be searched.

    element –和要搜索的相同類型的元素。

If the element exists – it returns an iterator to the first element in the range that compares equal to element (elements to be searched). If the element does not exist, the function returns last.

如果元素存在–它將迭代器返回到與元素 (等于要搜索的元素)相等的范圍內(nèi)的第一個(gè)元素。 如果該元素不存在,則該函數(shù)最后返回。

C ++ STL程序,用于檢查向量中是否存在給定元素 (C++ STL program to check whether given element exists in the vector or not)

// C++ STL program to check whether given // element exists in the vector or not #include <iostream> #include <vector> // for vectors #include <algorithm> // for find() using namespace std;int main() {int element; //element to be searched// Initializing a vectorvector<int> v1{ 10, 20, 30, 40, 50 };// input elementcout << "Enter an element to search: ";cin >> element;vector<int>::iterator it = find(v1.begin(), v1.end(), element);if (it != v1.end()) {cout << "Element " << element << " found at position : ";cout << it - v1.begin() + 1 << endl;}else {cout << "Element " << element << " does not found" << endl;}return 0; }

Output

輸出量

First run: Enter an element to search: 30 Element 30 found at position : 3Second run: Enter an element to search: 60 Element 60 does not found

翻譯自: https://www.includehelp.com/stl/check-whether-an-element-exists-in-a-vector-in-cpp-stl.aspx

stl向量

總結(jié)

以上是生活随笔為你收集整理的stl向量_如何检查C ++ STL中向量中是否存在元素?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。