๐ String
๐ String(๋ฌธ์์ด)
- ๋ฌธ์์ด์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋ฐ์ดํฐํ์
- ๊ธฐ๋ณธ์ ์ผ๋ก ๋์ ํฌ๊ธฐ ๋ฅผ ๊ฐ์ง๋ ๋ฌธ์์ด์ ์ฒ๋ฆฌ๊ฐ๋ฅ
๐ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, world!"; // ๋ฌธ์์ด ์ ์ธ ๋ฐ ์ด๊ธฐํ
cout << str << endl; // ์ถ๋ ฅ: "Hello, world!"
return 0;
}
- ํค๋ ํ์ผ ์กด์ฌ
- using namespace std; ๋ฅผ ํตํด std ์๋ตํจ
- โ โ ํฐ ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธ
๐ ์ฃผ์ ๊ธฐ๋ฅ
- ๋ฌธ์์ด์ ๊ธธ์ด : size(), length() ๋ ๋ค ๋์ผํ๊ฒ ์๋
string str = "Hello, world!";
cout << "Length: " << str.length() << endl; // ๊ธธ์ด ์ถ๋ ฅ: 13
cout << "Size: " << str.size() << endl; // ํฌ๊ธฐ ์ถ๋ ฅ: 13
- ๋ฌธ์ ์ ๊ทผ : ๋ฐฐ์ด์ฒ๋ผ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉ์ฌ์ฌ ์ ๊ทผ ๊ฐ๋ฅ
string str = "Hello";
cout << str[0] << endl; // 'H' ์ถ๋ ฅ
cout << str[4] << endl; // 'o' ์ถ๋ ฅ
- ๋ฌธ์์ด ๊ฒฐํฉ : โ+โ ๋ฅผ ํตํด ๊ฒฐํฉ ๊ฐ๋ฅ
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2; // ๋ฌธ์์ด ๊ฒฐํฉ
cout << result << endl; // "Hello World" ์ถ๋ ฅ
- ๋ฌธ์์ด ๋น๊ต : โ=โ ์ ํตํด ํ๋ณ ๊ฐ๋ฅ
string str1 = "apple";
string str2 = "banana";
if (str1 == str2) {
cout << "Equal" << endl;
} else {
cout << "Not Equal" << endl; // "Not Equal" ์ถ๋ ฅ
}
- ๋ฌธ์์ด ์๋ฅด๊ธฐ(substring) : substr() ํจ์๋ ์ฃผ์ด์ง ์์ ์์น์ ๊ธธ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ถ ๋ฌธ์์ด ์ถ์ถ
string str = "Hello, world!";
string sub = str.substr(7, 5); // "world"
cout << sub << endl; // "world" ์ถ๋ ฅ
substr(start, length) ์ ํํ๋ก, start๋ ์์ ์ธ๋ฑ์ค, length๋ ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธธ์ด์ด๋ค.
- ๋ฌธ์์ด ๊ฒ์(find) : find()ํจ์๋ ๋ฌธ์์ด ๋ด์์ ํน์ ๋ฌธ์๋ ๋ถ๋ถ ๋ฌธ์์ด์ ์ฐพ์ ์ ์๋ค
string str = "Hello, world!";
size_t pos = str.find("world"); // "world"์ ์์น ์ฐพ๊ธฐ
if (pos != string::npos) {
cout << "'world' found at index " << pos << endl; // 7 ์ถ๋ ฅ
}
find()ํจ์๋ ์ฐพ์ผ๋ฉด ์ฒ์ ์์ ์ธ๋ฑ์ค๋ฅผ ์ถ๋ ฅํ๊ณ ์ฐพ์ง ๋ชปํ๋ฉด npos๋ฅผ ๋ฐํํ๋ค.(hash table์์๋ npos ๋์ end()๋ฅผ ์ฌ์ฉํ๋ค.)
- ๋ฌธ์์ด ์ญ์ (erase) : erase() ํจ์๋ ๋ถ๋ถ ๋ฌธ์์ด ์ญ์ ๊ธฐ๋ฅ
string str = "Hello, world!";
str.erase(5, 7); // 5๋ฒ ์ธ๋ฑ์ค๋ถํฐ 7๊ฐ์ ๋ฌธ์๋ฅผ ์ญ์
cout << str << endl; // "Hello" ์ถ๋ ฅ
erase(start, length) ์ ํํ๋ก start๋ ์์ ์ธ๋ฑ์ค, length๋ start๋ถํฐ ์ญ์ ํ ๊ธธ์ด์ด๋ค.
- ๋ฌธ์์ด ์ฝ์ (insert) : insert() ํจ์๋ ๋ฌธ์์ด์ ํน์ ์์น์ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฝ์ ํ๋ค.
string str = "Hello";
str.insert(5, ", world!"); // 5๋ฒ ์ธ๋ฑ์ค์ ", world!" ์ฝ์
cout << str << endl; // "Hello, world!" ์ถ๋ ฅ
insert(start, โstringโ) ์ ํํ๋ก, start๋ ์ฝ์ ํ ์์ ์์น๋ก string์ ์ฝ์ ํ๋ค.
- ๋ฌธ์์ด ์นํ(replace) : replace() ํจ์๋ ํน์ ๋ถ๋ถ ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฌธ์์ด๋ก ๊ต์ฒดํ๋ ๊ธฐ๋ฅ
string str = "Hello, world!";
str.replace(7, 5, "C++"); // "world"๋ฅผ "C++"๋ก ๊ต์ฒด
cout << str << endl; // "Hello, C++!" ์ถ๋ ฅ
replace(start, length, โstringโ) ์ ํํ๋ก, start๋ ๊ต์ฒดํ ์์ ์ธ๋ฑ์ค, length๋ start๋ถํฐ ๊ต์ฒดํ ๊ธธ์ด, string์ ๊ทธ ์์น์ ๋ฃ์
๐ก ๋ฌธ์์ด ๊ด๋ จํจ์
- c_str() : std::string์ C ์คํ์ผ ๋ฌธ์์ด๋ก ๋ณํ
string str = "Hello";
const char* cstr = str.c_str();
cout << cstr << endl; // "Hello" ์ถ๋ ฅ
- clear() : ๋ฌธ์์ด์ ๋น ๋ฌธ์์ด๋ก ์ด๊ธฐํ
string str = "Hello";
str.clear();
cout << str << endl; // ๋น ๋ฌธ์์ด ์ถ๋ ฅ
- resize() : ๋ฌธ์์ด์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ
string str = "Hello";
str.resize(3); // "Hel"
cout << str << endl;
- swap() : ๋ ๋ฌธ์์ด์ ๋ด์ฉ์ ์๋ก ๊ตํ
string str1 = "Hello";
string str2 = "World";
str1.swap(str2);
cout << str1 << " " << str2 << endl; // "World Hello"
๐ C++11 ์ดํ์ ๋ฌธ์์ด ๊ธฐ๋ฅ
- std::to_string() : ์ซ์ ํ์ ์ ๋ฌธ์์ด๋ก ๋ณํ ๊ฐ๋ฅ
- std::stoi(), std::stol(), std::stof() : ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํ ๊ฐ๋ฅ
๐ ์ ๋ฆฌ
- std::string์ ๋์ ํฌ๊ธฐ๋ฅผ ์ง์ํ๋ ๋ฌธ์์ด ํ์
- ๋ฌธ์์ด์ ๋น๊ต, ๊ฒฐํฉ, ๊ฒ์, ์๋ฅด๊ธฐ, ์ฝ์ , ์ญ์ ๋ฑ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ๋ฌธ์์ด ์กฐ์ ๊ฐ๋ฅ
๐ก ์ถ๊ฐ
isdigit() ํจ์(03-08 ์ถ๊ฐ)
- isdigit()๋ ๋ฌธ์ c๊ฐ 0~9 ์ฌ์ด์ ์ซ์์ผ ๋๋ง true๋ฅผ ๋ฐํํ๋ฉฐ, ๋ค๋ฅธ ๋ฌธ์๋ false๋ฅผ ๋ฐํํ๋ค.
- ์๋ฅผ ๋ค์ด, โaโ, โ โ, โ!โ ๋ฑ์ ๋ชจ๋ ์ซ์๊ฐ ์๋๋ฏ๋ก false๋ฅผ ๋ฐํํ๋ค.