C++ ๋ฌธ์์ด ์๋ฅด๊ธฐ
์๋ก
C++ String ํด๋์ค๋ Python์ split ํจ์์ฒ๋ผ ๊ฐ๋จํ๊ฒ ๋ฌธ์์ด์ ์๋ฅด๋(ํ ํฌ๋์ด์ง) ํจ์๊ฐ ์๋ค.
C++์์ ๋ฌธ์์ด์ ์๋ฅด๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด์
strtok ํจ์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ (#include <string.h> ํค๋ ํ์)
char* strtok(char* str, char* delimiters);
strtok ํจ์๋ ์ ๋ ฅ๋ฐ์ char* ํ์ ์ ๋ฌธ์์ด์ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋์ฉ ์๋ผ ํฌ์ธํฐ๋ก ๋ฐํํ๋ ํจ์์ด๋ค.
๋ฌธ์์ด์ ๋๋๋ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. string์ char* ๋ฌธ์์ด ํํ๋ก ๋ณํํ๊ณ
2. char* ๋ฌธ์์ด์ strtok ํจ์๋ก ํ๋์ฉ ์๋ผ๋ธ๋ค
3. ์๋ผ๋ธ char* ๋ฌธ์์ด์ ์ฉ๋์ ๋ง๊ฒ ์ฌ์ฉํ๋ค.
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main() {
string s;
char s1[100] = "";
getline(cin, s);
for (int i = 0; i < (int)s.size(); i++) {
s1[i] = s[i];
}
// strtok ํจ์์์ string์ ์ฌ์ฉ๋ถ๊ฐ
char* ptr = strtok(s1, " ");
while (ptr != NULL) {
// ์๋ฅธ ๋ฌธ์์ด์ string์ ๋ฃ๊ณ ์ถ์ผ๋ฉด string(ptr) ํํ๋ก ์จ์ผํจ
cout << ptr << endl;
//strtok๋ ์ผ์นํ๋๊ฒ ์์ผ๋ฉด NULL ๋ฐํ
ptr = strtok(NULL, " ");
// ๋ค์ ๋ฌธ์์ด ์์์ฃผ์๋ ํจ์์ static ๋ณ์์ ๊ธฐ์ต๋์ด ์๋ค
// ๋ง์ฝ ๋ถ๋ฆฌํ์ง ๋ชปํ์ ๊ฒฝ์ฐ NULL์ด ๊ธฐ์ต๋๋ฉฐ NULL์ ๋ถ๋ฆฌํ๋ฉด NULL์ด ๋ฐํ
}
return 0;
}
strtok ํจ์์ ์ฒซ๋ฒ์งธ ์ธ์์ char* ๋ฌธ์์ด ๋ฃ์ผ๋ฉด ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ์๋ผ๋ด์ด return ํ๋ค.
strtok ํจ์์ ์ฒซ๋ฒ์งธ ์ธ์์ NULL์ ๋ฃ์ผ๋ฉด ์ด์ ์ ์ฐพ์ ๊ตฌ๋ถ์ ๋ค์์ ๋ฌธ์์ด์ ์๋ผ๋ด์ด return ํ๋ค.
๋ ์ด์ ์๋ฅผ ๋ฌธ์์ด์ด ์์ผ๋ฉด NULL์ return ํ๋ค.
์ด๋ ๊ตฌ๋ถ์๋ ์ฌ๋ฌ ์ข ๋ฅ๋ฅผ ๋ฃ์ ์ ์๋ค. (๊ตฌ๋ถ์๊ฐ " ,;-" ์ผ ๊ฒฝ์ฐ ๊ณต๋ฐฑ + ',' + ';' + '-' ๋ฅผ ๋ชจ๋ ๊ตฌ๋ถ์๋ก ๊ฐ์ง)
string์ char ๋ฐฐ์ด๋ก ์ฝ๊ฒ ๋ณ๊ฒฝํ ์ ์๊ฒ strcpy ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค. (#include <ctsring> ํค๋ ํ์)
char* strcpy(char* destination, const char* source);
strtok_s ํจ์๋ strtok ํจ์์ ์ ์ฌํ์ง๋ง ์ธ ๋ฒ์งธ ์ธ์์ ์๋ฅด๊ณ ๋จ์ ๋ฌธ์์ด์ ์ ์ฅํ๊ณ ์๋ค.
char* strtok_s(char* str, const char* delimeters, char** context);
strcpy์ strtok_s๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ
string lines = "hello my name is";
char s1[100];
strcpy(s1, lines.c_str());
char* context = NULL;
char* token = strtok_s(s1, " ", &context);
while (token) {
log.push_back(string(token));
cout << string(token) << endl;
token = strtok_s(NULL, " ", &context);
}
find ํจ์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
string ํด๋์ค์์ ๋ฌธ์์ด์ ์ฐพ์ ๋ ์ฌ์ฉํ๋ find ํจ์๋ฅผ ํ์ฉํด ๋ฌธ์์ด์ ์๋ฅด๋ ๋ฐฉ๋ฒ
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string lines = "hello,my,name,is";
size_t previous = 0, current;
current = lines.find(',');
// find ํจ์๋ ํด๋น ์์น๋ถํฐ ๋ฌธ์์ด์ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ npos๋ฅผ ๋ฐํํ๋ค.
while (current != string::npos) {
// ์ฒซ ์ธ์์ ์์น๋ถํฐ ๋๋ฒ์งธ ์ธ์ ๊ธธ์ด๋งํผ substring์ ๋ฐํ
string substring = lines.substr(previous, current - previous);
cout << substring << " ";
previous = current + 1;
// previous ๋ถํฐ ,์ด ๋์ค๋ ์์น๋ฅผ ์ฐพ๋๋ค.
current = lines.find(',', previous);
}
// ๋ง์ง๋ง ๋ฌธ์์ด ์ถ๋ ฅ
cout << lines.substr(previous, current - previous);
return 0;
}
stringstream์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ (#include <sstream> ํค๋ ํ์)
๊ณต๋ฐฑ tab ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ๋ค.
string lines = "hello my name is";
stringstream ss(lines);
string tmp;
while ((ss >> tmp)) {
log.push_back(tmp);
cout << tmp << endl;
}
istringstream์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ (#include <sstream> ํค๋ ํ์)
๊ตฌ๋ถ์๋ฅผ ์ ํ ์ ์๋ค.
string lines = "2002:04:21";
istringstream iss(lines);
string token;
while (getline(iss, token, ':')) {
cout << token << endl;
}