どうも、ちょげ(@chogetarou)です。
resize()
でVectorの末尾からN個の要素を削除する方法を紹介します。
方法

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