您的位置:首页 > 产品设计 > UI/UE

常用STL类的使用

2016-07-14 00:16 323 查看
栈 satck
队列 queue
优先队列 priority_queue
1.栈(stack)

 (1)定义:只允许在一端插入和删除的线性表;

 (2)栈顶(top):允许插入和删除的一端;

 (3)特点:后进先出(LIFO)。

 (4)引用头文件:#include<stack>

 (5)成员函数:

          empty  测试stack是否为空;

          pop 从stack的顶部移除元素;

          push 将元素添加到stack顶部;

          size  返回stack中的元素个数;

          top 返回stack顶部元素。

   (6)使用例子:

       #include<iostream>

       #include<stack>

       using namespace std;

       int main()

       {
            int i;
            int num;
            stack<int> st;      //声明一个存储int型的栈,stack是标准库中的一个容器适配器,

                                      //是个类模板,使用的时候需要实例化,int是模板实参。
            st.push(10);   //将元素添加到stack顶部;
            st.push(20);
            st.push(30);

            i = st.top();  //返回stack顶部元素
            cout << "The element at the top of the stack is:" << i << endl;

            num = st.size();   //返回stack中的元素个数
            cout << "The size of the stack is:" << num << endl;

            st.pop();     //从stack的顶部移除元素
            i = st.top();
            cout << "The element at the top of the stack is:" << i << endl;
            return 0;

      }

2.队列(queue)

  (1)定义:队列是只允许在一端删除,在另一端插入的线性表;

            队头(front):允许删除的一端;

            队尾(rear):允许插入的一端;

  (2)特性:先进先出(FIFO,First In First Out);

  (3)引用头文件:#include<queue>

  (4)成员函数:

           back():  Returns a reference to the last and most recently added element

                       at the back of the queue.(返回队列中最后和最近添加的元素)

           empty():  Tests if the queue is empty.(测试队列是否为空)

           front():  Returns a reference to the first element at the front of the queue.(返回队列中的第一个元素)

           pop():  Removes an element from the front of the queue.(删除队列第一个元素)

           push():  Adds an element to the back of the queue.(将元素插入到队尾)

           size():  Returns the number of the element in the queue.(返回队列的元素个数)

  (5)使用例子:

           #include<iostream>

           #include<queue>

           using namespace std;

           int main()

           {
                 int i;
                 int num;
                 queue<int> st;
                 st.push(10);   //将元素插入到队尾
                 st.push(20);
                 st.push(30);

                 i = st.front();  //返回队列中的第一个元素
                 cout << "the element at the front of the queue is:" << i << endl;

                 i = st.back();   //返回队列中最后和最近添加的元素
                 cout << "the element at the back of the queue is:" << i << endl;

                 num = st.size();  //返回队列的元素个数
                 cout << "the number of the element in the queue is:" << i << endl;

                 st.pop();  //删除队列第一个元素
                 i = st.front();
                 cout << "the element at the front of the queue is:" << i << endl;
                 if (!st.empty())  //测试队列是否为空
                      cout << "The queue is not empty!" << endl;
                 else
                      cout << "The queue is empty!" << endl;
                 return 0;

           }

3.优先队列(priority_queue)

 (1)定义:是一种最高级先出 (largest-in,first-out)的数据结构;  

 (2)特点:最高级先出 (largest-in,first-out);

 (3)引用头文件:#include<queue>;

 (4)成员函数:

          empty  测试priority_queue是否为空;

          pop  从priority_queue顶部位置移除最大的元素。

          push  将元素添加到基于元素优先级的优先队列中;

          size  返回priority_queue的元素个数;

          top  返回优先队列中的最大元素。

 (5)元素优先级的确定:默认的元素比较器是less<T>,即元素值大则优先;

 (6)使用例子:

         #include<iostream>

         #include<queue>

         using namespace std;

         int main()

         {
      priority_queue<int> st;  //声明一个存储int型的优先队列

 
      st.push(30);    //将元素添加到基于元素优先级的优先队列中;
      st.push(10);
      st.push(50);
      st.push(20);
      st.push(40);
      while (!st.empty())   //当优先队列st不为空时
      {
    cout << st.top() << " ";   //返回优先队列中的最大元素;
    st.pop();    //从priority_queue顶部位置移除最大的元素。
      }
      cout << endl;
      return 0;

        }

(7)元素优先级的改变:改写元素比较器

         最小优先队列的构建:


       
 #include<iostream>

         #include<queue>

         using namespace std;

         struct Node  //结点的结构体定义

         {
      int x;
      Node(int i)   //构造函数

              {

              x = i;
      }

         };

        bool operator <(const Node &a, const Node &b)   //改写元素比较器,即元素值小优先

        {
        if (a.x > b.x)
     return true;
        else
     return false;

        }

       int main()

       {
     Node n(0); //声明结点对象,并初始化
     priority_queue<Node> p;  //声明优先队列对象

    p.push(Node(4));
    p.push(Node(9));
    p.push(Node(3));
   while (!p.empty())
   {
 n = p.top();
 cout << n.x << " ";
 p.pop();
    }
   cout << endl;
   return 0;

      }

   结果显示:

    

   

          

            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息