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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時間:2025/3/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl resize函数_vector :: resize()函数以及C ++ STL中的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

stl resize函數

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

vector::resize() is a library function of "vector" header, it is used to resize the vector, it accepts the updated number of elements and a default value (optional) and resizes the vector container.

vector :: resize()是“ vector”標頭的庫函數,用于調整矢量的大小,它接受更新的元素數量和默認值(可選),并調整矢量容器的大小。

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

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

Syntax of vector::resize() function

vector :: resize()函數的語法

vector::resize();

Parameter(s): n – is the updated size, val – is the default value to be assigned to the new size, and value_type() – it is the value type of the container (a reference of the type of the first template parameter).

參數: n-是更新的大小, val-是要分配給新大小的默認值, value_type() -它是容器的值類型(第一個模板參數類型的引用) )。

Return value: void – It returns nothing.

返回值: void –不返回任何內容。

Example:

例:

Input:vector<int> vector1{ 1, 2, 3, 4, 5 };Function call:cout << vector1.resize(10);Output://if we print elements1 2 3 4 5 0 0 0 0 0

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

//C++ STL program to demonstrate example of //vector::resize() function#include <iostream> #include <vector> using namespace std;int main() {vector<int> v1;//printing the size of the vectorcout << "Total number of elements: " << v1.size() << endl;//pushing elementsv1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);//printing the size of the vectorcout << "Total number of elements: " << v1.size() << endl;//printing the elementscout << "vector elements are: ";for (int x : v1)cout << x << " ";cout << endl;//resizing the size with default value//and printing the elementsv1.resize(8, 99);//printing the size of the vectorcout << "Total number of elements: " << v1.size() << endl;//printing the elementscout << "vector elements are: ";for (int x : v1)cout << x << " ";cout << endl;//resizing the size without default value//and printing the elementsv1.resize(10);//printing the size of the vectorcout << "Total number of elements: " << v1.size() << endl;//printing the elementscout << "vector elements are: ";for (int x : v1)cout << x << " ";cout << endl;return 0; }

Output

輸出量

Total number of elements: 0 Total number of elements: 5 vector elements are: 10 20 30 40 50 Total number of elements: 8 vector elements are: 10 20 30 40 50 99 99 99 Total number of elements: 10 vector elements are: 10 20 30 40 50 99 99 99 0 0

Reference: C++ vector::resize()

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

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

stl resize函數

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現金大獎

總結

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

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