[高分求读取字符串求助

inori0821 2008-10-08 04:55:39
string s;
inf.open("file")s
while(getline(inf,s));

这个file每行都是由60个大小写字母组成
请教如何建立个数组,把每行按照每2个字母一组啊
比如有
aabbccddee
如何建立数组成array[0][0]=aa;array[0][1]=bb
以此类推啊?

万分感谢
...全文
102 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
inori0821 2008-10-08
  • 打赏
  • 举报
回复
感谢各位帮忙
解决了
谢谢
baihacker 2008-10-08
  • 打赏
  • 举报
回复
额,是在6楼的时候看错了...
换一组数据:

d:\>a
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11
22 33 44
bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11 22
33 44 55
cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11 22 33
44 55 66
dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11 22 33 44
55 66 77
ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11 22 33 44 55
66 77 88

d:\>
wudeshou82666 2008-10-08
  • 打赏
  • 举报
回复
取每行到string...
由于每行的字母固定
直接用循环
在里面用string类的成员函数搞定
baihacker 2008-10-08
  • 打赏
  • 举报
回复

//在5楼看错了
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;


int main()
{
vector< vector<string> > result;
string s;
fstream inf;
inf.open( "file.txt", ios::in);
while (getline(inf,s))
{
vector<string> vtemp;
if (s.length() >= 60)
for (int i = 0; i < 60; i+=2)
vtemp.push_back(s.substr(i, 2));
result.push_back(vtemp);
}
inf.close();
for (int i = 0; i < result.size();cout << endl, ++i)
for (int j = 0; j < result[i].size(); ++j)
cout << result[i][j] << " ";
return 0;
}


d:\>g++ test.cpp

d:\>a
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11
22 33 44
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11
22 33 44
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11
22 33 44
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 11
22 33 44

d:\>
lann64 2008-10-08
  • 打赏
  • 举报
回复
笨笨的写一个,仅仅示意。
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
string s="AABBCCDDEEFF";
vector<string> v;
char ch;
string snull;
istringstream line(s);
int lcount=0;

while(line>>ch)
{
v.push_back(snull);
v[lcount]+=ch;
line>>ch;
v[lcount]+=ch;
++lcount;
}

for (int i=0;i<lcount;i++)
cout<<v[i]<<endl;

return 0;
}
effective_person 2008-10-08
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str="aabbccddeeff";
string str1[10];
for(int i=0;i<str.size()-2;i++)
{
str1[i]=str.substr(i,2); //保存到数组中
cout<<str1[i]<<endl;
}
return 1;
}
baihacker 2008-10-08
  • 打赏
  • 举报
回复

//仅以读一行为例

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;


int main()
{
char result[30][2];
string s;
fstream inf;
inf.open( "file.txt", ios::in);
getline(inf,s);
memcpy(result, s.c_str(), 60);
inf.close();
for (int i = 0; i < 30;cout << endl, ++i)
for (int j = 0; j < 2; ++j)
cout << result[i][j];
return 0;
}
ysuliu 2008-10-08
  • 打赏
  • 举报
回复
用fstream。。。
wuyu637 2008-10-08
  • 打赏
  • 举报
回复
最笨的方法,就是一个一个访问,拿到char之后再做你要的赋值处理
lann64 2008-10-08
  • 打赏
  • 举报
回复
这个事先不知道第一维维数,数组定义(申请)多大呢?
用vector吧。
wuyu637 2008-10-08
  • 打赏
  • 举报
回复
int main()
{

string temp("hello world");

cout << temp << endl;
const char * pChar = temp.c_str();
for(int i = 0;i< temp.length();i++)
{
cout << *(pChar+i) << endl;

}
}
OenAuth.Core 2008-10-08
  • 打赏
  • 举报
回复
这个用数组,太麻烦了,只能用STL了

再说了,把每行按照每2个字母一组???是原来就有,还是原来没有,要变成2个一组?

64,654

社区成员

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

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