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::erase :vector::erase - cpprefjp C++日本語リファレンス
2)std::vector:vector - cpprefjp C++日本語リファレンス
3)std::pair:pair - cpprefjp C++日本語リファレンス