文件内容读取到string对象

zp8451867 2008-07-13 09:04:26
输入文件格式如下:
键值1 s1 s2 ....
键值2 s1 s2 s3 ....
.........

其中键值和si 都是字符串,中间都是用\t隔开,每行间用\n隔开,每个键值后面跟的字符串个数也不一样

现在要把它们存到一个 multimap <string,string> 结构中,即输入文件中每行第一个字符串为MAP的键值,每个键值对应多个字符串。数据量很大,效率要求较高,请问如何处理?

我尝试了用以下方法处理

ifstream inFile(filename)
string s;
inFile >> s;

这样一个一个读字符串,但是无法判断什么时候读到了每行的开头,以为每行开头的字符串要当作键值插入。有没有什么方法能判断出所读字符串位于每行第一个呢?

...全文
244 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2008-07-14
  • 打赏
  • 举报
回复
楼主,优先考虑用map<string, list<string> >
taodm 2008-07-14
  • 打赏
  • 举报
回复
楼主,优先考虑用map<string, list<string> >
K行天下 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zp8451867 的回复:]
引用 3 楼 k2eats 的回复:
你读到char数组里面 用strtok将字符串分开


读到CHAR数组里也不行,因为每行的数据是不定长的,数组空间不好分配,我认为只能先读一行到string中再想办法一个个提取字符串。
[/Quote]
哦,我看错题目意思了
初步做了一下:看合不合楼主的意思



//---------------------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int main(int argc, char* argv[])
{
fstream fin;

fin.open("1.txt", fstream::in);
multimap <string,string> ms;
multimap <string,string>::iterator it;

string str, value;
size_t pos;

if(!fin.is_open())
return -1;
while(getline(fin, str))
{
string::size_type p = str.find_first_of('\t');
string key = str.substr(0, p);
str.erase(0, p+1);

while( (pos = str.find_first_of('\t')) != string::npos)
{
value = str.substr(0, pos);

ms.insert(pair<string,string>(key, value));

str.erase(0, pos+1);

}

ms.insert(pair<string,string>(key, str));
}

for ( it=ms.begin() ; it != ms.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
cin.get();
return 0;

}
///////////////////////////////////////////////
/* 1.txt的文件内容:
键值1 s1 s2
键值2 s1 s2 s3
键值3 s1 s2
键值4 s1 s2 s3
键值5 s1 s2
键值6 s1 s2 s3
键值7 s1
键值8 s1 s2 s3 s4
*/
////////////////////////////////////////////////


////////////////////////////////////////////////
/*运行结果:
键值1 => s1
键值1 => s2
键值2 => s1
键值2 => s2
键值2 => s3
键值3 => s1
键值3 => s2
键值4 => s1
键值4 => s2
键值4 => s3
键值5 => s1
键值5 => s2
键值6 => s1
键值6 => s2
键值6 => s3
键值7 => s1
键值8 => s1
键值8 => s2
键值8 => s3
键值8 => s4
*/
///////////////////////////////////////////////////////
//---------------------------------------------------------------------------


lily604 2008-07-13
  • 打赏
  • 举报
回复
ifstream inFile(filename)
string s;
inFile.getline(s);
hagangqiou 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ttkk_2007 的回复:]
C/C++ code//举个例子:intmain()

{
ifstreamin("test1.txt");stringstr;if(!in)return1;while(getline(in, str)){string::size_type p=str.find('\t');stringkey=str.substr(0, p);
str.erase(0, p);string::size_type pos=str.find_first_not_of('\t');stringvalue=str.substr(pos);
cout<<key<<":"<<value<<endl;//怎么处理,就看你了}

}
[/Quote]

支持这个方法:getline(in,str);
shancao 2008-07-13
  • 打赏
  • 举报
回复
getline();就是读取一行的意思
skineffect 2008-07-13
  • 打赏
  • 举报
回复
string line;
ifstream input("myfile.txt");
getline(input,line);
用上面的代码就可将文本中的一行读入到line之中,然后定义个string find_elem="\t\n",用
string::size_type pos=0;
string::size_type pre_pos=0;
pos=line.find_first_of(find_elem,pos);
上面几行语句依次找到每个"\t"或"\n",然后可以用
string key,value;
key=line.substr(pre_pos,pos);
pre_pos=++pos;
将分割后的一个子sting压入到key中,再进行下个寻找,并将它们依次赋给value.
最后再一并赋给multimap<string,string>
我现在对multimap还不是很熟,所以先不写代码了:-)
zp8451867 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 k2eats 的回复:]
你读到char数组里面 用strtok将字符串分开
[/Quote]

读到CHAR数组里也不行,因为每行的数据是不定长的,数组空间不好分配,我认为只能先读一行到string中再想办法一个个提取字符串。
ttkk_2007 2008-07-13
  • 打赏
  • 举报
回复

//举个例子:
int main()

{
ifstream in("test1.txt");
string str;
if(!in)
return 1;
while(getline(in, str)){
string::size_type p = str.find('\t');
string key = str.substr(0, p);
str.erase(0, p);
string::size_type pos = str.find_first_not_of('\t');
string value = str.substr(pos);
cout << key << " :" << value << endl; //怎么处理,就看你了
}

}
K行天下 2008-07-13
  • 打赏
  • 举报
回复
你读到char数组里面 用strtok将字符串分开
zp8451867 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ttkk_2007 的回复:]
你一行一行的读,每读一行,把第一个字符串当成键,后面的当成值
[/Quote]

大哥,麻烦能不能说清楚点,我是新手。
好像用getline可以读一行到一个string里面吧???但是我不知道怎么样从string里面提取出各个字符串,能讲讲吗?
ttkk_2007 2008-07-13
  • 打赏
  • 举报
回复
你一行一行的读,每读一行,把第一个字符串当成键,后面的当成值

64,281

社区成员

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

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