在C++中如何分隔字符串

1使用boost  regex

#include <string>

#include <iostream>

#include <iterator>

#include <boost/regex.hpp>

#include <boost/algorithm/string/regex.hpp>

#include <vector>

using namespace std;

using namespace boost;

int main()

{

  string str = "abc1234def->ijkl->mnop56qrs->tuv7890wxyz" ;

  vector< string > result;

  boost::algorithm::split_regex( result, str,

                                 regex( "[0-9]+|->" ) ) ;

  copy( result.begin(), result.end(),

        ostream_iterator<string>( cout, "\n" ) ) ;

}

2、使用std::stringstream

//这个使用基本的stringstream功能,实际上我们可以指定std::istringstream分隔符

#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main()

{

    string s("Somewhere down the road");

    istringstream iss(s);

    do

    {

        string sub;

        iss >> sub;

        cout << "Substring: " << sub << endl;

    } while (iss);

    return 0;

}

3、它使用真正的C++算法和迭代器概念

#include <iostream>

#include <string>

#include <sstream>

#include <algorithm>

#include <iterator>

int main() {

    using namespace std;

    string sentence = "Something in the way she moves...";

    istringstream iss(sentence);

    copy(istream_iterator<string>(iss),

             istream_iterator<string>(),

             ostream_iterator<string>(cout, "\n"));

}

4、调用getline算法,似乎相当合适

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {

    std::stringstream ss(s);

    std::string item;

    while(std::getline(ss, item, delim)) {

        elems.push_back(item);

    }

    return elems;

}

5、

使用boost库

下面将使用boost中多个库,它们实际上从多个方面多个方式解决同一个问题。

//----------------------------------------------------------

//这个好像是最简单不过的了

#include <boost/algorithm/string.hpp>

std::vector<std::string> strs;

boost::split(strs, "string to split", boost::is_any_of("\t "));

//--------------------------------------------------------------

//使用boost中专门用户分隔的库,tokenizer,这实际上是一个分隔框架

#include <iostream>

#include <string>

#include <boost/foreach.hpp>

#include <boost/tokenizer.hpp>

using namespace std;

using namespace boost;

int main(int argc, char** argv)

{

   string text = "token  test\tstring";

   char_separator<char> sep(" \t");

   tokenizer<char_separator<char>> tokens(text, sep);

   BOOST_FOREACH(string t, tokens)

   {

      cout << t << "." << endl;

   }

}

//--------------------------------------------------------------------

6、这个使用boost字符串算法库,string_algo

#include <iostream>

#include <vector>

#include <boost/algorithm/string.hpp>

template<typename _OutputIterator>

inline void split(

    const std::string& str, 

    const std::string& delim, 

    _OutputIterator result)

{

    using namespace boost::algorithm;

    typedef split_iterator<std::string::iterator> It;

    for(It iter=make_split_iterator(str, first_finder(delim, is_equal()));

            iter!=It();

            ++iter)

    {

        *(result++) = boost::copy_range<std::string>(*iter);

    }

}

int main(int argc, char* argv[])

{

    using namespace std;

    vector<string> splitted;

    split("HelloFOOworldFOO!", "FOO", back_inserter(splitted));

    // or directly to console, for example

    split("HelloFOOworldFOO!", "FOO", ostream_iterator<string>(cout, "\n"));

    return 0;

}

string message = "A^B^C^D"; vector<string> tokens; boost::split(tokens, message, boost::is_any_of("^"));

boost::char_separator<char> sep("^"); boost::tokenizer<boost::char_separator<char> > tokens(text, sep);

//-----------------------------------------------------------------------------

7、这个使用正则表达式库,可以使用正则表达式作为分隔符

#include <iostream>

#include <string>

#include <boost/regex.hpp>

int main() {

    std::string line("A:::line::to:split");

    const boost::regex re(":+"); // one or more colons

    // -1 means find inverse matches aka split

    boost::sregex_token_iterator tokens(line.begin(),line.end(),re,-1);

    boost::sregex_token_iterator end;

    for (; tokens != end; ++tokens)

        std::cout << *tokens << std::endl;

}

8手工解析或使用strtok

#include <iostream>

#include <string>

#include <vector>

using namespace std;

vector<string> split(string str, const char delim) {

    vector<string> v;

    string tmp;

    string::iterator i;

    for(i = str.begin(); i <= str.end(); ++i) {

        if((const char)*i != delim  && i != str.end()) {

            tmp += *i; 

        } else {

            v.push_back(tmp);

            tmp = ""; 

        }   

    }   

    return v;

}

//-----------------------------------------------------------------

//这个使用标准C里提供的一个函数,strtok

#include<string>

using namespace std;

vector<string> split(char* str,const char* delim)

{

    char* token = strtok(str,delim);

    vector<string> result;

    while(token != NULL)

    {

        result.push_back(token);

        token = strtok(NULL,delim);

    }

    return result;

}

std::vector<std::string> split(const std::string &s, char delim) {

    std::vector<std::string> elems;

    return split(s, delim, elems);

}

猜你喜欢

转载自blog.csdn.net/lwhsyit/article/details/81302937