其实很简单的,请各位大侠们,帮小弟给解决这个问题啊,感激不尽,给分的

AXUBOD 2012-07-12 08:42:03
01.230 321 365421.5
这是我在文件中读取的其中一行,我现在要求的是C++,string来写的,依据空格将他们分成三个子字符串,也就是01.230是一个字符串,321是一个字符串,365421.5是一个字符串,麻烦了各位,先说声谢谢了
...全文
101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
W170532934 2012-07-12
  • 打赏
  • 举报
回复

#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;
}
AXUBOD 2012-07-12
  • 打赏
  • 举报
回复
可不可以不用vector这个帮忙写一下啊,我们根本还没学到vector这个啊,就用string来写
图灵狗 2012-07-12
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
string test = "01.230 321 365421.5";
vector<string> strvec;
string strtemp;

string::size_type pos1, pos2;
pos2 = test.find(' ');
pos1 = 0;
while (string::npos != pos2)
{
strvec.push_back(test.substr(pos1, pos2 - pos1));

pos1 = pos2 + 1;
pos2 = test.find(' ', pos1);
}
strvec.push_back(test.substr(pos1));

vector<string>::iterator iter1 = strvec.begin(), iter2 = strvec.end();
while (iter1 != iter2)
{
cout << *iter1 << endl;
++iter1;
}

return 0;
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