官术网_书友最值得收藏!

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).

主站蜘蛛池模板: 绥阳县| 墨玉县| 孝义市| 和静县| 宝鸡市| 绥阳县| 出国| 都兰县| 高青县| 万全县| 永善县| 金溪县| 岳阳县| 威海市| 连城县| 凤台县| 镇远县| 都昌县| 博客| 蚌埠市| 桐梓县| 阿克陶县| 宜宾县| 库尔勒市| 东辽县| 夏津县| 耒阳市| 连州市| 敦煌市| 红桥区| 柳林县| 牙克石市| 长泰县| 阿城市| 新民市| 修水县| 呼伦贝尔市| 金坛市| 芦溪县| 灵丘县| 运城市|