프로그래밍/C++
cygwin 의 g++ 로 utf-8 문자열 거꾸로하기
Scripter
2014. 4. 10. 13:04
locale을 적용하지 않은 경우:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
std::getline(std::cin, s);
std::reverse(s.begin(), s.end()); // modifies s
std::cout << s << std::endl;
return 0;
}
#include <string>
#include <algorithm>
int main()
{
std::string s;
std::getline(std::cin, s);
std::reverse(s.begin(), s.end()); // modifies s
std::cout << s << std::endl;
return 0;
}
컴파일:
$ g++ -o testReverseUTF8_003 testReverseUTF8_003.cpp
실행:
$ ./testReverseUTF8_003
안녕하세요?
?▒▒츄옕핅눕▒
locale을 적용하고 wstring, wcout, wcin을 사용한 경우:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::setlocale(LC_ALL, "ko_KR.UTF-8");
std::wstring s;
std::getline(std::wcin, s);
std::reverse(s.begin(), s.end()); // modifies s
std::wcout << s << std::endl;
return 0;
}
#include <string>
#include <algorithm>
int main()
{
std::setlocale(LC_ALL, "ko_KR.UTF-8");
std::wstring s;
std::getline(std::wcin, s);
std::reverse(s.begin(), s.end()); // modifies s
std::wcout << s << std::endl;
return 0;
}
컴파일:
$ g++ -o testReverseUTF8_003 testReverseUTF8_003.cpp
실행:
$ ./testReverseUTF8_003
안녕하세요?
?요세하녕안