string按照指定字符分割
int main()
{
string line,b;
getline(cin,line);
stringstream ss(line);
while(ss>>b) {
cout<<b<<endl;
}
}
string读取某⼀⾏,然后按空格分隔吹每⼀个string
char str[] = "now#the tiger is coming#please run away";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL )
{
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
字符分割
void split(string &str, string delimit, vector<string>&result) {
size_t pos = str.find(delimit);
str += delimit;//将分隔符加⼊到最后⼀个位置,⽅便分割最后⼀位
字符串截取几个字符
while (pos != string::npos) {
result.push_back(str.substr(0, pos));
str = str.substr(pos + 1);//substr的第⼀个参数为起始位置,第⼆个参数为复制长度,默认为string::npos到最后⼀个位置        pos = str.find(delimit);
}
}
int main() {
string str;
string delimit;
while (cin>>str)
{
cin >> delimit;
vector<string> result;
split(str, delimit, result);
for (int i = 0; i < result.size(); ++i) {
cout << result[i] << '';
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。