转贴:STL之list之用法小结

简介:
/*
list: 等同于双向链表,内存空间可以是不连续的,通过指针来进行数据的访问
   优: 1)插入/删除效率高
   缺: 1)不支持随机存取,查询效率较低
*/

#include 
< iostream >
#include 
< string >
#include 
< list >
using   namespace  std;

void  printList(list < int >  nList)
{
//使用迭代器
//list<int>::iterator iter;
//for ( iter = nList.begin(); iter != nList.end(); iter++)
//{
// cout<<*iter<<", ";
//}

//使用迭代器指针
list<int>::iterator *pIter = new list<int>::iterator; //list<int>::iterator *pIter;error,原因见vector相应函数
if ( NULL == pIter )
{
   return;
}

for (*pIter = nList.begin(); *pIter != nList.end(); (*pIter)++) //此处不可写成*pIter++
{
   cout<<**pIter<<", ";
}

cout<<endl;
}

int  main()
{
//创建list
list<int> l1; //创建一个没有任何元素的list
list<int> l2(10); //创建一个有n个元素的list,每个元素值为默认
list<double> l3(10, 9.3); //创建具有10个元素的list,每个元素的初始值为9.3
list<double> l4(l3); //通过拷贝一个list对象的元素,创建一个新的list对象
int iArray[] ={3, 10, 19};
list<int> l5(iArray, iArray + 3);//将另一个list对象的迭代器区间[first, last)所指的元素,拷贝到新创建的list对象中

//初如化赋值 :用push_back将元素依次链入LIST中
for (int i = 1; i<6; i++)
   l1.push_back(i);

list<int>::iterator iter;
cout<<"printList(l1): "<<endl;
printList(l1);

//元素插入 :尾部添加用push_back(); 首部插入用push_front();任意位置插入用insert(&pos, elem) 
//下面语句error:list与vector不同,因为存储方式上的差异, list不能+n,只能++,
//这种情况下最好还是通过迭代器++为好
//l1.insert((l1.begin())+1, 100); 
l1.insert(++l1.begin(), 100); //或者用iter=l1.begin(); iter++;l1.insert(iter, 100);
cout<<"++l1.insert(l1.begin(),100) (if use l1.begin()+1 is error) = "<<endl;
printList(l1);

cout<<"l1.push_back(200) = "<<endl;
l1.push_back(200);
printList(l1);
cout<<"l1.push_front(-200) = "<<endl;
l1.push_front(-200);
printList(l1);

//元素删除: 尾部删除用pop_back(); 首部删除用pop_front(); 
//指定元素的删除,位置用erase(&pos); 区间用erase(&first_pos, &last_pos)
//删除所有元素用 clear(); 
//删除list中所有元素值为value的元素用remove(value)
cout<<"l1.pop_back() = "<<endl;
l1.pop_back();
printList(l1);
cout<<"l1.pop_front() = "<<endl;
l1.pop_front();
printList(l1);

cout<<"l1.erase(++l1.begin()) = "<<endl;
l1.erase(++l1.begin());
printList(l1);

iter= l1.begin();
iter++;
iter++;
cout<<"iter=l1.begin; iter++;iter++; l1.erase(l1.begin(), iter) = "<<endl;
l1.erase(l1.begin(), iter);
printList(l1);
cout<<"l1.remove(100) = "<<endl;
l1.remove(100);
printList(l1);
cout<<"l1.remove(4) = "<<endl;
l1.remove(4);
printList(l1);

//其它
cout<<"其它: "<<endl;
list<int> listTest;
for (int i =1; i<6; i++)
   listTest.push_back(i*100);
cout<<"printList(listTest) = "<<endl;
printList(listTest);
//swap函数
cout<<"after l1.swap(listTest), l1 = "<<endl;
l1.swap(listTest);
printList(l1);

//splice函数 
//void splice(&pos, list &x):将list x归并到当前list的&pos之前,同时list x将被清空
//void splice(&pos, list &x, &x.pos),将list x中迭代器x.pos处的元素归并到当前list,同时被归并的元素将被清空
cout<<"after l1.splice(l1.begin(), listTest):"<<endl;
l1.swap(listTest); //恢复
l1.splice(l1.begin(), listTest);
cout<<"l1 = "<<endl;
printList(l1);
cout<<"listTest = "<<endl;
printList(listTest);

l1.clear();
for (int i = 1; i<6; i++)
   l1.push_back(i);
for (int i =1; i<6; i++)
   listTest.push_back(i*100); //恢复
cout<<"after l1.splice(++l1.begin(), listTest, --listTest.end()) :"<<endl;
l1.splice(++l1.begin(), listTest, --listTest.end());
cout<<"l1 = "<<endl;
printList(l1);
cout<<"listTest = "<<endl;
printList(listTest);

//merge函数(略)归并的两链表都是是有序性,此函数才有意义
//sort
cout<<"now l1 is set as : "<<endl;
l1.clear();
for (int i = 9; i >= 0; i--)
   l1.push_back(i);
printList(l1);

cout<<"l1.sort() = "<<endl;
l1.sort();
printList(l1);

cout<<"l1.empty() = "<<l1.empty()<<", l1.size() = "<<l1.size()<<endl;
cout<<"after l1.clear(): "<<endl;
l1.clear();
cout<<"l1.empty() = "<<l1.empty()<<", l1.size() = "<<l1.size()<<endl;
}




