1406번: editor
#include <iostream>
#include <list>
#include <string>
using namespace std;
#define endl '\n'
list<char> Set()
{
list<char> editor;
string sentence = "";
cin >> sentence;
for (const auto& ch : sentence)
editor.emplace_back(ch);
return editor;
}
bool IsPCommand(char InCommand) // P 명령 판별
{
return InCommand == 'P' ? true : false;
}
void ExcuteEtcCommand(list<char>& Editor, const char& InCommand, list<char>::iterator& InCursor)
{
switch (InCommand)
{
case 'L':
{
if (InCursor != Editor.begin()) // 커서가 에디터의 시작점아니면
InCursor--;
break;
} // case L
case 'D':
{
if (InCursor != Editor.end()) // 커서가 에디터의 끝점이 아니면
InCursor++;
break;
} // case D
case 'B':
{
if (InCursor != Editor.begin()) // 커서가 시작점이 아니면
InCursor = Editor.erase(--InCursor); // 커서를 앞으로 이동 시키고 해당 문자 지우기
// 참고 ( list return value )
// https://learn.microsoft.com/ko-kr/cpp/standard-library/list-class?view=msvc-170
break;
} // case B
}
}
void ExcuteCommand(list<char>& Editor)
{
auto cursor = Editor.end(); // 커서 위치 지정
size_t times = 0;
cin >> times;
char command, ch;
for (size_t i = 0; i < times; ++i)
{
cin >> command;
if (IsPCommand(command)) // P 명령 판별
{
cin >> ch;
Editor.insert(cursor, ch);
continue;
}
ExcuteEtcCommand(Editor, command, cursor);
}
}
int main()
{
list<char> editor = Set();
ExcuteCommand(editor);
for (const auto& ch : editor)
cout << ch;
cout << endl;
return 0;
}
✔ 느낀 점
STL list의 erase 부분을 너무 당연하게 앞의 부분을 지울거라 생각해서 애먹었던 문제
그거만 아니면 쉽게 풀 수 있는 문제라 생각한다. 다음엔 실수 없이 풀 수 있도록 기억해두자!!