'C++' std::erase

토스트·2025년 4월 28일

'C++' std::vector

목록 보기
2/6

erase

iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

: 단일 요소(position) 또는 요소 범위([first, last))를 제거하고 그 다음 위치를 반환합니다.

<example>

#include <iostream>
#include <vector>

using namespace std;

int main() {
	vector<int> vec = { 1, 2, 3, 4, 5, 6, 7 };

	vec.erase(vec.begin() + 2); // 3 제거

	for (const int& i : vec) {
		cout << i << ' ';
	}

	cout << endl;

	vec.erase(vec.begin() + 1, vec.begin() + 4); // 2, 4, 5 제거

	for (const int& i : vec) {
		cout << i << ' ';
	}

	return 0;
}

결과값

0개의 댓글