2. 문자열
1. 문자열 비교
2. 문자열 조회
3. 문자열 길이
4. 문자열 대소문자 변환
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string silla = "divided into th Three Kindoms.";
std::string joseon = "Yi Seong-gye, established Joseon in 1392.";
transform(silla.begin(), silla.end(), silla.begin(), (int (*)(int))toupper);
transform(joseon.begin(), joseon.end(), joseon.begin(), (int (*)(int))tolower);
char lower_ch = 'g';
char upper_ch = 'B';
lower_ch = toupper(lower_ch);
upper_ch = tolower(upper_ch);
cout << "문자열 대문자로 변환: " << silla << endl;
cout << "문자열 소문자로 변환: " << joseon << endl;
cout << "문자 대문자로 변환: " << lower_ch << endl;
cout << "문자 소문자로 변환: " << upper_ch << endl;
return 0;
}
5. 문자열 병합
#include <iostream>
#include <string>
using namespace std;
int main() {
string king = "조선 세종";
string favorite1 = "고기";
string favorite2 = "야근";
string king_info = "";
king_info += king;
king_info += "은 ";
king_info += favorite1;
king_info.append("와 ");
king_info.append(favorite2);
king_info.append("을 좋아했습니다.");
cout << king_info << endl;
return 0;
}
6. 문자열 추가
7. 문자열 일부 제거
8. 문자열 이동
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string str1 = "I like coding";
string str2 = move(str1);
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
vector<int> v1 = {1, 2, 3};
vector<int> v2 = move(v1);
cout << "v1 size: " << v1.size() << endl;
cout << "v2 size: " << v2.size() << endl;
return 0;
}
9. 특정 문자 제거
10. 문자열 일부 교체
11. 문자열 정수 변환
12. 문자열 숫자 변환
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss;
double number1 = 0.0;
ss << "1.2,2.6-3.8!4.7=8.9";
cout << "== string to double ==" << endl;
while (!ss.eof()) {
ss >> number1;
ss.ignore();
cout << number1 << ", ";
}
ss.clear();
ss.str("");
ss << "1,"
<< "2" << 3 << " " << 4;
int number2 = 0;
cout << endl
<< "== string to int ==" << endl;
while (!ss.eof()) {
ss >> number2;
ss.ignore();
cout << number2 << ", ";
}
cout << endl;
return 0;
}
13. 문자열 정렬
#include <iostream>
#include <string>
using namespace std;
int main() {
string sort_str1 = "gojoseon";
string sort_str2 = "AaBbCcDdEe";
sort(sort_str1.begin(), sort_str1.end());
sort(sort_str2.begin(), sort_str2.end());
cout << "소문자만 정렬: " << sort_str1 << endl;
cout << "대소문자만 정렬: " << sort_str2 << endl;
return 0;
}
14. 문자열 반전
15. 숫자 문자열 변환
16. 정수와 문자의 최대/최소값
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
auto result1 = min(1, 5);
auto result2 = max('a', 'z');
cout << result1 << ", " << result2 << endl;
auto result3 = minmax({'a', 'n', 'z'});
auto result4 = minmax({1, 2, 3});
cout << result3.first << ", " << result3.second << endl;
cout << result4.first << ", " << result4.second << endl;
return 0;
}