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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:12

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 elementscout << "first element : " << v1.at(0) << endl;cout << "second element: " << v1.at(1) << endl;cout << "last element : " << v1.at(v1.size() - 1) << endl;//accessing all elemenetscout << "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 函數

總結

以上是生活随笔為你收集整理的stl vector 函数_vector :: at()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。