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

stl向量

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

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

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

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

Syntax:

句法:

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

Parameters:

參數:

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

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

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

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

  • 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.

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

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 vector
vector<int> v1{ 10, 20, 30, 40, 50 };
// input element
cout << "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 : 3
Second 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向量

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542532.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542532.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542532.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java socket如何請求485協議_javaSE第十五部分 網絡編程(1)Socket和ServerSocket

網絡編程基礎知識C/S結構&#xff1a;全稱為Client/Server結構&#xff0c;是指客戶端和服務器結構。常見程序有&#xff31;&#xff31;、迅雷等軟件。B/S結構&#xff1a;全稱為Browser/Server結構&#xff0c;是指瀏覽器和服務器結構。常見瀏覽器有谷歌、火狐等。兩種架構各…

【分享】linux下u盤使用

2019獨角獸企業重金招聘Python工程師標準>>> linux下u盤使用 方案一&#xff1a; Linux不像Windows一樣&#xff0c;接上新硬件后可以自動識別&#xff0c;在Linux下無法自動識別新硬件的&#xff0c;需要手動去識別。USB移動存儲設備通常被識別為sda1&#xff0c;…

kotlin中判斷字符串_Kotlin程序刪除字符串中所有出現的字符

kotlin中判斷字符串Given a string and a character, we have to remove all occurrences of the character in given string. 給定一個字符串和一個字符&#xff0c;我們必須刪除給定字符串中所有出現的字符。 Example: 例&#xff1a; Input:string "includeHelp Del…

Java9中使用jpa,jpa – eclipselink在Java 9上使用final字段進行靜態編織

我有一些JPA注釋字段,如下所示&#xff1a;Column(name "SOME_FIELD", updatable false, nullable false)private final String someField;當實體插入數據庫時??,這些字段存儲在數據庫中.它們無法進一步更新.對于Java編程語言,可以將這些字段視為final.使用Ecli…

python語言程序設計及醫學應用_Python語言程序設計(高等學校計算機專業規劃教材)...

第1章Python語言概述/1 1.1Python語言的發展1 1.1.1Python的起源1 1.1.2Python的發展2 1.2Python語言的特點2 1.2.1Python的特性2 1.2.2Python的缺點4 1.2.3Python與其他語言的比較5 1.3簡單的Python程序介紹5 1.4Python的程序開發工具8 1.4.1Python的版本選擇8 1.4.2Python的安…

swift 3.0 中使用 xib

文章寫于2016年9月底&#xff0c;Xcode 8&#xff0c;swift 3.0真是蛋疼&#xff0c;折騰了很長時間&#xff0c;試了網上很多教程&#xff0c;結果又莫名的可以了&#xff01; 1.方法和OC中一樣 將一個xib文件和一個ViewController類進行關聯的幾步操作&#xff1a; command &…

數字圖像處理圖像反轉的實現_使用8086微處理器反轉16位數字

數字圖像處理圖像反轉的實現Problem statement: 問題陳述&#xff1a; Write an assembly language program in 8086 microprocessor to reverse 16 bit number using 8 bits operation. 在8086微處理器中編寫匯編語言程序&#xff0c;以使用8位操作反轉16位數字。 Example: …

php猴子找大王算法,教程方法;php實現猴子選大王問題算法實例電腦技巧-琪琪詞資源網...

琪琪詞資源網-教程方法;php實現猴子選大王問題算法實例電腦技巧&#xff0c;以下是給大家帶來的教程方法;php實現猴子選大王問題算法實例&#xff0c;大家可以了解一下哦!下面為你介紹php實現猴子選大王問題算法實例。本文實例講述了php實現猴子選大王問題算法。分享給大家供大…

numpy 歸一化_NumPy 數據歸一化、可視化

僅使用 NumPy&#xff0c;下載數據&#xff0c;歸一化&#xff0c;使用 seaborn 展示數據分布。下載數據import numpy as npurl https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.datawid np.genfromtxt(url, delimiter,, dtypefloat, usecols[1])僅提取…

java虛擬機規范閱讀(三)異常

