转贴: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
相关文章
|
10月前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
209 1
|
10月前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
328 7
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
193 2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
191 5
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
197 2
【C++】C++ STL 探索:List使用与背后底层逻辑(三)
【C++】C++ STL 探索:List使用与背后底层逻辑
136 2
【C++】C++ STL 探索:List使用与背后底层逻辑(二)
【C++】C++ STL 探索:List使用与背后底层逻辑
|
存储 编译器 C++
【C++】C++ STL 探索:List使用与背后底层逻辑(一)
【C++】C++ STL 探索:List使用与背后底层逻辑
159 2
|
存储 算法 程序员
C++基础知识(八:STL标准库(Vectors和list))
C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等. STL容器的提供是为了让开发者可以更高效率的去开发,同时我们应该也需要知道他们的底层实现,这样在出现错误的时候我们才知道一些原因,才可以更好的去解决问题。
182 0
|
算法 C语言 C++
【C++】详解STL的容器之一:list
【C++】详解STL的容器之一:list