#include <iostream> #include <list> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; while(N--) { list<char> L; list<char>::iterator cursor=L.begin(); string S; cin >> S; for(auto a : S) { switch(a) { case '-': { if(cursor != L.begin()) { cursor = L.erase(--cursor); } break; } case '<': { if(cursor != L.begin()) cursor--; break; } case '>': { if(cursor != L.end()) cursor++; break; } default : { L.insert(cursor,a); break; } } } for(auto a : L) cout << a ; cout <<'\n'; } }
- cursor = L.begin(); -> L.insert(cursor, 3) 하게되면
it는 L.end()와 같은 위치에 있다.- cursor = L.erase(cursor)
삭제 후 cursor의 값을 바꿔주지 않으면 cursor는 삭제된 값을 가지고 있다! 왜그러냐;- list.end()는 마지막 요소 다음을 가리킨다!(빈 곳)