Bradbury 2020. 4. 9. 12:44
728x90

์„œ๋ก 

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;
}

 

728x90