嵌套vector怎么输出

modicum_tk 2012-02-27 04:58:37
#include<iostream>
#include<fstream>
#include<vector>
#include<string>

using namespace std;


struct PART
{

string id;
string seq;
vector<string> andl;
};

struct PART info;

struct HEAD
{

string id;
vector<INFO> Info;
};

struct HEAD head;

int main()
{

string str, src;

string filename = "456.txt";

ifstream infile(filename.c_str());


while(getline(infile,str))

{

string::size_type m = str.find('\t');

src = str.substr(0,m);

if( src == "@part" )

head.Info.push_back(info);

if( src == "@l" )

info.andl.push_back(str.substr(m));

if( src == "@seq" )

info.seq = str.substr(m);

if( src == "id" )

info.id = str.substr(m);

}

return 0;

}


怎么输出。 把结构体中的数据全部输出来



@part
@id 2
@seq 23
@l 553627 16591772 17567667 1

@part
@id 2
@seq 24
@l 553627 16591771 17567695 1
@l 553627 17567695 17567666 1

@part
@id 2
@seq 25
@l 553627 16591770 17567666 1
要这样的效果
...全文
418 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
open_source_road 2012-03-13
  • 打赏
  • 举报
回复
不错!
Jim_King_2000 2012-02-27
  • 打赏
  • 举报
回复

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>


using std::cin;
using std::cout;
using std::istringstream;
using std::string;
using std::vector;
using std::for_each;


class Part
{
public:
string id;
string seq;
vector<string> remainder;
};


int main()
{
vector<Part *> parts;

string line;
while (getline(cin, line))
{
istringstream isstrm(line);
string token;

while (isstrm >> token)
{
if ("@part" == token)
{
parts.push_back(new Part);
break;
}

if ("@id" == token)
{
Part *last = parts.back();
assert(NULL != last);
isstrm >> last->id;
break;
}

if ("@seq" == token)
{
Part *last = parts.back();
assert(NULL != last);
isstrm >> last->seq;
break;
}

if ("@l" == token)
{
Part *last = parts.back();
assert(NULL != last);
while (isstrm >> token)
{
last->remainder.push_back(token);
}
break;
}
}
}

vector<Part *>::iterator parts_end = parts.end();
for (vector<Part *>::iterator it = parts.begin(); it != parts_end; ++it)
{
cout << (*it)->id << '\n';
cout << (*it)->seq << '\n';
for (vector<string>::iterator itl = (*it)->remainder.begin(); itl != (*it)->remainder.end(); ++itl)
{
cout << *itl << ' ';
}
cout << '\n';
}

for (vector<Part *>::iterator it = parts.begin(); it != parts_end; ++it)
{
delete *it;
}

return 0;
}
airtobreath 2012-02-27
  • 打赏
  • 举报
回复
把456.txt里面的内容也贴出来。
Jim_King_2000 2012-02-27
  • 打赏
  • 举报
回复
你不用再发了,晚上我帮你写一个好了。
Jim_King_2000 2012-02-27
  • 打赏
  • 举报
回复
不要用嵌套的vector,效率会非常低。

64,642

社区成员

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

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