どうも、ちょげ(@chogetarou)です。
resize()
でVectorの最後の要素を削除する方法を紹介します。
方法

resize()
でVectorの末尾の要素を削除するには、size()を使います。
まず、Vectorからresize()を呼び出します。
そして、resize()の引数に、Vectorのsize()を「ー1」した値を指定します。
//vect=任意のVector
vect.resize(vect.size() - 1);
上記のresize()は、Vectorの最後の要素を削除します。
使用例
#include <iostream>
#include <vector>
using namespace std;
int main(void){
vector<int> num = { 1, 2, 3, 4, 5 };
num.resize(num.size() - 1);
for (int &i: num) {
cout << i << endl;
}
}
出力:
1
2
3
4
コメント