65,211
社区成员
发帖
与我相关
我的任务
分享WideCharToMultiByte()
#include <string>
#include <iostream>
#include <vector>
#include "Windows.h"
using namespace std;
vector <string> Split(string &s)
{
string::size_type pos=0, pre_pos=0;
string tempword;
vector <string> TermVec;
char huang[] = ",";
char huang1[] = "矛";
string::size_type pos1 = 0, pos2 = 0;
while((pos = (pos1 = s.find(',', pos)) > (pos2 = s.find(",", pos)) ? pos2 : pos1) != string::npos )
// 这里不能使用楼主原来的函数,原来的函数都是针对单个字符的,如果是中文的逗号就会出现高字节匹配的问题
// while((pos = s.find_first_of(",,", pos)) != string::npos)
{
tempword = s.substr(pre_pos, pos-pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ') + 1);
if(tempword.size()==0){ pre_pos = ++pos; continue;}
TermVec.push_back(tempword);
if (IsDBCSLeadByte(s[pos]))// 这里主要使用了windows的API,判断当前字节是不是双字节字符
{
pos += 2;
pre_pos = pos;
}
else
{
pre_pos=++pos;
}
}
tempword=s.substr(pre_pos,pos-pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ')+1);
if(tempword.size()!=0)
TermVec.push_back(tempword);
return TermVec;
}
void main()
{
string s="我们是很好的朋友,可是有的时候却少不了闹矛盾,真好的,就连她都认为是不好的";
vector <string> vec=Split(s);
for(vector <string>::iterator iter=vec.begin();iter!=vec.end();iter++)
{cout <<*iter <<endl;}
}#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector<string> Split(string &s)
{
string::size_type pos=0, pre_pos=0;
string tempword;
vector <string> TermVec;
while(s[pos])
{
if(s[pos] == ',' || *(unsigned short *)&s[pos] == 0xaca3) // 逗号
{
tempword = s.substr(pre_pos, pos - pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ') + 1);
if(s[pos] < 0)
++ pos;
pre_pos = ++ pos;
if(tempword.size())
TermVec.push_back(tempword);
}
else
{
if(s[pos] < 0)
++ pos;
++ pos;
}
}
tempword=s.substr(pre_pos,pos-pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ')+1);
if(tempword.size())
TermVec.push_back(tempword);
return TermVec;
}
void main()
{
string s = "我们是很好的朋友 , 时间片, 连接 , 却少不了埃琣闹矛盾,可是有的时候,,就连她都认为是不好的";
vector <string> vec = Split(s);
for(vector <string>::iterator i = vec.begin(); i!=vec.end();i++)
cout << *i <<endl;
}
#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector<string> Split(string &s)
{
string::size_type pos=0, pre_pos=0;
string tempword;
vector <string> TermVec;
while(s[pos])//(pos = s.find_first_of(",,", pos)) != string::npos)
{
if(s[pos] != ',' && *(unsigned short *)&s[pos] != 0xaca3)
{
if(s[pos] < 0)
++ pos;
pos ++;
continue;
}
tempword=s.substr(pre_pos, pos - pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ') + 1);
if(s[pos] < 0)
++ pos;
pre_pos = ++ pos;
if(!tempword.size())
continue;
TermVec.push_back(tempword);
}
tempword=s.substr(pre_pos,pos-pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ')+1);
if(tempword.size())
TermVec.push_back(tempword);
return TermVec;
}
void main()
{
string s = "我们是很好的朋友,时间片,连接,却少不了埃琣闹矛盾,可是有的时候,,就连她都认为是不好的";
vector <string> vec = Split(s);
for(vector <string>::iterator i = vec.begin(); i!=vec.end();i++)
cout << *i <<endl;
}
if(s[pos] < 0 && *(unsigned short *)&s[pos] != 0xaca3)
{
pos += 2;
continue;
}
tempword=s.substr(pre_pos, pos - pre_pos);
#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector <string> Split(string &s)
{
string::size_type pos=0, pre_pos=0;
string tempword;
vector <string> TermVec;
while((pos=s.find_first_of(",,", pos)) != string::npos)
{
tempword=s.substr(pre_pos, pos - pre_pos);
if(s[pos] < 0 && *(unsigned short *)&s[pos] != 0xaca3)
{
pos += 2;
continue;
}
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ') + 1);
if(s[pos] < 0)
++ pos;
pre_pos = ++ pos;
if(!tempword.size())
continue;
TermVec.push_back(tempword);
}
tempword=s.substr(pre_pos,pos-pre_pos);
tempword.erase(0,tempword.find_first_not_of(' '));
tempword.erase(tempword.find_last_not_of(' ')+1);
if(tempword.size())
TermVec.push_back(tempword);
return TermVec;
}
void main()
{
string s = "我们是很好的朋友,可是有的时候却少不了闹矛盾,就连她都认为是不好的";
vector <string> vec=Split(s);
for(vector <string>::iterator iter=vec.begin();iter!=vec.end();iter++)
cout << *iter <<endl;
}