stl vector 函數_vector :: at()函數以及C ++ STL中的示例

stl vector 函數

C ++ vector :: at()函數 (C++ vector::at() function)

vector::at() is a library function of "vector" header, it is used to access an element from specified position, it accepts a position/index and returns the reference to the element at specified position/index.

vector :: at()“ vector”頭文件的庫函數,用于從指定位置訪問元素,它接受位置/索引并返回對指定位置/索引處的元素的引用。

Note: To use vector, include <vector> header.

注意:要使用向量,請包含<vector>標頭。

Syntax of vector::at() function

vector :: at()函數的語法

    vector::at(size_type n);

Parameter(s): void – It is a position of an element to be accessed.

參數: void –這是要訪問的元素的位置。

Return value: reference – It returns a reference to the element at position n.

返回值: reference –返回對位置n處元素的引用。

Example:

例:

    Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
cout << vector1.at(0) << endl;
cout << vector1.at(1) << endl;
Output:
1
2

C ++程序演示vector :: at()函數的示例 (C++ program to demonstrate example of vector::at() function)

//C++ STL program to demonstrate example of
//vector::at() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1{ 10, 20, 30, 40, 50 };
//accessing elements
cout << "first element : " << v1.at(0) << endl;
cout << "second element: " << v1.at(1) << endl;
cout << "last element  : " << v1.at(v1.size() - 1) << endl;
//accessing all elemenets
cout << "all elements of vector v1..." << endl;
for (int i = 0; i < v1.size(); i++)
cout << "element at index " << i << " : " << v1.at(i) << endl;
return 0;
}

Output

輸出量

first element : 10
second element: 20
last element  : 50
all elements of vector v1...
element at index 0 : 10
element at index 1 : 20
element at index 2 : 30
element at index 3 : 40
element at index 4 : 50

Reference: C++ vector::at()

參考: C ++ vector :: at()

翻譯自: https://www.includehelp.com/stl/vector-at-function-with-example.aspx

stl vector 函數

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

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

相關文章

敏捷開發“松結對編程”系列之七:問題集之一

本文是“松結對編程”系列的第七篇。&#xff08;之一&#xff0c;之二&#xff0c;之三&#xff0c;之四&#xff0c;之五&#xff0c;之六&#xff0c;之七&#xff0c;之八&#xff09;剛剛參加完MPD 2011深圳站&#xff0c;在演講中間及后來媒體采訪&#xff0c;被問到了一…

powerdesigner 導出數據庫表結構

http://www.360doc.com/content/12/0817/19/61497_230730771.shtml轉載于:https://www.cnblogs.com/gaohuag/p/3169095.html

C++中的sort函數對二維數組排序是按照什么準則?

遇到的一個疑惑&#xff0c;現記錄如下&#xff1a; int main() {vector<vector<int>> envelopes { {5, 8},{6, 7},{6, 4},{2, 3},{8,9} };sort(envelopes.begin(), envelopes.end());for (int i 0;i < envelopes.size();i)cout << envelopes[i][0]<…

Exercises

I. Faulty sentences 1&#xff0c;Our host entertained us with many interesting stories of adventure, he has been a member of an exploration team working in the Arctic. 翻譯&#xff1a;我們的主持人用許多有趣的冒險故事來娛樂我們&#xff0c;他是北極探險團隊…

數學專業學計算機哪一行,計算數學

計算數學(一個理科專業)語音編輯鎖定討論上傳視頻計算數學是由數學、物理學、計算機科學、運籌學與控制科學等學科交叉滲透而形成的一個理科專業。中文名計算數學外文名Computational Mathematics所 屬數學計算數學專業定義編輯語音計算數學也叫做數值計算方法或數值分析。主…

數論之數字根 杭電1013

做這道題就有一種感覺&#xff0c;&#xff0c;數學真是奇妙&#xff0c;&#xff0c;在網上查了一下&#xff0c;才知道數字根有那么多奇妙的性質。不過&#xff0c;對于這道題我卻是不太理解&#xff0c;&#xff0c;主要是不會證明為什么數字根就是各個位加起來對9取余&…

ubuntu12.10下安裝mysqlworkbench出現“Dependency is not satisfiable: libctemplate0”問題的解決方案...

