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

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