‘ostream_iterator’ 在此作用域中尚未声明

本文介绍了一个使用C++标准模板库(STL)中的list容器进行元素插入、查找和复制的示例程序。通过具体代码展示了如何将数组逆序插入到列表前端,并在找到特定元素后在该位置前插入新的元素。

2.cpp代码如下:

#include <iostream>
#include <algorithm>
#include <list>
 
using namespace std;
 
int iArray[5] = { 1, 2, 3, 4, 5 };
 
void Display(list<int>& v, const char* s);
 
int main()
{
  list<int> iList;
 
  // Copy iArray backwards into iList
  copy(iArray, iArray + 5, front_inserter(iList));
  Display(iList, "Before find and copy");
  
  // Locate value 3 in iList
  list<int>::iterator p =
	  find(iList.begin(), iList.end(), 3);
  
  // Copy first two iArray values to iList ahead of p
  copy(iArray, iArray + 2, inserter(iList, p));
  Display(iList, "After find and copy");
  
  return 0;
}

void Display(list<int>& a, const char* s)
{
	cout << s << endl;
	copy(a.begin(), a.end(),
		ostream_iterator<int>(cout, " "));
	cout << endl;
}


在linux下运行,出现:

[root@localhost home]# g++ 2.cpp
2.cpp: In function ‘void Display(std::list<int, std::allocator<int> >&, const char*)’:
2.cpp:34: 错误:‘ostream_iterator’ 在此作用域中尚未声明
2.cpp:34: 错误:expected primary-expression before ‘int’

 

上网查了下,才知道少包含了头文件#include <iterator>

加进去就运行OK了

[root@localhost home]# ./a.out
Before find and copy
5 4 3 2 1
After find and copy
5 4 1 2 3 2 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值