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

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

主站蜘蛛池模板: 永昌县| 寿阳县| 龙州县| 梓潼县| 沂水县| 临汾市| 德格县| 广宁县| 邢台市| 清涧县| 高要市| 正蓝旗| 中江县| 陵水| 辰溪县| 安吉县| 朝阳县| 和硕县| 海盐县| 固始县| 海原县| 雷州市| 温泉县| 邢台市| 冀州市| 胶南市| 宜良县| 宜黄县| 江永县| 庆安县| 清新县| 玛曲县| 镇坪县| 独山县| 星子县| 且末县| 龙口市| 保定市| 名山县| 霍邱县| 金沙县|