简体   繁体   English

C ++:STL容器适配器

[英]C++ : STL Container adapters

In the below code, 'Second' is initialised to the copy of deque and 'fourth' by using underlying container. 在下面的代码中,使用基础容器将“第二”初始化为双端队列和“第四”的副本。 I feel glad if someone can explain me, When to initialize to the copy of a container and when to use an underlying container. 如果有人可以向我解释,何时初始化为容器的副本以及何时使用基础容器,我感到很高兴。

#include <iostream>       // std::cout
#include <deque>          // std::deque
#include <list>           // std::list
#include <queue>          // std::queue

int main ()
{
  std::deque<int> mydeck (3,100);        // deque with 3 elements
  std::list<int> mylist (2,200);         // list with 2 elements

  std::queue<int> first;                 // empty queue
  std::queue<int> second (mydeck);       // queue initialized to copy of deque

  std::queue<int,std::list<int> > third; 
  std::queue<int,std::list<int> > fourth (mylist);

  std::cout << "size of first: " << first.size() << '\n';
  std::cout << "size of second: " << second.size() << '\n';
  std::cout << "size of third: " << third.size() << '\n';
  std::cout << "size of fourth: " << fourth.size() << '\n';

  return 0;
}

When to initialize to the copy of a container and when to use an underlying container? 何时初始化为容器的副本以及何时使用基础容器?

Both of these... 这两个...

std::queue<int> second(mydeck);
std::queue<int,std::list<int> > fourth(mylist);

...construct and initialise a queue by copying elements from the container specified as constructor argument (ie mydeck and mylist respectively). ...通过复制指定为构造函数参数的容器中的元素来构造和初始化队列(分别是mydeckmylist )。

If you mean to ask why the second specifies a second template argument of std::list<int> , that's because std::queue can store the data in any container that provides the API functions it expects. 如果要问为什么第二个参数指定第二个模板参数std::list<int> ,那是因为std::queue可以将数据存储在提供期望的API函数的任何容器中。 From cppreference , that second template parameter is: cppreference中 ,第二个模板参数为:

Container - The type of the underlying container to use to store the elements. 容器 -用于存储元素的基础容器的类型。 The container must satisfy the requirements of SequenceContainer . 容器必须满足SequenceContainer的要求。 Additionally, it must provide the following functions with the usual semantics: 此外,它必须提供具有通常语义的以下功能:

    back()
    front()
    push_back()
    pop_front() 

The standard containers std::deque and std::list satisfy these requirements. 标准容器std :: deque和std :: list满足这些要求。

Of these, I'd guess that std::list would typically be more efficient for one or a very few elements (exactly where the cutoff is depends on object size, memory library performance characteristics, CPU cache sizes, system load etc. - it gets complicated), then std::deque will have better average performance and memory usage for larger numbers of elements (with fewer but larger dynamic memory allocations/deallocations). 其中,我猜想std::list通常对于一个或很少几个元素会更高效(确切的说,临界值取决于对象大小,内存库性能特征,CPU缓存大小,系统负载等。-变得复杂),然后std::deque对于大量元素(具有更少但更大的动态内存分配/释放)将具有更好的平均性能和内存使用率。 But even an educated guess can go badly wrong for some specific use cases - if you care enough to consider tuning this, you should measure performance with each candidate container, and your actual data and usage, to inform your decision. 但是对于某些特定的用例,即使是经过深思熟虑的猜测也可能会犯下严重的错误-如果您足够关心以进行调整,则应该使用每个候选容器以及您的实际数据和使用情况来衡量性能,以做出决定。 Having the container be a template parameter allows the programmer to choose what's best for their needs. 将容器作为模板参数可以使程序员选择最适合其需求的东西。

The parameter also has a default... 该参数还具有默认值...

template <class T, class Container = std::deque<T>> class queue;

...so you only need to explicit specify a container if you're not happy to have it use a std::deque . ...因此,如果您不愿意使用std::deque则只需显式指定一个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM