STL中list的各个接口的使用
来源:互联网 发布:网络电视机顶盒功能 编辑:程序博客网 时间:2024/06/05 14:58
list是标准STL中的序列式容器,它里面的元素是有序的线性序列
STL中的list是一个双向循环链表,可以高效的插入删除元素
list不支持随机访问,因为它的存储空间不是连续的
list中的元素是通过迭代器来遍历的,迭代器是一种检查容器内元素并遍历的数据类型
标准STL中的list
list中的成员函数
一Member functions:
1、constructor:
a、explicit list ( const Allocator& = Allocator() );
默认构造函数链表为空
b、explicit list ( size_type n, const T& value = T(), const Allocator& = Allocator() );
带参数的构造函数,构造有n个值为value结点
c、template < class InputIterator >
list ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
构造函数的参数类型为迭代器的类型
d、list ( const list& x)
用已有链表构造一个新的链表
void Printlist(list<int> L)//封装打印函数{ list<int>::iterator it = L.begin(); while (it != L.end()) { cout << *it << " "; ++it; } cout << endl;}void fun1(){ list<int> first; //默认构造函数里边为空 list<int>second(5, 1); //创建5个值为1的链表 list<int>third(second.begin(), second.end());//使用带迭代器参数的构造函数将一块空间拷贝过来 list<int>forth(third);//拷贝构造函数 Printlist(first); Printlist(second); Printlist(third); Printlist(forth);}
2、析构函数destructor
~list();
回收我们之前分配的内存,STL中的内存分配是由自己的空间配置器来分配的
3、operator=赋值运算符重载
void Printlist(list<int> L){ list<int>::iterator it = L.begin(); while (it != L.end()) { cout << *it << " "; ++it; } cout << endl;}void fun2(){ list<int>first(5, 1); list<int>second(3, 2); Printlist(first); Printlist(second); second = first; Printlist(second);}
二:Iterators:迭代器的使用
1->begin();
iterator_begin() :普通迭代器接口
const iterator_begin() const : const迭代器接口
2->end();
iterator end ();
const_iterator end () const;
void fun3(){ list<int>L(5, 1); list<int>::iterator it = L.begin(); //这里返回的是非const迭代器调用的begin;list中的元素可以修改 while (it != L.end()) { (*it)++; cout << *it << " "; ++it; } cout << endl;}
void fun4(){ list<int>L(5, 1); list<int>::const_iterator it = L.begin(); //这里返回的是const迭代器调用的begin;list中的元素不可以修改 while (it != L.end()) { (*it)++; cout << *it << " "; ++it; } cout << endl;}
3->rbegin():
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
4->rend():
reverse_iterator rend();
const_reverse_iterator rend() const;
void fun5(){ list<int>L; L.push_back(1); L.push_back(2); L.push_back(3); L.push_back(4); L.push_back(5); list<int>::reverse_iterator rit = L.rbegin(); while (rit != L.rend()) { cout << *rit << " "; ++rit; } cout << endl;}void fun2()
三:Capacity(容量)
bool empty ( ) const;
size_type size() const();
size_type max_size () const;
void fun6(){ list<int>L; for (size_t i = 0; i < 10; i++) L.push_back(i); Printlist(L); cout << L.empty() << endl;//判断链表是否为空 cout << L.size() << endl; //链表中元素的有效个数 cout << L.max_size() << endl; //当前链表能存放元素最多个数}
四:Element access
front(): 链表的头
reference front ( );
const_reference front ( ) const;
back(); 链表的尾
reference back ( );
const_reference back ( ) const;
void fun7(){ list<int>L; for (size_t i = 1; i < 10; i++) L.push_back(i); Printlist(L); cout << L.front() << endl; cout << L.back() << endl;}
五:modifiers关于修改链表的内容
1>assign():
void assign ( InputIterator first, InputIterator last );//参数直接给成迭代器类型的,赋值的是一段区间
void assign (size_type n, const value_type& val);//直接赋值给链表n个值为val的结点
void fun8(){ list<int> L1; list<int> L2; L1.assign(5, 6); L2.assign(L1.begin(), L1.end()); Printlist(L1); Printlist(L2);}
2>push_front():
void push_front ( const T& x );
3>push_back():
void push_back ( const T& x );
4>pop_front():
void pop_front ( )
5>pop_back():
void pop_back ( );
void fun9(){ list<int> L1; list<int> L2; L1.push_back(1); L1.push_back(2); L1.push_back(3); L1.push_back(4); Printlist(L1); L1.pop_back(); Printlist(L1); L2.push_front(1); L2.push_front(2); L2.push_front(3); L2.push_front(4); Printlist(L2); L2.pop_front(); Printlist(L2);}
6>insert():
iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template
void insert ( iterator position, InputIterator first, InputIterator last );
void fun(){ list<int>first; list<int>second; list<int>third; first.insert(first.begin(), 5); second.insert(second.begin(), 5, 2); third.insert(third.begin(), second.begin(), second.end()); Printlist(first); Printlist(second); Printlist(third);}
7>erase():
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );
void fun1(){ list<int> L1; list<int> L2; L1.push_back(1); L1.push_back(2); L1.push_back(3); L1.push_back(4); Printlist(L1); L1.erase(L1.begin()); Printlist(L1); L1.erase(L1.begin(), L1.end()); Printlist(L1);}
8>swap()
void swap(list& x);
void fun2(){ list<int> L1(5, 1); list<int> L2(5, 0); Printlist(L1); Printlist(L2); L1.swap(L2);//交换两个链表 Printlist(L1); Printlist(L2);}
9>resize
void resize (size_type n, value_type val = value_type());
void fun3(){ list<int> L1(5, 1); L1.resize(3); L1.resize(8, 5);//超过指定size插入5 L1.resize(10);//没有指定元素插入0 Printlist(L1);}
六:operations
1.splice
void splice (iterator position, list& x);
void splice (iterator position, list& x, iterator i);
void splice (iterator position, list& x, iterator first, iterator last);
void fun9(){ list<int> L1; list<int> L2; L1.push_back(1); L1.push_back(2); L1.push_back(3); L1.push_back(4); L2.push_back(5); L2.push_back(6); L2.push_back(7); L2.push_back(8); list<int>::iterator it; it = L1.begin(); ++it; L1.splice(it, L2);//1 5 6 7 8 2 3 4 Printlist(L1); L2.splice(L2.begin(), L1, it); Printlist(L1);//1 5 6 7 8 3 4 Printlist(L2); // 2 it = L1.begin(); L2.splice(L2.begin(),L1,it, L1.end());//将l1插入到L2之前 Printlist(L2);//1 5 6 7 8 3 4 2}
2>remove
void fun4(){ list<int> L1; L1.push_back(1); L1.push_back(2); L1.push_back(3); L1.push_back(2); L1.push_back(4); Printlist(L1); L1.remove(2);//删除链表中所有的2 Printlist(L1);}
3>unique(去重)
void fun4(){ list<int> L1; L1.push_back(1); L1.push_back(2); L1.push_back(2); L1.push_back(3); L1.push_back(2); L1.push_back(4); Printlist(L1); L1.unique(); Printlist(L1);}
注意去重:要配合sort用只能去相邻的相同元素
4>merge(合并链表)
void fun5(){ list<int> l1(5, 1); list<int> l2(5, 2); l1.merge(l2);//把l2链表合并到l1中 Printlist(l1);}
5>sort(排序)
void fun6(){ list<int> L1; L1.push_back(1); L1.push_back(0); L1.push_back(2); L1.push_back(8); L1.push_back(3); L1.push_back(4); Printlist(L1); L1.sort(); Printlist(L1);}
- STL中list各个接口的使用
- 标准STL中list的各个接口的使用
- STL中list的各个接口的使用
- STL 中list的常用接口
- STL中list的使用
- STL中list的使用
- STL中list的使用:
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用
- STL中list的使用:
- STL中list的使用
- STL中list的使用(理论)
- STL中list的使用(理论)
- STL中list的使用(理论)
- STL 中 容器 list 的使用
- Linux cp
- Java 9 中的新特性
- log4j
- Eclipse去掉对JS文件的Validation
- Neo4J 安装&常见语句操作&项目中的集成应用&复杂查询(@Query注解和自定义Match)
- STL中list的各个接口的使用
- Incomplete history of a file in git (git-svn)
- UML序列图
- 大数据是什么和大数据技术十大核心原理详解
- Redis与Tomcat 简单Session共享
- 多线程:为什么在while循环中加入System.out.println,线程可以停止
- Jersey Politics POJ
- PHP中error_reporting()用法详解
- hrbust 哈理工 oj 1585 公主之魔镜魔镜 (优先队列)