list
- List containers are implemented as doubly-linked lists
Initialization
#include <iostream>
#include <list>
template <class T>
void display(T it, T end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
std::list<int> l2(10, 100);
std::list<int> l3(l2.begin(), l2.end());
int array[] = {1, 2, 3, 4};
std::list<int> l4(array, array+sizeof(array)/sizeof(int));
display(l4.begin(), l4.end());
return 0;
}
Retrieve List
#include <iostream>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
//front
l.front() *= 10;
std::cout<<l.front()<<std::endl;//10
//back
l.back() *= 10;
std::cout<<l.back()<<std::endl;//40
//advance
auto it = l.begin();
std::advance(it, 1);
*it *= 10;
std::cout<<*it<<std::endl;//20
//begin()
//std::cout<<*(l.begin()[1])<<std::endl;//error, the memory for list is not contiguous
return 0;
}
Insert and Delete
#include <iostream>
#include <list>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
//push_front
l.push_front(10);
//push_back
l.push_back(100);
//pop_front
l.pop_front();
//pop_back
l.pop_back();
//insert
auto it = l.begin();
std::advance(it, 1);
l.insert(it, 10);
display(l.begin(), l.end());//1 10 2 3 4
//erase
it = l.begin();
std::advance(it, 2);
l.erase(it);
display(l.begin(), l.end());//1 10 3 4
//resize
l.resize(10);//1 10 3 4 0 0 0 0 0 0
//clear
l.clear();
return 0;
}
Size
#include <iostream>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
l.resize(10);
std::cout<<"Size: "<<l.size()<<std::endl;//10
std::cout<<"Max_size: "<<l.max_size()<<std::endl;
return 0;
}
Operations
#include <iostream>
#include <list>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> l1 = {1, 2, 3, 4};
std::list<int> l2 = {10, 20, 30, 40};
//splice
auto it = l1.begin();
std::advance(it, 1);
l1.splice(it, l2);
display(l1.begin(), l1.end());//1 10 20 30 40 2 3 4
display(l2.begin(), l2.end());//l2 is empty
//remove
l1.remove(2);
display(l1.begin(), l1.end());//1 10 20 30 40 3 4
//remove_if
l1.remove_if([](int n){if(n%2 == 1) return n;});
display(l1.begin(), l1.end());//10 20 30 40 4
//sort
std::list<int> l3 = {8, 1, 5, 4, 3, 5, 1};
l3.sort();
display(l3.begin(), l3.end());//1 1 3 4 5 5 8
//unique
l3.unique();
display(l3.begin(), l3.end());//1 3 4 5 8
//merge
std::list<int> l4 = {1, 2, 3, 4};
std::cout<<"Merge: "<<std::endl;
//l4.merge(l3);
//l4.merge(l3, [](int a, int b) {return (a < b);});//return true if the first argument is less than the second
l4.merge(l3, [](int a, int b) {if (a < b) return true;else return false;});
display(l4.begin(), l4.end());//1 1 2 3 3 4 4 5 8
display(l3.begin(), l3.end());//l3 is empty
//sort
std::list<int> l5 = {4, 3, 2, 1};
//l5.sort();
l5.sort([](int a, int b){return (a<b);});
display(l5.begin(), l5.end());//1 2 3 4
//reverse
l5.reverse();
display(l5.begin(), l5.end());// 4 3 2 1
return 0;
}
Bubble Sort
#include <iostream>
#include <list>
#include <iterator>
template <class T>
void display(T c)
{
for(auto e : c)
std::cout<<e<<" ";
std::cout<<std::endl;
}
template <class T>
void sort(T c)
{
for(auto i = std::prev(c.end()); i != std::prev(c.begin()); --i)
{
for(auto j = c.begin(); j != i; ++j)
if(*j > *std::next(j))
{
std::swap(*j, *std::next(j));
}
display(c);
}
}
int main(int argc, char *argv[])
{
std::list<int> l = {5, 4, 3, 2, 1};
sort(l);//bubble sort
return 0;
}
Reference