(原文地址&#xff1a;http://www.cnblogs.com/Deasel-s-magic-box/p/3169790.html) 之前在window下面一直用navicat&#xff0c;轉到ubuntu下之后&#xff0c;雖然也找到一個navicat的linux版&#xff0c;但是經常各種莫名其妙的掛掉&#xff0c;而且界面實在是挫的1B 。 所以…

圖片透視變換操作

由于照相機硬件設備本身的誤差&#xff0c;可能會導致鏡頭畸變&#xff0c;從而導致照相機拍攝到的照片產生失真現象&#xff0c;此時可以通過透視變換去適當的校正。 大概的思路&#xff1a;在原圖像上確定四個點&#xff0c;然后再新圖像上也確定四個點&#xff0c;通過warp…

dp筆記:關于DP算法和滾動數組優化的思考

從網上總結了一些dp的套路以及對滾動數組的一些思考&#xff0c;現記錄如下&#xff0c;希望以后回顧此類算法時會有所幫助。 目錄1、DP算法經驗1、DP算法核心&#xff1a;2、DP算法類別以及例題例1&#xff1a;三步問題例2&#xff1a;最小路徑和例3&#xff1a;乘積最大子數組…

高職單招面試自我介紹稿子計算機專業,單招面試自我介紹稿子范文

每年很多參加高職單招的同學筆試不錯&#xff0c;卻在面試環節上失敗了。單招面試需要技巧&#xff0c;需要考生細心準備&#xff0c;以自信樂觀的態度全面對單招面試。下面是小編整理的單招面試自我介紹范文及技巧&#xff0c;歡迎閱讀。1單招面試自我介紹范文各位老師好&…

as_hash ruby_Ruby中帶有示例的Hash.delete_if方法

as_hash rubyHash.delete_if方法 (Hash.delete_if Method) In this article, we will study about Hash.delete_if Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this meth…

java學習筆記十二——多態

滿足多態的基本條件1、要有繼承2、要有重寫3、父類引用指向子類對象/** 多態例子 */ //定義游戲抽象類abstract class gameObject { String gameName; abstract String getGameName();}//紅警游戲class redAlert extends gameObject { String gameName "red Ale…

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解決方案

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解決方案 NoClassDefFoundErrorLogFactorySpringHibernate Spring3.1啟動時報錯&#xff1a;Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogF…

【C++ grammar】引用

1、引用就是另一個變量的別名 2、通過引用所做的讀寫操作實際上是作用與原變量上 引用方式&#xff1a; int x; int & rxx; or int x, &rxx;在C中&是取地址&#xff0c;在C中&放在一個變量的定義前&#xff0c;那就是引用 注意&#xff1a; 這種引用是錯誤的…

網絡安全 攻擊類型_網絡攻擊的類型| 網絡安全

網絡安全 攻擊類型Nowadays Cyber Security is an essential part of our life. We are store, secure and use data and security is important to prevent to steal data. There are numbers of attacks done by the attackers for ex: Man in middle attacks, Brute force, …

flash安全策略的理解

flash安全策略的理解 2011-06-25 01:48 11人閱讀 評論(0) 收藏 舉報 一直以來對flash的安全策略是一頭霧水&#xff0c;什么安全沙箱&#xff0c;跨域策略文件一堆東西亂七八糟&#xff0c;搞不清楚。不過糾結到現在已經基本上理解了。 flash的安全問題在官方手冊上有足夠的解…

【C++ grammar】nullptr and Dynamic Memory Allocation (空指針和動態內存分配)

空指針 1.1. 0帶來的二義性問題 C03中&#xff0c;空指針使用“0”來表示。0既是一個常量整數&#xff0c;也是一個常量空指針。C語言中&#xff0c;空指針使用(void *)0來表示有時候&#xff0c;用“NULL”來表示空指針(一種可能的實現方式是#define NULL 0) 1.2. C標準化委…

yum update Transaction Check Error

update系統時&#xff0c;發現其中一臺server居然提示&#xff1a; Transaction Check Error:file /usr/lib/perl5/5.8.8/CGI.pm from install of perl-5.8.8-32.el5_7.6.x86_64 conflicts with file from package perl-5.8.8-32.el5_6.3.i386file /usr/lib/perl5/5.8.8/Digest…

No module named ‘skimage.metrics‘在Anaconda3中的解決方法

1&#xff0c;進入Anaconda Prompt 2&#xff0c;進行安裝&#xff1a; pip install scikit-image 3&#xff0c;若還是報錯&#xff0c;嘗試進行更新一下 pip install scikit-image --upgrade

array_push_PHP array_push()函數與示例

array_pushPHP array_push()函數 (PHP array_push() function) array_push() function is used to insert/push one or more than one element to the array. array_push()函數用于將一個或多個元素插入/推入數組。 Syntax: 句法&#xff1a; array_push(array, elemement1, …