读书人

其实很简单的请各位大侠们帮小弟给

发布时间: 2012-09-03 09:48:39 作者: rapoo

其实很简单的,请各位大侠们,帮小弟给解决这个问题啊,感激不尽,给分的
01.230 321 365421.5
这是我在文件中读取的其中一行,我现在要求的是C++,string来写的,依据空格将他们分成三个子字符串,也就是01.230是一个字符串,321是一个字符串,365421.5是一个字符串,麻烦了各位,先说声谢谢了

[解决办法]

C/C++ code
#include <string>#include <iostream>#include <vector>using namespace std;int main(){        string test = "01.230 321 365421.5";        string one,two,three;        string strtemp;                string::size_type pos1, pos2;        pos2 = test.find(' ');        pos1 = 0;         if(string::npos != pos2)            {                one = test.substr(pos1, pos2 - pos1);                pos1 = pos2 + 1;                pos2 = test.find(' ', pos1);             }               if(string::npos != pos2)        {                      two= test.substr(pos1, pos2 - pos1);                pos1 = pos2 + 1;                pos2 = test.find(' ', pos1);         }               three = test.substr(pos1));               return 0;} 

读书人网 >C++

热点推荐