64,648
社区成员
发帖
与我相关
我的任务
分享
void TextQuery::build_map()
{
for(line_no line_num=0;
line_num != lines_of_text.size();
++line_num)
{
string line_string=lines_of_text[line_num];
char separators[11]={',','.',';',':','(',')','&','<','>','_','+'}; //11
char blank(' ');
vector<char> v_str;
for(size_t i=0;i != line_string.size();++i)
v_str.push_back(line_string[i]);
for(size_t i=0;i<11;++i)
replace(v_str.begin(),v_str.end(),separators[i],blank);
string new_line_string(v_str.begin(),v_str.end());
istringstream line(new_line_string);
string word;
while(line >> word)
word_map[word].insert(line_num);
}
}
#include <string.h>
#include <stdio.h>
int main(void)
{
char input[] = "hello,world!\nfor(;;);\nstd::string\nline>>word";
char *p,*delm=",()!\n;:>";
p = strtok(input, delm);
while(p)
{
printf("%s\n", p);
p = strtok(NULL, delm);
}
getchar();
return 0;
}
#include<iostream>
#include<locale>
#include<algorithm>
#include<sstream>
using namespace std;
struct commactype:ctype<char>
{
commactype():ctype<char>(get_table()){}
static ctype_base::mask const*get_table()
{
static ctype_base::mask*rc=0;
if(rc==0)
{
rc=new ctype_base::mask[std::ctype<char>::table_size];
fill_n(rc,ctype<char>::table_size,ctype_base::mask());
rc[',']=ctype_base::space;//设置自己的分隔符
rc['.']=ctype_base::space;
}
return rc;
}
};
int main()
{
istringstream iss("word,hello,we.can.do");
iss.imbue(locale(locale(),new commactype));
string word;
while(iss>>word){
cout<<word<<endl;
}
return 0;
}