当前位置:网站首页>Use of list
Use of list
2022-06-21 17:46:00 【Hero 2021】
List of articles
One 、list Introduction to
1.1 list Introduction to
- list You can use O(1) Time complexity of A sequential container that can be inserted and deleted anywhere , And the container can iterate back and forth .
- list Of The bottom layer is a two-way linked list structure , Each element in a two-way linked list is stored in independent nodes that are not related to each other , In a node, point to its previous element and the next element through a pointer .
- list And forward_list Very similar : The main difference is forward_list It's a single chain table , Can only iterate forward , Has made it simpler and more efficient .
- Compared with other sequential containers (array,vector,deque),list It is usually inserted at any position 、 Removing elements is more efficient .
- Compared with other sequential containers ,list and forward_list The biggest flaw is Random access to any location is not supported , such as : To visit list Of the 6 Elements , Must be from a known location ( Like the head or tail ) Iterate to this location , Iterating at this location requires a linear time overhead ;list Some extra space is needed , To save the associated information of each node ( For large elements with smaller storage types list This may be an important factor )
Two 、list Use
2.1 list Constructor for
| Constructors | Interface specification |
|---|---|
| list() | Empty structure |
| list (size_type n, const value_type& val = value_type()) | The initialization of the list Contained in the n individual val value |
| list (const list& x) | copy constructor |
| list (InputIterator first, InputIterator last) | Using iterator intervals [first,last) structure list |
void test_list1()
{
// Empty structure
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
for (int e: l1)
{
cout << e << " ";
}
cout << endl;
// The initialization of the list Contained in the n individual val value
list<int> l2(4,10);
for (int e : l2)
{
cout << e << " ";
}
cout << endl;
// copy constructor
list<int> l3(l1);
for (int e : l3)
{
cout << e << " ";
}
cout << endl;
// Using iterator intervals [first,last) structure list
list<int> l4(l3.begin(), l3.end());
for (int e : l4)
{
cout << e << " ";
}
}
2.2 list The use of iterators
| Function declaration | Interface specification |
|---|---|
| begin+end | Returns the iterator of the first element + return The iterator at the next position of the last element |
| rbegin+rend | return end Location + return begin Location |
// just \ reverse iterator
void test_list2()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
// Forward iterator
list<int>::iterator it = lt.begin();
while (it!=lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// reverse iterator
list<int>::reverse_iterator rit = lt.rbegin();
while (rit!=lt.rend())
{
cout << *rit << " ";
++rit;
}
}
2.3 list Related capacity size related function
| Function declaration | Interface specification |
|---|---|
| empty | testing list Is it empty , Is to return true, No return false |
| size | return list Number of valid nodes in |
void test_list3()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
cout << l1.size() << endl; // 4
cout << l1.empty() << endl;// 0
}
2.4 list Data access related functions
| Function declaration | Interface specification |
|---|---|
| front | return list Reference to the first node value in |
| back | return list Reference to the last node value in |
void test_list4()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
cout << l1.front() << endl; // 1
cout << l1.back() << endl; // 4
}
2.5 list Data adjustment related functions
| Function declaration | Interface specification |
|---|---|
| push_front | Insert element before first element |
| pop_front | Delete first element |
| push_back | Tail insertion |
| pop_back | Deletion at the end |
| insert | stay pos Position insertion value |
| erase | Delete pos The value of the location |
| swap | Two exchanges list The value in |
| clear | Empty list Valid elements in |
void test_list5()
{
list<int> l;
l.push_back(1);
l.push_front(2);
list<int>::iterator it = l.begin();
++it;
l.insert(it, 20);
for (int e : l)
{
cout << e << " ";
}
cout << endl;
cout << "------" << endl;
l.clear();
for (int e : l)
{
cout << e << " ";
}
}
2.6 list Other function operations in
| Function declaration | Interface specification |
|---|---|
| sort | Sort |
| reverse | Inversion |
| unique | duplicate removal ( Generally, you need to sort before de duplication ) |
| remove | Delete a given value |
void test_list6()
{
list<int> l1;
l1.push_back(1);
l1.push_back(7);
l1.push_back(3);
l1.push_back(3);
l1.push_back(3);
l1.push_back(4);
// Sort
l1.sort();
for (int e : l1)
{
cout << e << " ";
}
cout << endl;
// Inversion
l1.reverse();
for (int e : l1)
{
cout << e << " ";
}
cout << endl;
// duplicate removal
l1.unique();
for (int e : l1)
{
cout << e << " ";
}
cout << endl;
// Delete a given value
l1.remove(7);
for (int e : l1)
{
cout << e << " ";
}
cout << endl;
}
边栏推荐
- One trick: let logs help you make decisions through Yanrong SaaS data service platform +elk
- 潤邁德醫療通過上市聆訊:預計虧損將增加,霍雲飛兄弟持股約33%
- 润迈德医疗通过上市聆讯:预计亏损将增加,霍云飞兄弟持股约33%
- JetPack compose 状态提升(二)
- Seventy years of neural network: review and Prospect
- PTA L3-032 关于深度优先搜索和逆序对的题应该不会很难吧这件事 (30 分)
- Application architecture principles
- 3DE 运动轮廓数据修改
- Kubernetes + 焱融 SaaS 数据服务平台,个性化需求支持就没输过
- 3de 3D model View ne voit pas comment ajuster
猜你喜欢

Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud

How to adjust 3DE 3D model view if you can't see it

One trick: let logs help you make decisions through Yanrong SaaS data service platform +elk

PTA l3-031 thousand hand Guanyin (30 points)

Vscade tool
![[Oracle] is there a](/img/21/3c481be79a4f19b06a2a93ded4159f.png)
[Oracle] is there a "time" data type in oracle-- Research on Oracle data types

Behind Yanrong SaaS service platform, which is as stable as a rock, is the rise of data ecology

Jetpack compose status promotion (II)

火山引擎+焱融 YRCloudFile,驱动数据存储新增长

加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证
随机推荐
Stack cognition - stack overflow instance (ret2shellcode)
Kubernetes + Yanrong SaaS data service platform, personalized demand support has never been lost
Performance test ---locust's on_ Start and on_ Stop method
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
Is it safe and reliable to open futures accounts on koufu.com?
Analysis of 43 cases of MATLAB neural network: Chapter 26 classification of LVQ Neural Network - breast tumor diagnosis
3DE motion contour data modification
Seventy years of neural network: review and Prospect
LeetCode_字符串_简单_387. 字符串中的第一个唯一字符
What is the difference between IEC62133 and EN62133? Which items are mainly tested?
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
经纬度转换为距离
PTA l3-031 thousand hand Guanyin (30 points)
为什么RedisCluster设计成16384个槽?
窗帘做EN 1101易燃性测试过程是怎么样的?
正则表达式
Kotlin annotation declaration and use
fs. Readfile() and fs writeFile()
How to write technical documents software engineering at Google
compose 编程思想