📅 2011-Jan-04 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, lists, stl ⬩ 📚 Archive
A STL list in C++ provides the features of a doubly-linked list. Using the list is similar to using any other STL container, with iterators. Here is a simple example that illustrates the common list operations:
#include <list>
using namespace std;
typedef list<int> IntList;
typedef IntList::iterator IListIter;
typedef IntList::const_iterator IListCIter;
int main()
{// Empty list
IntList ilist; 100 ); // List with 100 elements of 0
IntList ilist1( 100, 3 ); // List with 100 elements of 3
IntList ilist2( // List copied from previous list
IntList ilist3( ilist2.begin(), ilist2.end() );
// Create list: 10->20->30
10 );
ilist.push_back( 20 );
ilist.push_back( 30 );
ilist.push_back(
// 3
ilist.size(); // false
ilist.empty();
// 10
ilist.front(); // 30
ilist.back();
99 ); // 99 added, 99->10->20->30
ilist.insert( ilist.begin(), // 99 erased, 10->20->30
ilist.erase( ilist.begin() ); // Entire list erased
ilist.erase( ilist.begin(), ilist.end() );
return 0;
}