c++ STL 三 List Stack queue

list

  1.vector容量會自動比實際大小多相,  list對於vector 不浪費資源      3.不支持隨機訪問的容器不能使用系統提供的算法,列如排序

  2.vector連續的可以隨機訪問,  list鏈表實現, 變化的空間時間消耗較大,不能隨機訪問           

構造函數

  list<int>  L (10,5)  放入10個5         list<int> L2(L.begin() , L.end() )

添加刪除

      尾部插入: push_back( 10 )             頭部插入:  push_front( 5)          尾部刪除: pop_back()       頭部刪除:  pop_front( 10)   

      索引1後面插入10:   insert( 1,10)     索引1後面插入2個10:   insert( 1,2,10)             clear()刪除所有   

      L.remove( 10)   刪除 容器中與10相同的數值

     返回第一個,最後一個元素:    L.front()   L.back()              size()    empty()

反轉

     L.reverse()   將鏈表反轉    

 排序

    list 內部提供的排序       L.sort( ) 默認從小到大排序

bool Mycopmare(int value ,int value2)
{
	return value > value2;            }
L.sort(Mycopmare);

 

Stack  先進後出 


	stack<int> s;
	s.push(10);
	s.push(20);
	s.push(30);
	while (!s.empty())
	{
		cout << s.top() << endl;   30 20 10
		s.pop();
	}

queue先進先出

        queue<int> s;
	s.push(10);
	s.push(20);
	s.push(30);
	while (!s.empty())
	{
		cout << "隊頭" << s.front() << endl;
		cout << "隊尾" << s.back() << endl;
		s.pop();
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章