cpp_string - 8BitsCoding/RobotMentor GitHub Wiki
C์ธ์ด์ C++์ string์ฒ๋ฆฌ ์ฐจ์ด์ ์ ๋ฐฐ์๋ณธ๋ค.
- C -
char *
๋ก ๋ฌธ์์ด(string) ์ฒ๋ฆฌ - C++ - 'std::string'์ผ๋ก ๋ฌธ์์ด(string) ์ฒ๋ฆฌ
์ข ๋ ์์ธํ ์์๋ณด์.
C ์๋ ์๋์ ๊ฐ์ ๋ฌธ์ ์ ์ด ์์์.
const int LINE_SIZE = 256;
char line[LINE_SIZE];
cin.getline(line, LINE_SIZE);
- ์๋ฌด๊ฒ๋ ์ฝ์ง ๋ชปํ์ ๋
- ํ ์ค์ ๋ฌธ์๊ฐ 256์ ์ด์์ผ๋(๋ฒํผ๊ฐ ์ถฉ๋ถํ์ง ์์ ๋)
C++ ์ std::string์ ์ด์ฉํ์ฌ ํด๊ฒฐ
#include <string>
std::string firstName;
std::cin >> firstName;
- ์ธ์ ๋ ๊ธธ์ด๊ฐ ์ฆ๊ฐํ ์ ์๋ค.
string A[] = {"1011","0010"};
cout << sizeof(A); // 64 (์ ์ฒด ๋ฌธ์์ด)
cout << sizeof(*A); // 32 (8*4)
int size = sizeof(*A)/sizeof(A);
๊ธธ์ด๋ฅผ ๋ฐํ
C์คํ์ผ(const char*)๋ก string๋ฐํ
// C
char firstName[20] = "POPE"l
char fullName[20];
// ๋์
- ์์ ํ์ง ์๋ค.
strcpy(fullName, firstName);
// ๋ง๋ถ์ด๊ธฐ - ์์ ํ์ง ์๋ค.
strcat(fullName, " KIM");
// C++
string firstName = "POPE";
string fullName = "John Doe";
// ๋์
fullName = firstName;
// ๋ง๋ถ์ด๊ธฐ
fullName += " KIM";
// C
char firstName[20] = "POPE";
char lastName[20] = "KIM";
char fullName[40];
snprintf(fullName, 40, "%s %s", firstName, lastName);
// C++
string firstName = "POPE";
string lastName = "KIM";
string fullName;
fullName = firstName + " " + lastName;
// C
if (strcmp(firstName1, firstName2) == 0)
{
}
if (strcmp(firstName1, firstName2) > 0)
{
}
// C++
if (firstName1 == firstName2)
{
}
if (firstName1 > firstName2)
{
// ์ฌ์ ์์ ์์๋ฅผ ๋น๊ตํ๋ค.
}
-
std::istringstream
- cin๊ณผ ๋น์ท, ํค๋ณด๋๋ฅผ ๋์ string์ผ๋ก ๋ถํฐ ์ฝ์ด์จ๋ค.
- sscanf()์ ๋น์ท(cf. scanf๋ ์ฝ์๋ก๋ถํฐ ์ฝ์ด์ค๊ณ sscanf๋ string์ผ๋ก ๋ถํฐ ์ฝ์ด์จ๋ค.)
-
std::ostringstream
- cout๊ณผ ๋น์ท, ์ฝ์ ๋์ string์ ์ถ๋ ฅํ๋ค.
- sprintf์ ๋น์ท
-
cout, cin๋ ์คํธ๋ฆผ
๋ฌธ์ ๋ฐฐ์ด ๊ธธ์ด์ ๊ดํด ๊ณ ๋ฏผํ ํ์๊ฐ ์๋ค -> ์ด๋ป๊ฒ ๋์๊ฐ๋์ง๋ฅผ ํ์ธํด๋ณด์.
๊ฐ๋จํ๊ฒ ์ ๋ฆฌํ์๋ฉด : ํ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ ๋น๋๊ณ (์ถ๊ฐ์ ์ผ๋ก ํ ๋น์ ์ํด์) ํฌ์ธํฐ๋ก ์ฒซ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋๋ค.(์ฐธ๊ณ ๋ก c_str()์ ์์๋ฉ๋ชจ๋ฆฌ ์ฃผ์(const char*)๋ฅผ ๋๊ฒจ์ฃผ๋ ํจ์์ด๋ค.)
- ํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ์๋๊ฐ ๋๋ฆฌ๋ค.
- ๋ฉ๋ชจ๋ฆฌ ๋จํธํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค.
- ๋ด๋ถ ๋ฒํผ์ ์ฆ๊ฐ๋ ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ์์ ํ์ง ์์ ์ ์๋ค.
- ์ด๋ค ๋ถ์ผ์์ ์ธ ๊ฒ์ธ๊ฐ๋ฅผ ๊ณ ๋ฏผํด์ ์ ํํด์ผํ๋ค.
- ๋ฐ๋ผ์ sprintf์ ํจ๊ป char[]๋ฅผ ๋งค์ฐ ๋ง์ด ์ฌ์ฉํ๋ค. -> ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ๋ฌธ์ ์์ ์ ๋ฆฌํ๋ค.
// include header
#include <string>
// ์์ฑ์ 1
string myString = "abcd";
// ์์ฑ์ 2
string mySTring;
mySTring = "abcd";
// ํ์ฅ, ๋ฌธ์์ด ์ถ๊ฐ1
string base = "hello world!";
base += "x";
// ํ์ฅ, ๋ฌธ์์ด ์ถ๊ฐ2
string base = "hellow world!";
base.append("appended!");
string src = "I am source :";
string dst;
dst = src;
string base = "hello world!";
base.length(); // 13
base.size(); // 13
length์ size๋ ๊ฐ์ ๊ธฐ๋ฅ์ ํ๋ค.
string base = "hello world!";
base.capacity(); // ํด๋น string์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ถ๋ ฅ, ๋ง์ฝ ๋ฌธ์์ด์ด ๋์ด๋ ๊ฒฝ์ฐ ํด๋น capacity๋ณด๋ค ํด ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ ํ ๋นํ๊ฒ ๋๋ค.
string base = "hello world!";
base.max_size(); // ์ต๋ ํ ๋น๊ฐ๋ฅํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ด์ฆ ๋ฆฌํด
string base = "hello world!";
base.at(0); // h
base.at(1); // e
string base = "hello world!";
base.find("world!");
if(base.find("world!)!=string::npos) {
// world!๋ผ๋ ๋ฌธ์์ด์ ์ฐพ์์ ๊ฒฝ์ฐ
}
์ถ๊ฐ) ๋ฌธ์์ด์ด ์์ ํ ๋์ผํ์ง ์์๋ ๊ฒ์์ด ๊ฐ๋ฅํจ.
string base = "hello world!"; if(base.find("world!")!=string::npos) { printf("find world!\n"); } if(base.find("wor")!=string::npos) { printf("find wor\n"); } return 0;
string a = "I am string one!");
string b = "string"
if(a.compare(b)==0) {
// ๋ ๋ฌธ์์ด์ด ๊ฐ์
}
else if(a.compare(b)<0) {
// a๊ฐ b๋ณด๋ค ์ฌ์ ์์ผ๋ก ์์ผ ๋
}
else if(a.compare(b)>0) {
// a๊ฐ b๋ณด๋ค ์ฌ์ ์์ผ๋ก ๋ค์ผ ๋
}
์ถ๊ฐ์ ์ธ ์ฌํญ์ ์ฐธ๊ณ ์ฌ์ดํธ๋ฅผ ํ์ธํ์
char *cStr = "C++";
std::string Str = std::string(cStr);