鏁呭鐩稿叧鐨勭煡璇嗗仛涓涓葷粨
1.浣跨敤
std::stringstream
鍒囧壊瀛楃涓?br>姣斿:
std::stringstream str(text);
std::string tok;
while(getline(str,tok,(char)32)){}
2.榪樻槸浣跨敤std::stringstream嫻佹瀽鍑?br>姣斿:
std::stringstream str(s);
string t1,t2,t3,t4;
str >>t1;
str >>t2;
str >>t3;
str >>t4;
3.浣跨敤strtok
姣斿:
char* token = strtok(str.c_str()," ");
4.浣跨敤boost鐨?
tokenizer
姣斿:
boost::tokenizer<> ss(s,char_separator(' '));
for(boost::tokenizer<>::iterator beg=ss.begin(); beg!=ss.end(); ++beg){cout<<*beg<<endl;}
涓嶈繃鍏墮粯璁ょ殑鍒嗛殧涓虹┖鏍煎拰鏍囩偣絎﹀彿
濡傛灉闇瑕佸畾鍒跺叾妯℃澘鍙傛暟
鍙互鎸夌収涓嬮潰鐨勪功鍐?
class char_separator
{
public:
char_separator(char c):sep(c){}
void reset(){}
template <typename InputIterator,typename Token>
bool operator()(InputIterator& next,InputIterator end,Token& tok)
{
tok = Token();
for(;next != end && *next == sep;++next);
if (next == end)
return false;
for(;next != end && *next != sep;++next)
{
tok += *next;
}
return true;
}
private:
char sep;
};
姣斿:
boost::tokenizer<char_separator> mytok(str,char_separator('@'));
for(boost::tokenizer<char_separator>::iterator beg=mytok.begin(); beg!=mytok.end(); ++beg)
{
if(index == 0)
text1 = *beg;
else if(index == 1)
text2 = *beg;
else if(index == 2)
text3 = *beg;
else if(index == 3)
text4 = *beg;
index++;
}
5.榪樻槸boost.
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/find_iterator.hpp>
using namespace std;
using namespace boost;
int main(int argc, char *argv[])
{
vector<string> strlist;
string str("12 34 56 678 ccsdu2004");
split(strlist,str,is_any_of(" "),token_compress_on);
for(unsigned int index=0;index<strlist.size();index++)
{
cout<<index <<":"<<strlist[index]<<endl;
};
system("PAUSE");
return EXIT_SUCCESS;
}
6.鍏朵粬涓嶇煡閬撶殑鏂規硶鑻ュ共
...

]]>