C++에서 문자열 토큰 화

Jinku Hu 2023년10월12일
  1. findsubstr 함수를 사용하여 C++에서 문자열 토큰 화
  2. std::stringstreamgetline 함수를 사용하여 C++에서 문자열 토큰 화
  3. istringstreamcopy 알고리즘을 사용하여 C++에서 문자열 토큰 화
C++에서 문자열 토큰 화

이 기사에서는 C++에서 문자열을 토큰 화하는 방법에 대한 몇 가지 방법을 설명합니다.

findsubstr 함수를 사용하여 C++에서 문자열 토큰 화

std::string 클래스에는 주어진 문자열 객체에서 일련의 문자를 검색하는find 함수가 내장되어 있습니다. find 함수는 문자열에서 찾은 첫 번째 문자의 위치를 반환하고 찾을 수없는 경우npos를 반환합니다. find 함수 호출이if 문에 삽입되어 마지막 문자열 토큰이 추출 될 때까지 문자열을 반복합니다.

사용자는 모든string 유형 구분자를 지정하고find 메소드에 전달할 수 있습니다. 토큰은 문자열의vector로 푸시되고 각 반복에서erase()함수로 이미 처리 된 부분이 제거됩니다.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

string text = "Think you are escaping and run into yourself.";
int main() {
  string delim = " ";
  vector<string> words{};

  size_t pos = 0;
  while ((pos = text.find(delim)) != string::npos) {
    words.push_back(text.substr(0, pos));
    text.erase(0, pos + delim.length());
  }
  if (!text.empty()) words.push_back(text.substr(0, pos));
  for (const auto &str : words) {
    cout << str << endl;
  }

  return EXIT_SUCCESS;
}

출력:

Think
you
are
escaping
and
run
into
yourself.

std::stringstreamgetline 함수를 사용하여 C++에서 문자열 토큰 화

stringstream을 사용하여 처리 할 문자열을 수집하고getline을 사용하여 주어진 구분 기호를 찾을 때까지 토큰을 추출 할 수 있습니다. 이 방법은 단일 문자 구분 기호에서만 작동합니다.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::stringstream;
using std::vector;

int main() {
  string text = "Think you are escaping and run into yourself.";
  char del = ' ';
  vector<string> words{};

  stringstream sstream(text);
  string word;
  while (std::getline(sstream, word, del)) words.push_back(word);

  for (const auto &str : words) {
    cout << str << endl;
  }

  return EXIT_SUCCESS;
}

출력:

Think
you
are
escaping
and
run
into
yourself.

istringstreamcopy 알고리즘을 사용하여 C++에서 문자열 토큰 화

또는<algorithm>헤더에서copy 기능을 사용하고 공백 구분 기호에서 문자열 토큰을 추출 할 수 있습니다. 다음 예에서는 토큰을 표준 출력으로 만 반복하고 스트리밍합니다. copy 메소드로 문자열을 처리하기 위해istringstream에 삽입하고 반복자를 활용합니다.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::stringstream;
using std::vector;

int main() {
  string text = "Think you are escaping and run into yourself.";
  string delim = " ";
  vector<string> words{};

  istringstream iss(text);
  copy(std::istream_iterator<string>(iss), std::istream_iterator<string>(),
       std::ostream_iterator<string>(cout, "\n"));

  return EXIT_SUCCESS;
}

출력:

Think
you
are
escaping
and
run
into
yourself.
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - C++ String