- Mastering C++ Programming
- Jeganathan Swaminathan
- 274字
- 2021-07-02 18:28:50
List
The list STL container makes use of a doubly linked list data structure internally. Hence, a list supports only sequential access, and searching a random value in a list in the worst case may take O(N) runtime complexity. However, if you know for sure that you only need sequential access, the list does offer its own benefits. The list STL container lets you insert data elements at the end, in the front, or in the middle with a constant time complexity, that is, O(1) runtime complexity in the best, average, and worst case scenarios.
The following image demonstrates the internal data structure used by the list STL:

Let's write a simple program to get first-hand experience of using the list STL:
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
int main () {
list<int> l;
for (int count=0; count<5; ++count)
l.push_back( (count+1) * 100 );
auto pos = l.begin();
cout << "\nPrint the list ..." << endl;
while ( pos != l.end() )
cout << *pos++ << "-->";
cout << " X" << endl;
return 0;
}
I'm sure that by now you have got a taste of the C++ STL, its elegance, and its power. Isn't it cool to observe that the syntax remains the same for all the STL containers? You may have observed that the syntax remains the same no matter whether you are using an array, a vector, or a list. Trust me, you will get the same impression when you explore the other STL containers as well.
Having said that, the previous code is self-explanatory, as we did pretty much the same with the other containers.
Let's try to sort the list, as shown in the following code:
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
int main () {
list<int> l = { 100, 20, 80, 50, 60, 5 };
auto pos = l.begin();
cout << "\nPrint the list before sorting ..." << endl;
copy ( l.begin(), l.end(), ostream_iterator<int>( cout, "-->" ));
cout << "X" << endl;
l.sort();
cout << "\nPrint the list after sorting ..." << endl;
copy ( l.begin(), l.end(), ostream_iterator<int>( cout, "-->" ));
cout << "X" << endl;
return 0;
}
Did you notice the sort() method? Yes, the list container has its own sorting algorithms. The reason for a list container to support its own version of a sorting algorithm is that the generic sort() algorithm expects a random access iterator, whereas a list container doesn't support random access. In such cases, the respective container will offer its own efficient algorithms to overcome the shortcoming.
Interestingly, the runtime complexity of the sort algorithm supported by a list is O (N log2 N).
- Spring Boot 2實戰之旅
- DBA攻堅指南:左手Oracle,右手MySQL
- Monkey Game Development:Beginner's Guide
- Python for Secret Agents:Volume II
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- 跟“龍哥”學C語言編程
- 精通Scrapy網絡爬蟲
- C++程序設計基礎教程
- Visual C++應用開發
- 劍指MySQL:架構、調優與運維
- The Complete Coding Interview Guide in Java
- 編程菜鳥學Python數據分析
- OpenStack Networking Essentials
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- INSTANT Apache ServiceMix How-to