首页 > 解决方案 > Cleanest way to tokenize a std::string in C++ with the '\t' tab character as the single delimiter?

问题描述

std::vector<std::string> tokenize(std::string str) {
    std::vector<std::string> tokens;

    /* 
      // trying to figure out how to split the string up into individual token strings
      // 
    */
    
    return tokens;
}

void testFunc() {
  std::string text= "   smoker  coffee";
  std::vector<std::string> tokens = tokenize(text);
  
  for (std::string s : tokens) {
      std::cout << s << std::endl;
  }
  
}


output:
smoker
coffee

I've tried using std::getline(...) as suggested here How to split a file lines with space and tab differentiation?

but, I don't think it worked right and was tripping up over whether or not I was using the '\t' character as a delimiter correctly.

标签: c++stringstd

解决方案


推荐阅读