0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++で文字列を分割する方法

Last updated at Posted at 2018-12-12
split_string.cpp
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <boost/algorithm/string.hpp>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::string;
using std::stringstream;
using std::vector;

void method1();
void method2();

int main(int argc, char **argv) {
  return 0;
}

// 方法1: getlineを使う方法
void method1() {
  string line;
  cin >> line;
  string token;
  stringstream ss(line);
  char delim = ';';
  while (getline(ss, token, delim)) {
    cout << token << endl;
  }
}

// 方法2: boostのsplitを使う方法
void method2() {
  string line;
  cin >> line;
  vector<string> tokens;
  string delim = ";";
  boost::split(tokens, line, boost::is_any_of(delim));
  for (auto token : tokens) {
    cout << token << endl;
  }
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?