我有一个字符串currentLine="12 23 45",需要从中提取出12、23、45,但不使用Boost库。由于我使用的是字符串,所以strtok对我来说无法使用。我尝试了很多方法,但仍然没有成功。
下面是我的最后一次尝试:
没有使用strtok的方法
下面是我的最后一次尝试:
while(!inputFile.eof())
while(getline(inputFile,currentLine))
{
int countVar=0;
int inputArray[10];
char* tokStr;
tokStr=(char*)strtok(currentLine.c_str()," ");
while(tokstr!=NULL)
{
inputArray[countVar]=(int)tokstr;
countVar++;
tokstr=strtok(NULL," ");
}
}
}
没有使用strtok的方法
string currentLine;
while(!inputFile.eof())
while(getline(inputFile,currentLine))
{
cout<<atoi(currentLine.c_str())<<" "<<endl;
int b=0,c=0;
for(int i=1;i<currentLine.length();i++)
{
bool lockOpen=false;
if((currentLine[i]==' ') && (lockOpen==false))
{
b=i;
lockOpen=true;
continue;
}
if((currentLine[i]==' ') && (lockOpen==true))
{
c=i;
break;
}
}
cout<<b<<"b is"<<" "<<c;
}
tokstr
和tokStr
大小写不匹配?另外,https://dev59.com/k3VC5IYBdhLWcg3wnCj6。 - Mr Lister