如何用istringstream和其他分隔符来分隔字符串,而不是空格?

时间:2022-09-23 07:56:04

The following trick using istringstream to split a string with white spaces.

下面的技巧使用istringstream来分割一个带有空格的字符串。

int main() {
    string sentence("Cpp is fun");
    istringstream in(sentence);
    vector<string> vec = vector<string>(istream_iterator<string>(in), istream_iterator<string>());
    return 0;
}

Is there a similar trick to split a string with any delimiter? For instance, | in "Cpp|is|fun".

用任何分隔符分割字符串有类似的技巧吗?例如,“Cpp|”中的|就是|fun。

3 个解决方案

#1


20  

Generally speaking the istringstream approach is slow/inefficient and requires at least as much memory as the string itself (what happens when you have a very large string?). The C++ String Toolkit Library (StrTk) has the following solution to your problem:

一般来说,istringstream方法是缓慢/低效的,并且至少需要与字符串本身一样多的内存(当您有一个非常大的字符串时会发生什么?)c++ String Toolkit Library (StrTk)对您的问题有以下解决方案:

#include <string>
#include <vector>
#include <deque>
#include "strtk.hpp"
int main()
{
   std::string sentence1( "Cpp is fun" );
   std::vector<std::string> vec;
   strtk::parse(sentence1," ",vec);

   std::string sentence2( "Cpp,is|fun" );
   std::deque<std::string> deq;
   strtk::parse(sentence2,"|,",deq);

   return 0;
}

More examples can be found Here

更多的例子可以在这里找到

#2


3  

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::istringstream iss { "Cpp|is|fun" };

  std::string s;
  while ( std::getline( iss, s, '|' ) )
    std::cout << s << std::endl;

  return 0;
}

Demo

演示

#3


-1  

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

using namespace std;


vector<string> splitter(string in_pattern, string& content){
    vector<string> split_content;

    regex pattern(in_pattern);
    copy( sregex_token_iterator(content.begin(), content.end(), pattern, -1),
    sregex_token_iterator(),back_inserter(split_content));  
    return split_content;
}

int main()
{   

    string sentence = "This|is|the|sentence";
    //vector<string> words = splitter(R"(\s+)", sentence); // seperate by space
    vector<string> words = splitter(R"(\|)", sentence);

    for (string word: words){cout << word << endl;}

}   

// we use regex to find the "|" and split the surrounding elements into an array. We then cout each of those elements in a for loop.

//  This method allows for splitting with regex as an alternative

#1


20  

Generally speaking the istringstream approach is slow/inefficient and requires at least as much memory as the string itself (what happens when you have a very large string?). The C++ String Toolkit Library (StrTk) has the following solution to your problem:

一般来说,istringstream方法是缓慢/低效的,并且至少需要与字符串本身一样多的内存(当您有一个非常大的字符串时会发生什么?)c++ String Toolkit Library (StrTk)对您的问题有以下解决方案:

#include <string>
#include <vector>
#include <deque>
#include "strtk.hpp"
int main()
{
   std::string sentence1( "Cpp is fun" );
   std::vector<std::string> vec;
   strtk::parse(sentence1," ",vec);

   std::string sentence2( "Cpp,is|fun" );
   std::deque<std::string> deq;
   strtk::parse(sentence2,"|,",deq);

   return 0;
}

More examples can be found Here

更多的例子可以在这里找到

#2


3  

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::istringstream iss { "Cpp|is|fun" };

  std::string s;
  while ( std::getline( iss, s, '|' ) )
    std::cout << s << std::endl;

  return 0;
}

Demo

演示

#3


-1  

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

using namespace std;


vector<string> splitter(string in_pattern, string& content){
    vector<string> split_content;

    regex pattern(in_pattern);
    copy( sregex_token_iterator(content.begin(), content.end(), pattern, -1),
    sregex_token_iterator(),back_inserter(split_content));  
    return split_content;
}

int main()
{   

    string sentence = "This|is|the|sentence";
    //vector<string> words = splitter(R"(\s+)", sentence); // seperate by space
    vector<string> words = splitter(R"(\|)", sentence);

    for (string word: words){cout << word << endl;}

}   

// we use regex to find the "|" and split the surrounding elements into an array. We then cout each of those elements in a for loop.

//  This method allows for splitting with regex as an alternative