⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter8-42.cpp

📁 C++STL程序员开发指南
💻 CPP
字号:
//文件名:CHAPTER8-42.cpp
#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 ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -