//STL示例 advance及distance
#include <iostream>
#include <list>
#include <algorithm> //find use
#include <iterator>
using namespace std;
int iArray[5] = { 1, 2, 3, 4, 5 };
void Display(list<int>& a, const char* s);
int main ()
{
list<int> iList;
list<int>::iterator p=iList.begin();//定义迭代器并指向容器开始
copy(iArray, iArray + 5, inserter(iList,p));//从开头一个个插入元素
//copy(iArray, iArray + 5, front_inserter(iList));//前端插入
Display(iList,"插入后:");
p =find(iList.begin(), iList.end(), 2);
cout << "before: p == " << *p << endl;
advance(p, 2); //等于p+=2;
cout << "after : p == " << *p << endl;
int k = 0;
k=distance(p,iList.end());//等于iList.end()-p
cout << "k == " << k << endl;
return 0;
}
void Display(list<int>& a, const char* s)
{
cout << s << endl;
copy(a.begin(), a.end(),ostream_iterator<int>(cout, " "));//使用空格分开容器各项
cout << endl;
}
分享到:
相关推荐
常用STL的示例代码,一些例子,常用STL的示例代码
C++ STL库使用示例, 包含了基本上所有C++库函数使用, 是C++开发常用必备手册
设计模式+STL代码示例 设计模式+STL代码示例 设计模式+STL代码示例
西门子plc案例,stl学习
资源中包含了使用MFC的示例代码和使用STL的示例代码 Zippe(使用STL的示例代码)只能在release STL配置下编译通过 本压缩包是ZipArchive的GPL版本,通过研究这些代码对熟悉压缩算法,MFC和STL都有一定的帮助。
C++ STL_示例word 格式 提供例程 string vector list等数据结构示例 #include #include using namespace std; void main() { //用const char * 构造strText对象 string strText("This is a test"); //在strText...
STL经典学习代码实例,unique,search,
stl STL编程 C++ STL使用教程 stl 电子书两本(C++ STL使用教程,STL编程)
很详细全面的stl教程,包含非常详细的c++ stl使用教程及编程教学
vc下使用stl获得各类型最大最小值的示例程序。欢迎下载,使用和交流。
基于stl共享内存,可以像使用STL容器一样使用共享内存。方便快捷。具体参考里面的代码实现
STL入门 STL入门 STL入门 STL入门 STL入门 STL入门
自己的学习笔记 STL 算法库大部分函数的用法 带注释
条款27:用distance和advance把const_iterator转化成iterator 条款28:了解如何通过reverse_iterator的base得到iterator 条款29:需要一个一个字符输入时考虑使用istreambuf_iterator 算法 条款30:确保目标区间...
基于STL-10 格式写入3个文件:stl10-train.t7, stl10-test.t7, stl10-unlabeled.t7 每个文件都是一个表格形式: th > stl10 = torch. load ( ' stl10-train.t7 ' )th > print (stl10){ data : ByteTensor - size: ...
STL的容器deque的详细使用方法和文档 6.0代码
总结的STL中vector map list的使用方法和误区
Vector、Deque、List、Set等等,快速学习STL实例 ,迄今为止较好的实例,包括类、结构等作为stl元素