输出vector迭代器,出错。

kaponihap 2009-04-07 01:19:14
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include<iterator>

using namespace std;

typedef vector<vector<string> > vec_ins;
typedef vector<vector<string> >::iterator iter;
typedef vector<string>::iterator it_str;


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}

int main()
{
int i=0,max_line;
vec_ins a;
vector<string> v_str[100];
string ss_line;
it_str iter_str;
iter it;
ifstream is;
if ( !open_file(is, "text_in.txt") )
{
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
while ( getline(is, ss_line) )
{
v_str[i].push_back(ss_line);
a.push_back(v_str[i++]);
}
max_line=i;
for(i=0;i<max_line;i++)
for(iter_str=v_str[i].begin();iter_str!=v_str[i].end();iter_str++)
cout<<*iter_str<<endl;
for(it=a.begin();it!=a.end();it++)
cout<<*it<<endl;
}

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<c
lass std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' (or there is no acceptable conversion)
执行 cl.exe 时出错.


我想输出vector,但是老是报错,实在不知道如何搞,求高手帮助。
...全文
91 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyond071 2009-04-07
  • 打赏
  • 举报
回复
学习了
Paradin 2009-04-07
  • 打赏
  • 举报
回复
顶猫狗
se7venhigh 2009-04-07
  • 打赏
  • 举报
回复
个人觉得要是只是输出内容的话,iter_str就够了,试了下能够按原文输出内容的。
mengde007 2009-04-07
  • 打赏
  • 举报
回复
楼上真快啊……
  • 打赏
  • 举报
回复

typedef vector <vector <string> > vec_ins;
typedef vector <vector <string> >::iterator iter;
typedef vector <string>::iterator it_str;


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}

int main()
{
int i=0,max_line;
vec_ins a;
vector <string> v_str[100];
string ss_line;
it_str iter_str;
iter it;
ifstream is;
if ( !open_file(is, "text_in.txt") )
{
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
while ( getline(is, ss_line) )
{
v_str[i].push_back(ss_line);
a.push_back(v_str[i++]);
}
max_line=i;
for(i=0;i <max_line;i++)
for(iter_str=v_str[i].begin();iter_str!=v_str[i].end();iter_str++)
cout <<*iter_str <<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<" :";
for(iter_str=it->begin();iter_str!=it->end();iter_str++) //用iter_str迭代器再去输出it,这样就可以了
cout <<*iter_str <<endl;
}
}
  • 打赏
  • 举报
回复
max_line=i;
for(i=0;i <max_line;i++)
for(iter_str=v_str[i].begin();iter_str!=v_str[i].end();iter_str++)
cout < <*iter_str < <endl;
for(it=a.begin();it!=a.end();it++) //it指向的是vector <string>数据,<<无法直接输出,要么重载操作符,要么再套迭代器
cout < <*it < <endl;
}

65,211

社区成员

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

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