pop_front |
C++ Library |
void pop_front()
Removes an object from the front of the list container. If you call pop_front on an empty list container the results are undefined.
#include <list> #include <iostream> #include <functional> int main() { //default constructor std::list<int> c1 ; //create list with 10 copies of 4 std::list<int> c2(10, 4) ; //copy constructor std::list<int> c3(c2) ; int ai[] = {0, 1, 2, 3, 4, 5} ; int i ; std::list<int> c4 ; //get_allocator std::list<int>::allocator_type a1 = c4.get_allocator() ; //push_back for(i = 0; i < 5; i++) c4.push_back(ai[i]) ; //range copy constructor std::list<int> c5(c4.begin(), c4.end()) ; //begin, end std::cout << "c4 (using begin, end) = " ; std::list<int>::iterator Iter ; for(Iter = c4.begin(); Iter != c4.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //rbegin, rend std::cout << "c4 (using rbegin, rend) = " ; std::list<int>::reverse_iterator RevIter ; for(RevIter = c4.rbegin(); RevIter != c4.rend(); RevIter++) std::cout << *RevIter << ", " ; std::cout << std::endl ; //assign c2.assign(c5.begin(), c5.end()) ; c1.assign(10, 4) ; //back std::cout << "last element in c2 = " << c2.back() << std::endl ; //front std::cout << "first element in c2 = " << c2.front() << std::endl ; //size std::cout << "number of elements in c2 = " << c2.size() << std::endl ; //max_size std::cout << "max number of elements c2 can hold using current allocator = " << c2.max_size() << std::endl ; //erase c3.erase(c3.begin(), c3.end()) ; //clear c2.clear() ; //empty if (c2.empty() == true) std::cout << "c2 is now empty" << std::endl ; //resize c2.resize(10, 30) ; std::cout << "number of elements in c2 = " << c2.size() << std::endl ; std::cout << "last element in c2 = " << c2.back() << std::endl ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //push_front c2.push_front(25) ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //pop_back c2.pop_back() ; std::cout << "last element in c2 = " << c2.back() << std::endl ; //pop_front c2.pop_front() ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //swap c3.swap(c2) ; std::cout << "number of elements in c3 = " << c3.size() << std::endl ; std::cout << "last element in c3 = " << c3.back() << std::endl ; std::cout << "first element in c3 = " << c3.front() << std::endl ; //insert c1.insert(c1.begin(), 20) ; c3.insert(c3.begin(), 4, 10) ; c3.insert(c3.end(), c5.begin(), c5.end()) ; std::cout << "c1 = " ; for(Iter = c1.begin(); Iter != c1.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //reverse c3.reverse() ; std::cout << "c3, after reversing = " ; for(Iter = c3.begin(); Iter != c3.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //remove c3.remove(30) ; std::cout << "c3, after removing all occurences of 30 = " ; for(Iter = c3.begin(); Iter != c3.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; c2.insert(c2.begin(), 4, 10) ; //remove_if c2.remove_if(std::bind2nd(std::not_equal_to<int>(), 10)) ; std::cout << "c2, after removing all elements which are not equal to 10 = " ; for(Iter = c2.begin(); Iter != c2.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; c2.insert(c2.begin(), 35) ; //sort c3.sort() ; std::cout << "c3, after sorting = " ; for(Iter = c3.begin(); Iter != c3.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //unique c2.unique() ; std::cout << "c2, after removing duplicates = " ; for(Iter = c2.begin(); Iter != c2.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //splice c3.splice(c3.end(), c2) ; c3.splice(c3.end(), c4, c4.begin()) ; c3.splice(c3.end(), c1, c1.begin(), c1.end()) ; c3.sort() ; c3.unique() ; std::cout << "c3 = " ; for(Iter = c3.begin(); Iter != c3.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; std::cout << "c2 = " ; for(Iter = c2.begin(); Iter != c2.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; std::cout << "c1 = " ; for(Iter = c1.begin(); Iter != c1.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; return 0 ; }
c4 (using begin, end) = 0, 1, 2, 3, 4, c4 (using rbegin, rend) = 4, 3, 2, 1, 0, last element in c2 = 4 first element in c2 = 0 number of elements in c2 = 5 max number of elements c2 can hold using current allocator = 1073741823 c2 is now empty number of elements in c2 = 10 last element in c2 = 30 first element in c2 = 30 first element in c2 = 25 last element in c2 = 30 first element in c2 = 30 number of elements in c3 = 9 last element in c3 = 30 first element in c3 = 30 c1 = 20, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, c3, after reversing = 4, 3, 2, 1, 0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 10, 10, 10, 10, c3, after removing all occurences of 30 = 4, 3, 2, 1, 0, 10, 10, 10, 10, c2, after removing all elements which are not equal to 10 = 10, 10, 10, 10, c3, after sorting = 0, 1, 2, 3, 4, 10, 10, 10, 10, c2, after removing duplicates = 35, 10, c3 = 0, 1, 2, 3, 4, 10, 20, 35, c2 = c1 =