C++ string
string는 C++ 표준 라이브러리로 STL에 포함되지 않는다.
string는 문자만을 원소로 저장하고 문자열을 조작할 목적으로 사용되는 컨테이너이다.
string를 사용하기 위해서는 <stirng> 헤더를 인클루드 해야한다.
C/C++ 문자열처럼 마지막에 '\0'을 포함해야하는 요구사항은 없다.
생성자 |
|
string s |
기본 생성자로 s 생성 |
string s(str) |
str 문자열로 s 생성 |
string s(str, n) |
str 문자열에서 n개의 문자로 s를 생성 |
string s(n, c) |
n개의 c문자로 s를 생성 |
string s(iter1, iter2) |
반복자 구간 [iter1, iter2)의 문자로 s를 생성 |
string s(p1, p2) |
포인터 구간 [p1, p2)의 문자로 s를 생성 |
생성자 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> #include <string> int main() { std::string t("Hello!"); const char* p1 = "Hello!"; const char* p2 = p1 + 6; std::string s1; std::string s2("Hello!"); std::string s3("Hello!", 5); std::string s4(5, 'H'); std::string s5(t.begin(), t.end()); std::string s6(p1, p2); std::cout << "s1()\t\t: " << s1 << '\n'; std::cout << "s2(str)\t\t: " << s2 << '\n'; std::cout << "s3(str, n)\t: " << s3 << '\n'; std::cout << "s4(n, c)\t: " << s4 << '\n'; std::cout << "s5(iter1, iter2): " << s5 << '\n'; std::cout << "s6(p1, p2)\t: " << s6 << '\n'; return 0; } | cs |
생성자 예제 실행 결과
멤버함수
append(), +=, push_back() 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <string> int main() { std::string s1("Hello"); std::string s2("Hello"); std::string s3("Hello"); std::string s4("Hello"); std::string s5("Hello"); std::string s6("Hello"); std::string s7("Hello"); std::string s8("Hello"); std::string s9("Hello"); std::string s10("Hello"); std::string t(" string!"); const char* p1 = " string!"; const char* p2 = p1 + 8; s1.append(t);//s1 + t s2.append(t, 0, 4);//s2 + (t문자열에서 0부터 4번째 인덱스 까지 추가) s3.append(" string!"); s4.append(" string!", 5);//주어진 문자열에서 5개 문자 추가 s5.append(t.begin(), t.end()); s6.append(p1, p2); s7.append(5, 's');//주어진 문자 c를 n번 추가 s8 += t; s9 += " string!"; for (std::string::iterator it = t.begin(); it != t.end(); it++) s10.push_back((*it)); std::cout << "s1.append(s)\t\t:" << s1 << '\n'; std::cout << "s2.append(s, off, n)\t:" << s2 << '\n'; std::cout << "s3.append(str)\t\t:" << s3 << '\n'; std::cout << "s4.append(str, n)\t:" << s4 << '\n'; std::cout << "s5.append(iter1, iter2)\t:" << s5 << '\n'; std::cout << "s6.append(p1, p2)\t:" << s6 << '\n'; std::cout << "s7.append(n, c)\t\t:" << s7 << '\n'; std::cout << "s8+=s\t\t\t:" << s8 << '\n'; std::cout << "s9+=str\t\t\t:" << s9 << '\n'; std::cout << "s10.push_back(c)\t:" << s10 << '\n'; return 0; } | cs |
실행 결과
멤버함수
append(), = 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <string> int main() { std::string s1; std::string s2; std::string s3; std::string s4; std::string s5; std::string s6; std::string s7; std::string s8; std::string s9; std::string t("Hello string!"); const char* p1 = "Hello string!"; const char* p2 = p1 + 13; s1.assign(t);//s1 + t s2.assign(t, 0, 5);//s2 + (t문자열에서 0부터 5번째 인덱스 까지 할당) s3.assign("Hello"); s4.assign("Hello!", 5);//주어진 문자열에서 5개 문자 추가 s5.assign(t.begin(), t.end()); s6.assign(p1, p2); s7.assign(5, 'H');//주어진 문자 c를 n번 추가 s8 = t; s9 = "Hello string!"; std::cout << "s1.append(s)\t\t:" << s1 << '\n'; std::cout << "s2.append(s, off, n)\t:" << s2 << '\n'; std::cout << "s3.append(str)\t\t:" << s3 << '\n'; std::cout << "s4.append(str, n)\t:" << s4 << '\n'; std::cout << "s5.append(iter1, iter2)\t:" << s5 << '\n'; std::cout << "s6.append(p1, p2)\t:" << s6 << '\n'; std::cout << "s7.append(n, c)\t\t:" << s7 << '\n'; std::cout << "s8+=s\t\t\t:" << s8 << '\n'; std::cout << "s9+=str\t\t\t:" << s9 << '\n'; return 0; } | cs |
멤버 함수
c_str(), data() 사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <string> int main() { std::string s("Hello!"); const char *str; const char *buf; str = s.c_str(); buf = s.data(); std::cout << "'\\0' 문자로 끝나는 문자열: " << str << '\n'; std::cout << "'\\0' 문자를 포함하지 않는 문자열 배열: "; for (int i = 0; i < s.size(); i++) std::cout << buf[i]; std::cout << '\n'; return 0; } | cs |
실행 결과
scanf/printf를 사용할 때, c_str()을 사용하면 string 문자열을 출력할 수 있다.
멤버 함수
문자열 비교 compare() 사용 예제
s1 > s2 : 1
s1 < s2 : -1
s1 == s2 : 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> #include <string> int main() { std::string s1("ABCDEF"); std::string s2("CBAABC"); const char* s3 = "CBAABC"; std::cout << "s1.compare(s2) : " << s1.compare(s2) << '\n'; // ABCDEF < CBAABC std::cout << "s1.compare(off, n, s2) : " << s1.compare(3, 3, s2) << '\n'; //ABCDEF > ABC std::cout << "s1.compare(off1, n1, s1, off2, n2) : " << s1.compare(0, 3, s2, 3, 3) << '\n'; // ABC == ABC std::cout << "s1.compare(s3) : " << s1.compare(s3) << '\n'; // ABCDEF < CBAABC std::cout << "s1.compare(off, n, s3) : " << s1.compare(3, 3, s3) << '\n'; //ABCDEF > ABC std::cout << "s1.compare(off1, n1, s3, n2) : " << s1.compare(2, 1, s3, 1) << '\n'; //C == C return 0; } | cs |
실행 결과
문자열 비교 연산(!=, ==, >, <, >=, <=)는 전체 문자열을 비교하지만 compare()는 부분 문자열을 비교할 수 있는 장점이 있다.
멤버 함수
copy()
문자열 복사가 가능하다. copy()는 마지막에 '\0'을 추가하지 않으므로 별도로 넣어줘야 한다.
부분 복사도 가능하다.
사용 예제
1 2 3 4 5 | s.copy(buf, s.length()); buf[s.length()] = '\-'; s.copy(buf, 4, 2);//복사할 위치, 개수, 오프셋 buf[4] = '0'; | cs |
멤버 함수
find()
문자열에서 해당 문자/문자열의 위치를 반환. 없을 경우 -1 반환
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> #include <string> int main() { std::string str("There are two needles in this haystack with needles."); std::string str2("needle"); // different member versions of find in the same order as above: std::size_t found = str.find(str2); if (found != std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; found = str.find("needles are small", found + 1, 6); if (found != std::string::npos) std::cout << "second 'needle' found at: " << found << '\n'; found = str.find("haystack"); if (found != std::string::npos) std::cout << "'haystack' also found at: " << found << '\n'; found = str.find('.'); if (found != std::string::npos) std::cout << "Period found at: " << found << '\n'; // let's replace the first needle: str.replace(str.find(str2), str2.length(), "preposition"); std::cout << str << '\n'; return 0; } | cs |
멤버 함수
insert()
문자열에서 원하는 위치에 원하는 문자열 추가
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <string> int main() { std::string str = "to be question"; std::string str2 = "the "; std::string str3 = "or not to be"; std::string::iterator it; // used in the same order as described above: str.insert(6, str2); // to be (the )question str.insert(6, str3, 3, 4); // to be (not )the question str.insert(10, "that is cool", 8); // to be not (that is )the question str.insert(10, "to be "); // to be not (to be )that is the question str.insert(15, 1, ':'); // to be not to be(:) that is the question it = str.insert(str.begin() + 5, ','); // to be(,) not to be: that is the question str.insert(str.end(), 3, '.'); // to be, not to be: that is the question(...) str.insert(it + 2, str3.begin(), str3.begin() + 3); // (or ) std::cout << str << '\n'; return 0; } | cs |
멤버 함수
replace()
원하는 위치의 문자열을 교체
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <string> int main() { std::string base = "this is a test string."; std::string str2 = "n example"; std::string str3 = "sample phrase"; std::string str4 = "useful."; // replace signatures used in the same order as described above: // Using positions: 0123456789*123456789*12345 std::string str = base; // "this is a test string." str.replace(9, 5, str2); // "this is an example string." (1) str.replace(19, 6, str3, 7, 6); // "this is an example phrase." (2) str.replace(8, 10, "just a"); // "this is just a phrase." (3) str.replace(8, 6, "a shorty", 7); // "this is a short phrase." (4) str.replace(22, 1, 3, '!'); // "this is a short phrase!!!" (5) // Using iterators: 0123456789*123456789* str.replace(str.begin(), str.end() - 3, str3); // "sample phrase!!!" (1) str.replace(str.begin(), str.begin() + 6, "replace"); // "replace phrase!!!" (3) str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7); // "replace is cool!!!" (4) str.replace(str.begin() + 12, str.end() - 4, 4, 'o'); // "replace is cooool!!!" (5) str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end());// "replace is useful." (6) std::cout << str << '\n'; return 0; } | cs |
멤버 함수
substr()
문자열의 부분 문자열을 구하는 함수
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> #include <string> int main () { std::string str="We think in generalities, but we live in details."; // (quoting Alfred N. Whitehead) std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find("live"); // position of "live" in str std::string str3 = str.substr (pos); // get from "live" to the end std::cout << str2 << ' ' << str3 << '\n'; return 0; } | cs |
멤버 함수
erase()
문자열을 지우는 함수
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> #include <string> int main () { std::string str ("This is an example sentence."); std::cout << str << '\n'; // "This is an example sentence." str.erase (10,8); // ^^^^^^^^ std::cout << str << '\n'; // "This is an sentence." str.erase (str.begin()+9); // ^ std::cout << str << '\n'; // "This is a sentence." str.erase (str.begin()+5, str.end()-9); // ^^^^^ std::cout << str << '\n'; // "This sentence." return 0; } | cs |
참조: http://www.cplusplus.com/reference/string/string/
참고 문헌: 뇌를 자극하는 C++ STL
'컴퓨터공학 > STL' 카테고리의 다른 글
[STL] C++ STL deque (0) | 2018.07.19 |
---|---|
[STL] C++ STL vector (0) | 2018.07.19 |
[STL]C++ STL priority_queue (0) | 2018.07.19 |
[STL]C++ STL queue (0) | 2018.07.19 |
[STL]C++ STL stack (0) | 2018.07.19 |