본문 바로가기
Leaning/C++

[Hackerrank] Vector-Erase

by ksw8596 2024. 11. 4.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    cin >> n;
    int position, start, end;
    vector<int> v(n, 0);
    for(int i = 0; i < n; i++)
    {
        cin >> v[i];
    }
    cin >> position;
    v.erase(v.begin()+(position-1));
    cin >> start >> end;
    v.erase(v.begin()+(start-1), v.begin()+(end-1));
    cout << v.size() << endl;
    for(int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    return 0;
}

erase 쓰는방식에 대해 알아보았다.

 

v.erase(v.begin()+지울위치값) -> 한 위치값을 지움

v.erase(v.begin()+start위치, v.begin()+end위치) -> 범위안의 값을 지움

 

'Leaning > C++' 카테고리의 다른 글

[프로그래머스] 9번/ 지폐접기  (0) 2024.11.21
[Hackerrank] Vector-Sort  (0) 2024.11.04
DFS(깊이 우선 탐색), BFS(너비 우선 탐색)  (0) 2024.10.25
C++ 자료구조(STL)  (0) 2024.10.24
[HackerRank] Hotel Prices  (0) 2024.10.17