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

803

被折叠的 条评论
为什么被折叠?



