C++ 向量::pop_back() 函數
vector::pop_back() 是"vector" 頭文件的庫函數,用於從vector 尾部刪除一個元素,從vector 後麵刪除元素並返回void。
注意:要使用矢量,請包括<vector>
標題。
vector::pop_back() 函數的語法
vector::pop_back();
參數: none
——它什麽都不接受。
返回值: none
——在什麽都沒有回報。
例:
Input: vector<int> v1{10, 20, 30, 40, 50}; //removing elemenets v1.pop_back(); //removes 50 v1.pop_back(); //removes 40 Output: //if we print the values v1:10 20 30
演示vector::pop_back()函數示例的C++程序
//C++ STL program to demonstrate example of
//vector::pop_back() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50 };
//printing elements
cout << "v1:";
for (int x:v1)
cout << x << " ";
cout << endl;
//removing elements
v1.pop_back();
v1.pop_back();
//printing elements
cout << "After removing elements..." << endl;
cout << "v1:";
for (int x:v1)
cout << x << " ";
cout << endl;
return 0;
}
輸出
v1:10 20 30 40 50 After removing elements... v1:10 20 30
參考:C++ 向量::pop_back()
相關用法
- C++ vector::push_back()用法及代碼示例
- C++ vector::push_back()、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::pop_back() function with example in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。