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 0C ++程序演示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 0Reference: 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中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python insert_Python
- 下一篇: c ++atoi函数_atoi()函数以