C++競プロ学習日記(仮)

( 学習記録であり解説Blogではないです )

vector<pair<type, type>>の要素削除の仕方

#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(n); i++)
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int main()
{
    int N;
    cin >> N;
    
    vector<pair<int, string>> P;
    REP( i, N )
    {
        string S;
        cin >> S;
        P.emplace_back( i + 1, S );
    }
    REP( i, (int)P.size() )
    {
        if( P[i].first % 2 != 0 )
        {
            //要素を削除
            P.erase( P.begin() + i );
        }
        cout << P[i].first << " " << P[i].second << endl;
    }
    return 0;
}

pair要素の削除の仕方で時間を溶かしたのでメモ。
pairという事に囚われていたのですが、pairと言ってもvectorなので
削除の仕方はvectorと同じでした...(それはそう)
( P[ i ].first と P[ i ].second をそれぞれ erase すると何故か思っていた...)

上記コードの実行結果: [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

1)vector::erasevector::erase - cpprefjp C++日本語リファレンス
2)std::vectorvector - cpprefjp C++日本語リファレンス
3)std::pairpair - cpprefjp C++日本語リファレンス