64,637
社区成员
发帖
与我相关
我的任务
分享
#include <sstream>
#include <vector>
int main(int argc, char* argv[])
{
std::istringstream iss("123 abc 89 ...");
std::vector<std::string> tokens;
std::string s;
while(iss >> s)
{
tokens.push_back(s);
if(tokens.size() == 2) break;
}
//whatever
return 0;
}
[/quote]
谢谢解答,受教了#include <iostream>
#include <string>
#include<sstream>
#include <vector>
int main(int argc, char* argv[])
{
std::string text = "123 abc 89 ...";
std::vector<std::string> vec;
std::istringstream stream(text);
std::string str;
while(stream>>str){
vec.push_back(str);
//if(vec.size()==2)break; //根据数量来停止
//if(vec.back()=="abc")break; //根据插入的字符串是否为指定内容来停止
}
for(auto v:vec){
std::cout<<v<<std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include<sstream>
int main(int argc, char* argv[])
{
std::string text = "123 abc 89 ...";
std::string str[2];//换成vector之类的容器,可实现任意数量
std::istringstream stream(text);
for(int i=0;i<2;i++){//把2改成n指定数量
stream>>str[i];
/*if(str[i]=="abc")//"abc"换指定字符串
break;*/
}
for(int i=0;i<2;i++)
std::cout<<str[i]<<std::endl;
getchar();
return 0;
}
#include <sstream>
#include <vector>
int main(int argc, char* argv[])
{
std::istringstream iss("123 abc 89 ...");
std::vector<std::string> tokens;
std::string s;
while(iss >> s)
{
tokens.push_back(s);
if(tokens.size() == 2) break;
}
//whatever
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
vector<string> veStr;
veStr.clear();
cout<<"请输入需要被拆分的字符串,以空格为间隔:"<<endl;
char szBuffer[256] = {0x00};
gets(szBuffer); //获取输入的字符串
string strSource = szBuffer;
cout<<strSource<<endl;
unsigned long nBegin = 0, nPos = 0;
string strSubStr;
while((nPos = strSource.find(" ", nBegin)) != string::npos) //查找" "
{
strSubStr = strSource.substr(nBegin, nPos-nBegin);
veStr.push_back(strSubStr);
nBegin = nPos + 1;
if (nBegin > strSource.size())
break;
}
if (nBegin <= strSource.size()){
strSubStr = strSource.substr(nBegin);
veStr.push_back(strSubStr);
}
int nSize = veStr.size();
for (int n = 0;n<nSize;n++) //打印
{
cout<<veStr[n]<<endl;
}
system("pause");
return 0;
}
#include <string>
#include <sstream>
#include <vector>
int main(int argc, char* argv[])
{
std::istringstream iss("123 abc 89 ...");
std::vector<std::string> tokens;
std::string s;
while(iss >> s)
{
tokens.push_back(s);
if(s == "abc") break;
}
//whatever
return 0;
}
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
int main()
{
std::string strTemp= "123 ab 89 def 504";
boost::char_separator<char> ep(" ");
tokenizer tok(strTemp,ep);
for (tokenizer::const_iterator it = tok.begin(); it != tok.end(); ++it)
{
std::cout<<*it<<std::endl;
}
return 0;
}
#include <stdio.h>
int main(int argc, char* argv[])
{
char text[] = "123 abc 89 ...";
char str[2][10];
sscanf(text, "%s%s", str[0], str[1]);
printf("(%s, %s)\n", str[0], str[1]);
return 0;
}
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
p=s;
while (1) {
k=sscanf(p,"%d%n",&v,&n);
printf("k,v,n=%d,%d,%d\n",k,v,n);
if (1==k) {
p+=n;
} else if (0==k) {
printf("skip char[%c]\n",p[0]);
p++;
} else {//EOF==k
break;
}
}
printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.