// 测试结果
printList(l1):
1 2 3 4 5 ,
++ l1.insert(l1.begin(), 100 ) ( if  use l1.begin() + 1   is  error
1 100 2 3 4 5 ,
l1.push_back(
200 =
1 100 2 3 4 5 200 ,
l1.push_front(
- 200 =
- 200 1 100 2 3 4 5 200 ,
l1.pop_back() 
=
- 200 1 100 2 3 4 5 ,
l1.pop_front() 
=
1 100 2 3 4 5 ,
l1.erase(
++ l1.begin())  =
1 2 3 4 5 ,
iter
= l1.begin; iter ++ ;iter ++ ; l1.erase(l1.begin(), iter)
3 4 5 ,
l1.remove(
100 =
3 4 5 ,
l1.remove(
4 =
3 5 ,
其它:
printList(listTest) 
=
100 200 300 400 500 ,
after l1.swap(listTest), l1 
=
100 200 300 400 500 ,
after l1.splice(l1.begin(), listTest):
l1 
=
100 200 300 400 500 3 5 ,
listTest 
=

after l1.splice(
++ l1.begin(), listTest,  -- listTest.end())
l1 
=
1 500 2 3 4 5 ,
listTest 
=
100 200 300 400 ,
now l1 
is   set   as  :
9 8 7 6 5 4 3 2 1 0 ,
l1.sort() 
=
0 1 2 3 4 5 6 7 8 9 ,
l1.empty() 
=   0 , l1.size()  =   10
after l1.clear():
l1.empty() 
=   1 , l1.size()  =   0
相关文章
|
1天前
|
编译器 C++ 容器
【C++/STL】:list容器的深度剖析及模拟实现
【C++/STL】:list容器的深度剖析及模拟实现
8 2
|
1天前
|
存储 C++ 容器
【C++/STL】:list容器的基本使用
【C++/STL】:list容器的基本使用
6 1
|
6天前
|
存储 索引 Python
Python教程:深入了解 Python 中 Dict、List、Tuple、Set 的高级用法
Python 中的 Dict(字典)、List(列表)、Tuple(元组)和 Set(集合)是常用的数据结构,它们各自有着不同的特性和用途。在本文中,我们将深入了解这些数据结构的高级用法,并提供详细的说明和代码示例。
13 2
|
20天前
|
存储 C++
C++初阶学习第十一弹——探索STL奥秘(六)——深度刨析list的用法和核心点
C++初阶学习第十一弹——探索STL奥秘(六)——深度刨析list的用法和核心点
22 7
|
22天前
|
C++ 容器
9.STL中list的常见操作(图文并茂)
9.STL中list的常见操作(图文并茂)
|
1月前
|
存储 C++ 容器
黑马c++ STL部分 笔记(7) list容器
黑马c++ STL部分 笔记(7) list容器
|
11天前
|
存储 缓存 编译器
【C++进阶】深入STL之list:模拟实现深入理解List与迭代器
【C++进阶】深入STL之list:模拟实现深入理解List与迭代器
10 0
|
11天前
|
C++ 容器
【C++进阶】深入STL之list:高效双向链表的使用技巧
【C++进阶】深入STL之list:高效双向链表的使用技巧
14 0
|
1月前
|
存储 编译器 C++
【STL】list的底层原理及其实现
【STL】list的底层原理及其实现
|
15小时前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
6 1