C++ 向量::push_back() 函數
vector::push_back() 是 "vector" 頭文件的庫函數,用於在向量的末尾插入/添加一個元素,它接受一個相同類型的元素並在向量的末尾添加給定的元素並增加向量的大小。
注意:要使用矢量,請包括<vector>
標題。
vector::push_back() 函數的語法
vector::push_back(value_type n);
參數: n
– 是要添加到向量末尾的元素。
返回值: void
——在什麽都沒有回報。
例:
Input: vector<int> v1; v1.push_back(20); v1.push_back(30); v1.push_back(40); v1.push_back(50); Output: //if we print the values v1:20 30 40 50
演示vector::push_back()函數示例的C++程序
//C++ STL program to demonstrate example of
//vector::push_back() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1;
//inserting elements and printing size
cout << "size of v1:" << v1.size() << endl;
v1.push_back(10);
cout << "size of v1:" << v1.size() << endl;
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
cout << "size of v1:" << v1.size() << endl;
//printing all elements
cout << "elements of vector v1..." << endl;
for (int x:v1)
cout << x << " ";
cout << endl;
return 0;
}
輸出
size of v1:0 size of v1:1 size of v1:5 elements of vector v1... 10 20 30 40 50
參考:C++ 向量::push_back()
相關用法
- C++ vector::push_back()、vector::pop_back()用法及代碼示例
- C++ vector::pop_back()用法及代碼示例
- C++ vector::max_size()用法及代碼示例
- C++ vector::rbegin()用法及代碼示例
- C++ vector::crend()用法及代碼示例
- C++ vector::emplace_back用法及代碼示例
- C++ vector::at()用法及代碼示例
- C++ vector::swap()用法及代碼示例
- C++ vector::shrink_to_fit()用法及代碼示例
- C++ vector::cbegin()用法及代碼示例
- C++ vector::back()用法及代碼示例
- C++ vector::assign()用法及代碼示例
- C++ vector::begin()用法及代碼示例
- C++ vector::cend()用法及代碼示例
- C++ vector::operator[]用法及代碼示例
- C++ vector::clear()用法及代碼示例
- C++ vector::cbegin()、vector::cend()用法及代碼示例
- C++ vector::empty()用法及代碼示例
- C++ vector::front()、vector::back()用法及代碼示例
- C++ vector::at()、vector::swap()用法及代碼示例
注:本文由純淨天空篩選整理自 vector::push_back() function with example in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。