C++ 中是否有链表预定义库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a linked list predefined library in C++?
提问by user69514
Is there a linked list in C++ that I could just #include? Or do I need to create my own if I want to use one?
C++ 中有一个链表可以让我#include 吗?或者如果我想使用一个,我需要创建我自己的吗?
回答by GManNickG
As daniel notes, yes, std::list
. Usage would be:
#include <list>
// ...
std::list<int> listOfInts;
listOfInts.push_back(1);
// ...
And so on.
等等。
You can find a complete list of STL classes here. The section you're after is 3.2, Container classes. Another useful reference of the C++ Standard Library is here.
回答by Idan K
#include <list>
回答by manugupt1
In c++ we have the STL, Standard Template Libraries which do contain a a lot of implemantations of popular data structures and algorithms like stacks, queues, linked listsand popular searching and sorting algorithms even.....
在 C++ 中,我们有 STL,标准模板库,它包含大量流行数据结构和算法的实现,如堆栈、队列、链表和流行的搜索和排序算法,甚至......
As already told by daniel you can include it by #include< list>
正如 daniel 已经告诉你的,你可以通过 #include< list> 包含它
回答by HostileFork says dont trust SE
For the sake of completing awareness of "common link lists that are out there", the Qt library defines its own QLinkedListas part of its container classes (QMap, QString, etc.)
为了完成对“外面的公共链接列表”的认识,Qt 库定义了自己的QLinkedList作为其容器类(QMap、QString 等)的一部分。
They support both standard iterators, as well as Java-Style Iterators, which have an easy-to-use syntax:
它们支持标准迭代器和Java-Style Iterators,它们具有易于使用的语法:
QLinkedList<QString> list;
list << "A" << "B" << "C" << "D";
QListIterator<QString> i(list);
while (i.hasNext())
qDebug() << i.next();
Update:I posted this answer originally in 2009, to bring attention to the Qt class. In the post-C++11 world, with things like range-based
for
, you can generally get even better syntax than the Java-Style iterators...without sacrificing performance to do so.So while this was probably worth bringing up for completeness when I posted, today I'd not be likely to mention it. Unless you have some strange reason not to, just use the standard library's singly linked list (
std::forward_list
) or doubly linked list (std::list
).
更新:我最初在 2009 年发布了这个答案,以引起对 Qt 类的关注。在 C++11 之后的世界中,使用range-based 之类的
for
东西,您通常可以获得比 Java 样式迭代器更好的语法......而不会牺牲性能。因此,虽然在我发布时为了完整性可能值得提出这一点,但今天我不太可能提及它。除非您有一些奇怪的理由不这样做,否则只需使用标准库的单向链表 (
std::forward_list
) 或双向链表 (std::list
)。
回答by D.F
I know this question is quite old, anyway maybe it worth to update it as it's up on the search hits when you look for STD and linked lists:
我知道这个问题已经很老了,无论如何也许值得更新它,因为当您查找 STD 和链接列表时,它会出现在搜索结果中:
In addition to what already said by the others (use std::listfor a double-linked list), for the most common use case of a single-linked list, std::forward_listshould be preferred, because it's optimized for single-linked lists specifically.
除了其他人已经说过的(将std::list用于双链表)之外,对于单链表的最常见用例,应该首选std::forward_list,因为它针对单链表进行了优化。特别是链表。