zoukankan      html  css  js  c++  java
  • c++stack类的用法

    官方解释:

    LIFO stack

    Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.

    stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.

    The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:

    • empty
    • size
    • back
    • push_back
    • pop_back


    The standard container classes vectordeque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.

    常用成员函数:

     emplace代码实现:

    // stack::emplace
    #include <iostream>       // std::cin, std::cout
    #include <stack>          // std::stack
    #include <string>         // std::string, std::getline(string)
    
    int main ()
    {
      std::stack<std::string> mystack;
    
      mystack.emplace ("First sentence");
      mystack.emplace ("Second sentence");
    
      std::cout << "mystack contains:
    ";
      while (!mystack.empty())
      {
        std::cout << mystack.top() << '
    ';
        mystack.pop();
      }
    
      return 0;
    }

    结果:

    mystack contains:
    Second sentence
    First sentence

    swap代码实现:

     1 // stack::swap
     2 #include <iostream>       // std::cout
     3 #include <stack>          // std::stack
     4 
     5 int main ()
     6 {
     7   std::stack<int> foo,bar;
     8   foo.push (10); foo.push(20); foo.push(30);
     9   bar.push (111); bar.push(222);
    10 
    11   foo.swap(bar);
    12 
    13   std::cout << "size of foo: " << foo.size() << '
    ';
    14   std::cout << "size of bar: " << bar.size() << '
    ';
    15 
    16   return 0;
    17 }

    结果:

    size of foo: 2
    size of bar: 3

    一些思考:emplace跟插入有什么区别呢?

    当调用push或insert成员函数时,我们将元素类型的对象传递给它们,这些对象被拷贝到容器中。而当我们调用一个emplace成员函数时,则是将参数传递给元素类型的构造函数。emplace成员使用这些参数在容器管理的内存空间中直接构造元素。

    emplace函数的参数根据元素类型而变化,参数必须与元素类型的构造函数相匹配。emplace函数在容器中直接构造元素。传递给emplace函数的参数必须与元素类型的构造函数相匹配。

    参考:https://blog.csdn.net/fengbingchun/article/details/78670376

  • 相关阅读:
    percona-toolkit 之 【pt-heartbeat】说明
    sql-索引的作用(超详细)
    SqlServer2005 海量数据 数据表分区解决难题
    怎样玩转千万级别的数据(表分区)
    SQL数据缓存依赖总结
    关于缓存中Cookie,Session,Cache的使用
    Anti-Forgery Request Recipes For ASP.NET MVC And AJAX
    jquery 父、子页面之间页面元素的获取,方法的调用
    win7系统部分软件显示乱码怎么办
    转:mvc 当中 [ValidateAntiForgeryToken] 的作用
  • 原文地址:https://www.cnblogs.com/dmndxld/p/10713128.html
Copyright © 2011-2022 走看看