Java虛擬機里面的異常使用Throwable或其子類的實例來表示&#xff0c;拋異常的本質實際上是程序控制權的一種即時的、非局部&#xff08;Nonlocal&#xff09;的轉換——從異常拋出的地方轉換至處理異常的地方。絕大多數的異常的產生都是由于當前線程執行的某個操作所導致的&am…

puppeteer api_使用Node.js和puppeteer API從URL創建PDF文件

puppeteer apiWe will continue using Node.js and puppeteer which is a node library. As we saw in our last article, Puppeteer is a Node library developed by Google and provides a high-level API for developers. 我們將繼續使用Node.js和puppeteer(這是一個節點庫)…

python線程同步鎖_[python] 線程間同步之Lock RLock

為什么需要同步 同樣舉之前的例子&#xff0c;兩個線程分別對同一個全局變量進行加減&#xff0c;得不到預期結果&#xff0c;代碼如下&#xff1a; total 0 def add(): global total for i in range(1000000): total 1 def desc(): global total for i in range(1000000): t…

servlet的由來

2019獨角獸企業重金招聘Python工程師標準>>> 動靜態網頁技術 首先說下訪問網頁的大概過程&#xff1a; 你在瀏覽器中輸入網址&#xff0c;按下enter鍵&#xff0c;此時瀏覽器代你做了很多事&#xff0c;簡要說為&#xff1a;將你輸入的這個網址作為目的地參數&#…

php header 文件大小,php獲取遠程文件大小及信息的函數(head_php

php獲取遠程文件大小及信息的函數(header頭信息獲取)阿里西西Alixixi.com開發團隊在做一個客戶系統時&#xff0c;需要做遠程下載的功能&#xff0c;并實時顯示進度條效果。所以&#xff0c;需要預先讀取遠程文件的大小信息&#xff0c;然后做為實時下載進度條的參數。功能函數…

Java ObjectInputStream readUnsignedShort()方法(帶示例)

ObjectInputStream類readUnsignedShort()方法 (ObjectInputStream Class readUnsignedShort() method) readUnsignedShort() method is available in java.io package. readUnsignedShort()方法在java.io包中可用。 readUnsignedShort() method is used to read 2 bytes (i.e. …

python中info的用法_Python pandas.DataFrame.info函數方法的使用

DataFrame.info(self, verboseNone, bufNone, max_colsNone, memory_usageNone, null_countsNone) [source] 打印DataFrame的簡要摘要。 此方法顯示有關DataFrame的信息&#xff0c;包括索引dtype和列dtype&#xff0c;非空值和內存使用情況。 參數&#xff1a;verbose &#x…

第四次作業 孫保平034 李路平029

用C編寫一個學生成績管理系統 1、可以實現以下功能&#xff1a; cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 1.增加學生成績 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl; 2、用鏈表存儲信息 * 程序頭部的注釋結束 3、約定的規范&#xff1a; 1界面設計簡介&#xff0c;人性化…

php serialize error at offset,PHP Notice: unserialize(): Error at offset XX of XX bytes

之前同事在本地開發的時候&#xff0c;出現一個錯誤&#xff0c;如下圖所示&#xff1a;字面意思就是反序列化錯誤&#xff0c;由此bug引申出來序列化和反序列化得應用&#xff0c;以及php array當key為string類型的數字值時&#xff0c;會發生什么情形。先來看序列化$str [1 …

Java ClassLoader setClassAssertionStatus()方法與示例

ClassLoader類setClassAssertionStatus()方法 (ClassLoader Class setClassAssertionStatus() method) setClassAssertionStatus() method is available in java.lang package. setClassAssertionStatus()方法在java.lang包中可用。 setClassAssertionStatus() method is used …

python怎么變各種顏色_python – 如何淡化顏色

有很多方法可以做到這一點.您如何選擇這取決于您是否重視速度和簡單性或感知均勻性.如果你需要它是真正統一的,你需要用顏色配置文件定義RGB顏色,你需要配置文件的原色,這樣你就可以轉換為XYZ,然后轉換到LAB,你可以操作L通道. 大多數情況下,您不需要這樣做,而是可以使用像Photo…