简体   繁体   English

关于std :: list C ++的问题

[英]Questions about std::list C++

I'm new to the whole stl business and I have one question. 我是整个Stl业务的新手,我有一个问题。 Before using stl my list nodes would be like this : 在使用stl之前,我的列表节点将是这样的:

Class node
{
    int duration;
    string name;
    node * next;
    node * previous;
};

So my question is that when I m about to use stl do i need the node * next and node * previous or they are completely useless since std::list makes ammend for them with their own next and previous functions? 所以我的问题是,当我要使用stl时,我是否需要node * nextnode * previous否则它们完全没有用,因为std :: list用它们自己的next和previous函数来补偿它们?

And one more question, do you guys prefer using std::list or making your own? 还有一个问题,你们喜欢使用std :: list还是自己制作? Because this is gonna be my first time using std::list and Im quite buffled. 因为这将是我第一次使用std :: list和Im,我对此感到非常困惑。

Thanks for your time. 谢谢你的时间。

No you don't need next and previous , std::list implements internally the whole linked list mechanism. 不,您不需要nextpreviousstd::list内部实现了整个链表机制。 just: 只是:

Class node {
  int duration;
  string name;
};

and define your list as: 并将您的列表定义为:

std::list<node> myList;

Even better use a std::vector instead of std::list which outperforms in most situations the std::list as it renders more cache friendly. 甚至最好使用std::vector而不是std::list ,因为它在大多数情况下都优于std::list因为它使缓存更加友好。

std::vector<node> myVec;

Another option would be to use a std::pair instead of defining your own struct : 另一种选择是使用std::pair而不是定义自己的struct

std::list<std::pair<int, std::string>> myList;

However, use of std::pair here is cleary subjective. 但是,这里使用std::pair很明显是主观的。 I find it more handy to use std::pair instead of defining struct s with two member variables, due to the fact that I can see the element type of my STL data-structure at the place where is defined. 我发现使用std::pair而不是使用两个成员变量定义struct更方便,因为我可以在定义的位置看到STL数据结构的元素类型。

As 101010 tells, that's the case. 正如101010所说的那样。 Also remember the performance of stl list is good since the allocator they use for those is optimized for memory management. 还请记住,stl list的性能很好,因为用于它们的分配器针对内存管理进行了优化。

The classical "add of nodes" routine is mostly implemented as a single allocation (with new (C++) or malloc (C)). 经典的“节点添加”例程通常实现为单个分配(使用new(C ++)或malloc(C))。 Nevertheless you can improve that through static arrays or laying data on vectors and pointing to them. 不过,您可以通过静态数组或将数据放在矢量上并指向它们来改善此问题。 I'm just telling in case you want to build something your own and compare performance. 我只是在告诉您是否要构建自己的东西并比较性能。

Also let me note that you are using a double linked list. 另外请注意,您正在使用双向链接列表。 In many cases you only need one iterator per block stl has the forward_list container. 在许多情况下,每个块仅需要一个迭代器,而stl具有forward_list容器。

No, your elements of a std::list do not need to have next and previous members. 不,您的std::list元素不需要具有nextprevious成员。 std::list takes care of such bookkeeping internally. std::list内部负责此类记账。

Generally, I will use a standard container rather than rolling my own, unless I have some particular requirements that the standard containers really don't meet. 通常,除非我对标准容器确实不能满足某些特殊要求,否则我将使用标准容器而不是自己动手使用。 If std::list is the one that is best suited - it often isn't, but is sometimes - I use that. 如果std::list是最合适的-通常不是,但是有时-我使用它。

Although there is some learning value in trying to create your own containers, that sort of thing is not productive in the long run. 尽管尝试创建自己的容器有一定的学习价值,但从长远来看,这种事情是无济于事的。 The standard containers - assuming a quality implementation - are better than most people can do on their own. 标准容器(假设实施质量很高)比大多数人自己能做的更好。

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

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