当前位置: 代码迷 >> 综合 >> c++ STL 三 List Stack queue
  详细解决方案

c++ STL 三 List Stack queue

热度:98   发布时间:2023-12-24 03:37:02.0

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 10s.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();}

 

  相关解决方案