STL有什么优点?

时间:2023-01-14 09:26:37

I am a Java developer trying to learn C++. I have many times read on the internet (including Stack Overflow) that STL is the best collections library that you can get in any language. (Sorry, I do not have any citations for that)

我是一个正在学习c++的Java开发人员。我在因特网上读过很多次(包括栈溢出),STL是任何语言中都能得到的最好的集合库。(对不起,我没有任何引用)

However after studying some STL, I am really failing to see what makes STL so special. Would you please shed some light on what sets STL apart from the collection libraries of other languages and make it the best collection library?

但是在学习了一些STL之后,我真的不明白STL为什么这么特别。请您介绍一下STL与其他语言的集合库的区别,使它成为最好的集合库,好吗?

14 个解决方案

#1


23  

What is so great about the STL ?

STL有什么了不起的?

The STL is great in that it was conceived very early and yet succeeded in using C++ generic programming paradigm quite efficiently.

STL的伟大之处在于它是在很早的时候就被构想出来的,但是它成功地有效地使用了c++通用编程范式。

It separated efficiently the data structures: vector, map, ... and the algorithms to operate on them copy, transform, ... taking advantage of templates to do so.

它有效地分离了数据结构:向量、映射、……对它们进行操作的算法复制,转换,……利用模板这样做。

It neatly decoupled concerns and provided generic containers with hooks of customization (Comparator and Allocator template parameters).

它完全解耦了关注点,并提供了具有自定义钩子的通用容器(比较器和分配器模板参数)。

The result is very elegant (DRY principle) and very efficient thanks to compiler optimizations so that hand-generated algorithms for a given container are unlikely to do better.

由于编译器的优化,结果是非常优雅的(DRY原则),非常高效,因此,为给定容器手工生成的算法不太可能做得更好。

It also means that it is easily extensible: you can create your own container with the interface you wish, as long as it exposes STL-compliant iterators you'll be able to use the STL algorithms with it!

这也意味着它很容易扩展:您可以使用您希望的接口创建您自己的容器,只要它公开兼容STL的迭代器,您就可以使用STL算法!

And thanks to the use of traits, you can even apply the algorithms on C-array through plain pointers! Talk about backward compatibility!

多亏了特性的使用,你甚至可以通过纯指针将算法应用到C-array上!谈论向后兼容性!

However, it could (perhaps) have been better...

然而,它本可以(也许)更好……

What is not so great about the STL ?

STL有什么不好?

It really pisses me off that one always have to use the iterators, I'd really stand for being able to write: std::foreach(myVector, [](int x) { return x+1;}); because face it, most of the times you want to iterate over the whole of the container...

让我很生气的是,人们总是要使用迭代器,我真的希望能够写出:std::foreach(myVector, [](int x) {return x+1;});因为面对它,大多数时候你想要遍历整个容器……

But what's worse is that because of that:

但更糟糕的是,正因为如此:

set<int> mySet = /**/;

set<int>::const_iterator it = std::find(mySet.begin(), mySet.end(), 1005); // [1]
set<int>::const_iterator it = mySet.find(1005); // [2]

[1] and [2] are carried out completely differently, resulting in [1] having O(n) complexity while [2] has O(log n) complexity! Here the problem is that the iterators abstract too much.

[1]和[2]完全不同,导致[1]具有O(n)复杂度,[2]具有O(log n)复杂度!这里的问题是迭代器抽象得太多。

I don't mean that iterators are not worthy, I just mean that providing an interface exclusively in terms of iterators was a poor choice.

我并不是说迭代器没有价值,我只是说,仅从迭代器的角度提供接口是一个糟糕的选择。

I much prefer myself the idea of views over containers, for example check out what has been done with Boost.MPL. With a view you manipulate your container with a (lazy) layer of transformation. It makes for very efficient structures that allows you to filter out some elements, transform others etc...

与容器相比,我更喜欢视图的概念,例如,看看Boost.MPL做了什么。您可以使用一个(懒惰的)转换层来操作您的容器。它使非常有效的结构,允许你过滤一些元素,转换其他的……

Combining views and concept checking ideas would, I think, produce a much better interface for STL algorithms (and solve this find, lower_bound, upper_bound, equal_range issue).

结合视图和概念检查思想,我认为可以为STL算法生成更好的接口(并解决这个查找、下限、上限、equal_range问题)。

It would also avoid common mistakes of using ill-defined ranges of iterators and the undefined behavior that result of it...

它还可以避免使用定义不明确的迭代器范围和由此产生的未定义行为的常见错误……

#2


26  

It's not so much that it's "great" or "the best collections library that you can get in *any* language", but it does have a different philosophy to many other languages.

这并不是说它是“伟大的”或“最好的集合库,你可以用任何一种语言”,但它确实对许多其他语言有不同的哲学。

In particular, the standard C++ library uses a generic programming paradigm, rather than an object-oriented paradigm that is common in languages like Java and C#. That is, you have a "generic" definition of what an iterator should be, and then you can implement the function for_each or sort or max_element that takes any class that implements the iterator pattern, without actually having to inherit from some base "Iterator" interface or whatever.

特别是,标准c++库使用通用编程范式,而不是Java和c#等语言中常见的面向对象范式。也就是说,您有一个迭代器应该是什么的“通用”定义,然后您可以实现函数for_each或sort或max_element,该函数接受实现迭代器模式的任何类,而不必实际继承某个基本的“迭代器”接口或其他东西。

#3


20  

What I love about the STL is how robust it is. It is easy to extend it. Some complain that it's small, missing many common algorithms or iterators. But this is precisely when you see how easy it is to add in the missing components you need. Not only that, but small is beautiful: you have about 60 algorithms, a handful of containers and a handful of iterators; but the functionality is in the order of the product of these. The interfaces of the containers remain small and simple.

我喜欢STL的地方是它的健壮性。扩展它很容易。有些人抱怨说它很小,缺少许多常用的算法或迭代器。但是,当您看到添加所需的组件是多么容易时,您就会看到这一点。不仅如此,“小”也是美丽的:您有大约60种算法、一些容器和一些迭代器;但是功能是按照这些产品的顺序排列的。容器的接口仍然是小而简单的。

Because it's fashion to write small, simple, modular algorithms it gets easier to spot bugs and holes in your components. Yet, at the same time, as simple as the algorithms and iterators are, they're extraordinarily robust: your algorithms will work with many existing and yet-to-be-written iterators and your iterators work with many existing and yet-to-be-written algorithms.

因为编写小型、简单、模块化的算法是一种时尚,因此可以更容易地发现组件中的bug和漏洞。然而,与此同时,尽管算法和迭代器非常简单,但它们非常健壮:您的算法将与许多现有的和尚未编写的迭代器一起工作,您的迭代器与许多现有的和尚未编写的算法一起工作。

I also love how simple the STL is. You have containers, you have iterators and you have algorithms. That's it (I'm lying here, but this is what it takes to get comfortable with the library). You can mix different algorithms with different iterators with different containers. True, some of these have constraints that forbid them from working with others, but in general there's a lot to play with.

我也喜欢STL有多简单。有容器,有迭代器,有算法。就这样(我躺在这里,但这就是让图书馆感到舒适的地方)。您可以将不同的算法与不同容器的不同迭代器混合使用。的确,其中有些约束禁止它们与其他约束一起工作,但总的来说,还有很多要处理。

Niklaus Wirth said that a program is algorithms plus data-structures. That's exactly what the STL is about. If Ruby and Python are string superheros, then C++ and the STL are an algorithms-and-containers superhero.

Niklaus Wirth说,程序就是算法加上数据结构。这就是STL的意义所在。如果Ruby和Python是字符串超级英雄,那么c++和STL则是算法和容器超级英雄。

#4


13  

STL's containers are nice, but they're not much different than you'll find in other programming languages. What makes the STL containers useful is that they mesh beautifully with algorithms. The flexibility provided by the standard algorithms is unmatched in other programming languages.

STL的容器是很好的,但是它们与其他编程语言的容器没有什么不同。STL容器之所以有用,是因为它们与算法配合得很好。标准算法提供的灵活性在其他编程语言中是无与伦比的。

Without the algorithms, the containers are just that. Containers. Nothing special in particular.

如果没有算法,容器就是这样。容器。没什么特别的。

Now if you're talking about container libraries for C++ only, it is unlikely you will find libraries as well used and tested as those provided by STL if nothing else because they are standard.

现在,如果您只讨论c++的容器库,那么您不太可能找到与STL提供的库一样使用和测试良好的库,因为它们是标准的。

#5


13  

The STL works beautifully with built-in types. A std::array<int, 5> is exactly that -- an array of 5 ints, which consumes 20 bytes on a 32 bit platform.

STL与内置类型配合得很好。一个std::array 就是——一个5 ints的数组,在32位平台上消耗20字节。 ,>

java.util.Arrays.asList(1, 2, 3, 4, 5), on the other hand, returns a reference to an object containing a reference to an array containing references to Integer objects containing ints. Yes, that's 3 levels of indirection, and I don't dare predict how many bytes that consumes ;)

java.util.Arrays。另一方面,asList(1,2,3,4,5)返回对对象的引用,该对象包含对包含int的整数对象的引用的数组的引用。是的,这是3个层次的间接指令,我不敢预测会消耗多少字节;

#6


8  

This is not a direct answer, but as you're coming from Java I'd like to point this out. By comparison to Java equivalents, STL is really fast.

这不是一个直接的答案,但是当你从Java中来的时候,我想指出这一点。与Java等价物相比,STL非常快。

I did find this page, showing some performance comparisons. Generally Java people are very touchy when it comes to performance conversations, and will claim that all kinds of advances are occurring all the time. However similar advances are also occurring in C/C++ compilers.

我确实找到了这个页面,显示了一些性能比较。一般来说,Java人员在谈到性能会话时非常敏感,他们会声称各种各样的改进一直在发生。然而,类似的进展也出现在C/ c++编译器中。

#7


7  

Keep in mind that STL is actually quite old now, so other, newer libraries may have specific advantages. Given the age, its' popularity is a testament to how good the original design was.

请记住,STL实际上已经相当老了,所以其他的新库可能具有特定的优势。考虑到时代,它的流行证明了最初的设计是多么的好。

There are four main reasons why I'd say that STL is (still) awesome:

我认为STL(仍然)很棒的原因有四个:

Speed STL uses C++ templates, which means that the compiler generates code that is specifically tailored to your use of the library. For example, map will automagically generate a new class to implement a map collection of 'key' type to 'value' type. There is no runtime overhead where the library tries to work out how to efficiently store 'key' and 'value' - this is done at compile time. Due to the elegant design some operations on some types will compile down to single assembly instructions (e.g. increment integer-based iterator).

Speed STL使用c++模板,这意味着编译器生成专门为您使用该库而定制的代码。例如,map将自动生成一个新类来实现“key”类型到“value”类型的映射集合。没有运行时开销,库试图找出如何有效地存储“key”和“value”——这是在编译时完成的。由于设计精美,对某些类型的一些操作将编译成单个的汇编指令(例如,递增整数迭代器)。

Efficiency The collections classes have a notion of 'allocators', which you can either provide yourself or use the library-provided ones which allocate only enough storage to store your data. There is no padding nor wastage. Where a built-in type can be stored more efficiently, there are specializations to handle these cases optimally, e.g. vector of bool is handled as a bitfield.

集合类有一个“分配器”的概念,您可以自己提供它,也可以使用图书馆提供的那些只分配足够的存储空间来存储数据的。没有填充和浪费。在可以更有效地存储内置类型的地方,有专门的方法来最优地处理这些情况,例如,bool的向量被处理为位域。

Exensibility You can use the Containers (collection classes), Algorithms and Functions provided in the STL on any type that is suitable. If your type can be compared, you can put it into a container. If it goes into a container, it can be sorted, searched, compared. If you provide a function like 'bool Predicate(MyType)', it can be filtered, etc.

您可以使用STL中提供的容器(集合类)、算法和函数来描述任何适合的类型。如果您的类型可以进行比较,您可以将其放入一个容器中。如果它进入一个容器,它可以被排序,搜索,比较。如果您提供一个类似“bool Predicate(MyType)”的函数,它可以被过滤,等等。

Elegance Other libraries/frameworks have to implement the Sort()/Find()/Reverse() methods on each type of collection. STL implements these as separate algorithms that take iterators of whatever collection you are using and operate blindly on that collection. The algorithms don't care whether you're using a Vector, List, Deque, Stack, Bag, Map - they just work.

优雅的其他库/框架必须在每种类型的集合上实现Sort()/Find()/反向()方法。STL将它们作为单独的算法来实现,这些算法使用您正在使用的任何集合的迭代器,并盲目地对该集合进行操作。算法并不关心你是否在使用向量,列表,Deque,堆栈,包,映射——它们只是起作用。

#8


3  

Well, that is somewhat of a bold claim... perhaps in C++0x when it finally gets a hash map (in the form of std::unordered_map), it can make that claim, but in its current state, well, I don't buy that.

嗯,这有点大胆的说法……也许在c++ 0x中,当它最终得到一个散列映射(以std::unordered_map的形式)时,它可以做出这样的声明,但是在当前状态下,我不买它。

I can tell you, though, some cool things about it, namely that it uses templates rather than inheritance to achieve its level of flexibility and generality. This has both advantages and disadvantages; a disadvantage is that lots of code gets duplicated by the compiler, and any sort of dynamic runtime typing is very hard to achieve; however, a key advantage is that it is incredibly quick. Because each template specialization is really its own separate class generated by the compiler, it can be highly optimized for that class. Additionally, many of the STL algorithms that operate on STL containers have general definitions, but have specializations for special cases that result in incredibly good performance.

但是,我可以告诉您一些关于它的很酷的事情,即它使用模板而不是继承来实现它的灵活性和通用性。这既有优点也有缺点;缺点是大量的代码被编译器复制,任何类型的动态运行时类型都很难实现;然而,它的一个关键优势是速度快得令人难以置信。因为每个模板专门化实际上都是由编译器生成的独立类,所以可以为该类进行高度优化。此外,许多在STL容器上操作的STL算法都有通用的定义,但是对特殊情况有专门的定义,这些特殊情况会导致非常好的性能。

#9


3  

STL gives you the pieces.

STL给你碎片。

Languages and their environments are built from smaller component pieces, sometimes via programming language constructs, sometimes via cut-and-paste. Some languages give you a sealed box - Java's collections, for instance. You can do what they allow, but woe betide you if you want to do something exotic with them.

语言及其环境是由较小的组件块构建的,有时通过编程语言结构构建,有时通过剪切-粘贴。有些语言给你一个封闭的盒子——比如Java的集合。你可以做他们允许做的事,但如果你想做一些异国的事情,你就会倒霉。

The STL gives you the pieces that the designers used to build its more advanced functionality. Directly exposing the iterators, algorithms, etc. give you an abstract but highly flexible way of recombining core data structures and manipulations in whatever way is suitable for solving your problem. While Java's design probably hits the 90-95% mark for what you need from data structures, the STL's flexibility raises it to maybe 99%, with the iterator abstraction meaning you're not completely on your own for the remaining 1%.

STL提供了设计人员用来构建更高级功能的部分。直接公开迭代器、算法等,可以提供一种抽象但高度灵活的方法,以适合解决问题的任何方式重新组合核心数据结构和操作。虽然Java的设计可能达到了数据结构所需的90-95%,但是STL的灵活性使它可能提高到99%,迭代器抽象意味着对于剩下的1%来说,您不能完全独立。

When you combine that with its speed and other extensibility and customizabiltiy features (allocators, traits, etc.), you have a quite excellent package. I don't know that I'd call it the best data structures package, but certainly a very good one.

当您将它与它的速度、其他可扩展性和定制特性(分配器、特性等)结合在一起时,您将得到一个非常优秀的包。我不知道我是否认为它是最好的数据结构包,但肯定是一个非常好的包。

Warning: percentages totally made up.

警告:百分比完全构成。

#10


2  

Unique because it

独特的,因为它

  • focuses on basic algorithms instead of providing ready-to-use solutions to specific application problems.
  • 关注基本的算法,而不是为特定的应用问题提供现成的解决方案。
  • uses unique C++ features to implement those algorithms.
  • 使用独特的c++特性来实现这些算法。

As for being best... There is a reason why the same approach wasn't (and probably won't) ever followed by any other language, including direct descendants like D.

是最好的…这就是为什么同样的方法没有(也可能不会)被任何其他语言采用,包括像D这样的直接后代。

#11


2  

The standard C++ library's approach to collections via iterators has come in for some constructive criticism recently. Andrei Alexandrescu, a notable C++ expert, has recently begun working on a new version of a language called D, and describes his experiences designing collections support for it in this article.

标准c++库通过迭代器处理集合的方法最近受到了一些建设性的批评。著名的c++专家安德烈·亚历山大最近开始开发一种名为D的语言的新版本,并在本文中描述了他设计集合支持的经验。

Personally I find it frustrating that this kind of excellent work is being put into yet another programming language that overlaps hugely with existing languages, and I've told him so! :) I'd like someone of his expertise to turn their hand to producing a collections library for the so-called "modern languages" that are already in widespread use, Java and C#, that has all the capabilities he thinks are required to be world-class: the notion of a forward-iterable range is already ubiquitous, but what about reverse iteration exposed in an efficient way? What about mutable collections? What about integrating all this smoothly with Linq? etc.

就我个人而言,我发现这种优秀的工作正在被投入到另一种与现有语言重叠的编程语言中,这让我感到很沮丧,我已经告诉过他了!:)我想让他的专长的人把他们的手产生集合库的所谓“现代语言”已经在广泛使用,Java和c#中,他认为,所有的功能需要世界级的:forward-iterable范围的概念已经无处不在,但反向迭代暴露在一个有效的方法呢?可变集合呢?如何将所有这些顺利地集成到Linq中呢?等。

Anyway, the point is: don't believe anyone who tells you that the standard C++ way is the holy grail, the best it could possibly be. It's just one way among many, and has at least one obvious drawback: the fact that in all the standard algorithms, a collection is specified by two separate iterators (begin and end) and hence is clumsy to compose operations on.

无论如何,关键是:不要相信那些告诉你标准c++方法是圣杯的人,它可能是最好的。它只是许多方法中的一种,并且至少有一个明显的缺点:在所有标准算法中,集合都是由两个独立的迭代器(开始和结束)指定的,因此编写操作很笨拙。

#12


2  

Obviously C++, C#, and Java can enter as many pissing contests as you want them to. The clue as to why the STL is at least somewhat great is that Java was initially designed and implemented without type-safe containers. Then Sun decided/realised people actually need them in a typed language, and added generics in 1.5.

显然,c++、c#和Java可以像您希望的那样输入许多pissing竞赛。关于STL至少有几分优秀的原因,可以看出Java最初是在没有类型安全容器的情况下设计和实现的。然后Sun决定/意识到人们实际上需要一种类型语言,并在1.5中添加了泛型。

You can compare the pros and cons of each, but as to which of the three languages has the "greatest" implementation of generic containers - that is solely a pissing contest. Greatest for what? In whose opinion? Each of them has the best libraries that the creators managed to come up with, subject to other constraints imposed by the languages. C++'s idea of generics doesn't work in Java, and type erasure would be sub-standard in typical C++ usage.

您可以对每种语言的优缺点进行比较,但是对于这三种语言中哪一种实现了泛型容器的“最大”实现——这完全是一场激烈的竞争。最大的为了什么?在谁的意见?它们中的每一个都有创建者设法找到的最好的库,受语言的其他限制。c++的泛型概念在Java中不起作用,在典型的c++使用中,类型擦除是不标准的。

#13


0  

The primary thing is, you can use templates to make using containers switch-in/switch-out, without having to resort to the horrendous mess that is Java's interfaces.

最主要的是,您可以使用模板使使用容器切换到/切换,而不必求助于Java接口的可怕混乱。

#14


0  

If you fail to see what usage the STL has, I recommend buying a book, "The C++ Programming Language" by Bjarne Stroustrup. It pretty much explains everything there is about C++ because he's the dude who created it.

如果您不知道STL有什么用途,我建议您买一本Bjarne Stroustrup的《c++编程语言》。它很好地解释了c++的一切,因为是他创造了c++。

#1


23  

What is so great about the STL ?

STL有什么了不起的?

The STL is great in that it was conceived very early and yet succeeded in using C++ generic programming paradigm quite efficiently.

STL的伟大之处在于它是在很早的时候就被构想出来的,但是它成功地有效地使用了c++通用编程范式。

It separated efficiently the data structures: vector, map, ... and the algorithms to operate on them copy, transform, ... taking advantage of templates to do so.

它有效地分离了数据结构:向量、映射、……对它们进行操作的算法复制,转换,……利用模板这样做。

It neatly decoupled concerns and provided generic containers with hooks of customization (Comparator and Allocator template parameters).

它完全解耦了关注点,并提供了具有自定义钩子的通用容器(比较器和分配器模板参数)。

The result is very elegant (DRY principle) and very efficient thanks to compiler optimizations so that hand-generated algorithms for a given container are unlikely to do better.

由于编译器的优化,结果是非常优雅的(DRY原则),非常高效,因此,为给定容器手工生成的算法不太可能做得更好。

It also means that it is easily extensible: you can create your own container with the interface you wish, as long as it exposes STL-compliant iterators you'll be able to use the STL algorithms with it!

这也意味着它很容易扩展:您可以使用您希望的接口创建您自己的容器,只要它公开兼容STL的迭代器,您就可以使用STL算法!

And thanks to the use of traits, you can even apply the algorithms on C-array through plain pointers! Talk about backward compatibility!

多亏了特性的使用,你甚至可以通过纯指针将算法应用到C-array上!谈论向后兼容性!

However, it could (perhaps) have been better...

然而,它本可以(也许)更好……

What is not so great about the STL ?

STL有什么不好?

It really pisses me off that one always have to use the iterators, I'd really stand for being able to write: std::foreach(myVector, [](int x) { return x+1;}); because face it, most of the times you want to iterate over the whole of the container...

让我很生气的是,人们总是要使用迭代器,我真的希望能够写出:std::foreach(myVector, [](int x) {return x+1;});因为面对它,大多数时候你想要遍历整个容器……

But what's worse is that because of that:

但更糟糕的是,正因为如此:

set<int> mySet = /**/;

set<int>::const_iterator it = std::find(mySet.begin(), mySet.end(), 1005); // [1]
set<int>::const_iterator it = mySet.find(1005); // [2]

[1] and [2] are carried out completely differently, resulting in [1] having O(n) complexity while [2] has O(log n) complexity! Here the problem is that the iterators abstract too much.

[1]和[2]完全不同,导致[1]具有O(n)复杂度,[2]具有O(log n)复杂度!这里的问题是迭代器抽象得太多。

I don't mean that iterators are not worthy, I just mean that providing an interface exclusively in terms of iterators was a poor choice.

我并不是说迭代器没有价值,我只是说,仅从迭代器的角度提供接口是一个糟糕的选择。

I much prefer myself the idea of views over containers, for example check out what has been done with Boost.MPL. With a view you manipulate your container with a (lazy) layer of transformation. It makes for very efficient structures that allows you to filter out some elements, transform others etc...

与容器相比,我更喜欢视图的概念,例如,看看Boost.MPL做了什么。您可以使用一个(懒惰的)转换层来操作您的容器。它使非常有效的结构,允许你过滤一些元素,转换其他的……

Combining views and concept checking ideas would, I think, produce a much better interface for STL algorithms (and solve this find, lower_bound, upper_bound, equal_range issue).

结合视图和概念检查思想,我认为可以为STL算法生成更好的接口(并解决这个查找、下限、上限、equal_range问题)。

It would also avoid common mistakes of using ill-defined ranges of iterators and the undefined behavior that result of it...

它还可以避免使用定义不明确的迭代器范围和由此产生的未定义行为的常见错误……

#2


26  

It's not so much that it's "great" or "the best collections library that you can get in *any* language", but it does have a different philosophy to many other languages.

这并不是说它是“伟大的”或“最好的集合库,你可以用任何一种语言”,但它确实对许多其他语言有不同的哲学。

In particular, the standard C++ library uses a generic programming paradigm, rather than an object-oriented paradigm that is common in languages like Java and C#. That is, you have a "generic" definition of what an iterator should be, and then you can implement the function for_each or sort or max_element that takes any class that implements the iterator pattern, without actually having to inherit from some base "Iterator" interface or whatever.

特别是,标准c++库使用通用编程范式,而不是Java和c#等语言中常见的面向对象范式。也就是说,您有一个迭代器应该是什么的“通用”定义,然后您可以实现函数for_each或sort或max_element,该函数接受实现迭代器模式的任何类,而不必实际继承某个基本的“迭代器”接口或其他东西。

#3


20  

What I love about the STL is how robust it is. It is easy to extend it. Some complain that it's small, missing many common algorithms or iterators. But this is precisely when you see how easy it is to add in the missing components you need. Not only that, but small is beautiful: you have about 60 algorithms, a handful of containers and a handful of iterators; but the functionality is in the order of the product of these. The interfaces of the containers remain small and simple.

我喜欢STL的地方是它的健壮性。扩展它很容易。有些人抱怨说它很小,缺少许多常用的算法或迭代器。但是,当您看到添加所需的组件是多么容易时,您就会看到这一点。不仅如此,“小”也是美丽的:您有大约60种算法、一些容器和一些迭代器;但是功能是按照这些产品的顺序排列的。容器的接口仍然是小而简单的。

Because it's fashion to write small, simple, modular algorithms it gets easier to spot bugs and holes in your components. Yet, at the same time, as simple as the algorithms and iterators are, they're extraordinarily robust: your algorithms will work with many existing and yet-to-be-written iterators and your iterators work with many existing and yet-to-be-written algorithms.

因为编写小型、简单、模块化的算法是一种时尚,因此可以更容易地发现组件中的bug和漏洞。然而,与此同时,尽管算法和迭代器非常简单,但它们非常健壮:您的算法将与许多现有的和尚未编写的迭代器一起工作,您的迭代器与许多现有的和尚未编写的算法一起工作。

I also love how simple the STL is. You have containers, you have iterators and you have algorithms. That's it (I'm lying here, but this is what it takes to get comfortable with the library). You can mix different algorithms with different iterators with different containers. True, some of these have constraints that forbid them from working with others, but in general there's a lot to play with.

我也喜欢STL有多简单。有容器,有迭代器,有算法。就这样(我躺在这里,但这就是让图书馆感到舒适的地方)。您可以将不同的算法与不同容器的不同迭代器混合使用。的确,其中有些约束禁止它们与其他约束一起工作,但总的来说,还有很多要处理。

Niklaus Wirth said that a program is algorithms plus data-structures. That's exactly what the STL is about. If Ruby and Python are string superheros, then C++ and the STL are an algorithms-and-containers superhero.

Niklaus Wirth说,程序就是算法加上数据结构。这就是STL的意义所在。如果Ruby和Python是字符串超级英雄,那么c++和STL则是算法和容器超级英雄。

#4


13  

STL's containers are nice, but they're not much different than you'll find in other programming languages. What makes the STL containers useful is that they mesh beautifully with algorithms. The flexibility provided by the standard algorithms is unmatched in other programming languages.

STL的容器是很好的,但是它们与其他编程语言的容器没有什么不同。STL容器之所以有用,是因为它们与算法配合得很好。标准算法提供的灵活性在其他编程语言中是无与伦比的。

Without the algorithms, the containers are just that. Containers. Nothing special in particular.

如果没有算法,容器就是这样。容器。没什么特别的。

Now if you're talking about container libraries for C++ only, it is unlikely you will find libraries as well used and tested as those provided by STL if nothing else because they are standard.

现在,如果您只讨论c++的容器库,那么您不太可能找到与STL提供的库一样使用和测试良好的库,因为它们是标准的。

#5


13  

The STL works beautifully with built-in types. A std::array<int, 5> is exactly that -- an array of 5 ints, which consumes 20 bytes on a 32 bit platform.

STL与内置类型配合得很好。一个std::array 就是——一个5 ints的数组,在32位平台上消耗20字节。 ,>

java.util.Arrays.asList(1, 2, 3, 4, 5), on the other hand, returns a reference to an object containing a reference to an array containing references to Integer objects containing ints. Yes, that's 3 levels of indirection, and I don't dare predict how many bytes that consumes ;)

java.util.Arrays。另一方面,asList(1,2,3,4,5)返回对对象的引用,该对象包含对包含int的整数对象的引用的数组的引用。是的,这是3个层次的间接指令,我不敢预测会消耗多少字节;

#6


8  

This is not a direct answer, but as you're coming from Java I'd like to point this out. By comparison to Java equivalents, STL is really fast.

这不是一个直接的答案,但是当你从Java中来的时候,我想指出这一点。与Java等价物相比,STL非常快。

I did find this page, showing some performance comparisons. Generally Java people are very touchy when it comes to performance conversations, and will claim that all kinds of advances are occurring all the time. However similar advances are also occurring in C/C++ compilers.

我确实找到了这个页面,显示了一些性能比较。一般来说,Java人员在谈到性能会话时非常敏感,他们会声称各种各样的改进一直在发生。然而,类似的进展也出现在C/ c++编译器中。

#7


7  

Keep in mind that STL is actually quite old now, so other, newer libraries may have specific advantages. Given the age, its' popularity is a testament to how good the original design was.

请记住,STL实际上已经相当老了,所以其他的新库可能具有特定的优势。考虑到时代,它的流行证明了最初的设计是多么的好。

There are four main reasons why I'd say that STL is (still) awesome:

我认为STL(仍然)很棒的原因有四个:

Speed STL uses C++ templates, which means that the compiler generates code that is specifically tailored to your use of the library. For example, map will automagically generate a new class to implement a map collection of 'key' type to 'value' type. There is no runtime overhead where the library tries to work out how to efficiently store 'key' and 'value' - this is done at compile time. Due to the elegant design some operations on some types will compile down to single assembly instructions (e.g. increment integer-based iterator).

Speed STL使用c++模板,这意味着编译器生成专门为您使用该库而定制的代码。例如,map将自动生成一个新类来实现“key”类型到“value”类型的映射集合。没有运行时开销,库试图找出如何有效地存储“key”和“value”——这是在编译时完成的。由于设计精美,对某些类型的一些操作将编译成单个的汇编指令(例如,递增整数迭代器)。

Efficiency The collections classes have a notion of 'allocators', which you can either provide yourself or use the library-provided ones which allocate only enough storage to store your data. There is no padding nor wastage. Where a built-in type can be stored more efficiently, there are specializations to handle these cases optimally, e.g. vector of bool is handled as a bitfield.

集合类有一个“分配器”的概念,您可以自己提供它,也可以使用图书馆提供的那些只分配足够的存储空间来存储数据的。没有填充和浪费。在可以更有效地存储内置类型的地方,有专门的方法来最优地处理这些情况,例如,bool的向量被处理为位域。

Exensibility You can use the Containers (collection classes), Algorithms and Functions provided in the STL on any type that is suitable. If your type can be compared, you can put it into a container. If it goes into a container, it can be sorted, searched, compared. If you provide a function like 'bool Predicate(MyType)', it can be filtered, etc.

您可以使用STL中提供的容器(集合类)、算法和函数来描述任何适合的类型。如果您的类型可以进行比较,您可以将其放入一个容器中。如果它进入一个容器,它可以被排序,搜索,比较。如果您提供一个类似“bool Predicate(MyType)”的函数,它可以被过滤,等等。

Elegance Other libraries/frameworks have to implement the Sort()/Find()/Reverse() methods on each type of collection. STL implements these as separate algorithms that take iterators of whatever collection you are using and operate blindly on that collection. The algorithms don't care whether you're using a Vector, List, Deque, Stack, Bag, Map - they just work.

优雅的其他库/框架必须在每种类型的集合上实现Sort()/Find()/反向()方法。STL将它们作为单独的算法来实现,这些算法使用您正在使用的任何集合的迭代器,并盲目地对该集合进行操作。算法并不关心你是否在使用向量,列表,Deque,堆栈,包,映射——它们只是起作用。

#8


3  

Well, that is somewhat of a bold claim... perhaps in C++0x when it finally gets a hash map (in the form of std::unordered_map), it can make that claim, but in its current state, well, I don't buy that.

嗯,这有点大胆的说法……也许在c++ 0x中,当它最终得到一个散列映射(以std::unordered_map的形式)时,它可以做出这样的声明,但是在当前状态下,我不买它。

I can tell you, though, some cool things about it, namely that it uses templates rather than inheritance to achieve its level of flexibility and generality. This has both advantages and disadvantages; a disadvantage is that lots of code gets duplicated by the compiler, and any sort of dynamic runtime typing is very hard to achieve; however, a key advantage is that it is incredibly quick. Because each template specialization is really its own separate class generated by the compiler, it can be highly optimized for that class. Additionally, many of the STL algorithms that operate on STL containers have general definitions, but have specializations for special cases that result in incredibly good performance.

但是,我可以告诉您一些关于它的很酷的事情,即它使用模板而不是继承来实现它的灵活性和通用性。这既有优点也有缺点;缺点是大量的代码被编译器复制,任何类型的动态运行时类型都很难实现;然而,它的一个关键优势是速度快得令人难以置信。因为每个模板专门化实际上都是由编译器生成的独立类,所以可以为该类进行高度优化。此外,许多在STL容器上操作的STL算法都有通用的定义,但是对特殊情况有专门的定义,这些特殊情况会导致非常好的性能。

#9


3  

STL gives you the pieces.

STL给你碎片。

Languages and their environments are built from smaller component pieces, sometimes via programming language constructs, sometimes via cut-and-paste. Some languages give you a sealed box - Java's collections, for instance. You can do what they allow, but woe betide you if you want to do something exotic with them.

语言及其环境是由较小的组件块构建的,有时通过编程语言结构构建,有时通过剪切-粘贴。有些语言给你一个封闭的盒子——比如Java的集合。你可以做他们允许做的事,但如果你想做一些异国的事情,你就会倒霉。

The STL gives you the pieces that the designers used to build its more advanced functionality. Directly exposing the iterators, algorithms, etc. give you an abstract but highly flexible way of recombining core data structures and manipulations in whatever way is suitable for solving your problem. While Java's design probably hits the 90-95% mark for what you need from data structures, the STL's flexibility raises it to maybe 99%, with the iterator abstraction meaning you're not completely on your own for the remaining 1%.

STL提供了设计人员用来构建更高级功能的部分。直接公开迭代器、算法等,可以提供一种抽象但高度灵活的方法,以适合解决问题的任何方式重新组合核心数据结构和操作。虽然Java的设计可能达到了数据结构所需的90-95%,但是STL的灵活性使它可能提高到99%,迭代器抽象意味着对于剩下的1%来说,您不能完全独立。

When you combine that with its speed and other extensibility and customizabiltiy features (allocators, traits, etc.), you have a quite excellent package. I don't know that I'd call it the best data structures package, but certainly a very good one.

当您将它与它的速度、其他可扩展性和定制特性(分配器、特性等)结合在一起时,您将得到一个非常优秀的包。我不知道我是否认为它是最好的数据结构包,但肯定是一个非常好的包。

Warning: percentages totally made up.

警告:百分比完全构成。

#10


2  

Unique because it

独特的,因为它

  • focuses on basic algorithms instead of providing ready-to-use solutions to specific application problems.
  • 关注基本的算法,而不是为特定的应用问题提供现成的解决方案。
  • uses unique C++ features to implement those algorithms.
  • 使用独特的c++特性来实现这些算法。

As for being best... There is a reason why the same approach wasn't (and probably won't) ever followed by any other language, including direct descendants like D.

是最好的…这就是为什么同样的方法没有(也可能不会)被任何其他语言采用,包括像D这样的直接后代。

#11


2  

The standard C++ library's approach to collections via iterators has come in for some constructive criticism recently. Andrei Alexandrescu, a notable C++ expert, has recently begun working on a new version of a language called D, and describes his experiences designing collections support for it in this article.

标准c++库通过迭代器处理集合的方法最近受到了一些建设性的批评。著名的c++专家安德烈·亚历山大最近开始开发一种名为D的语言的新版本,并在本文中描述了他设计集合支持的经验。

Personally I find it frustrating that this kind of excellent work is being put into yet another programming language that overlaps hugely with existing languages, and I've told him so! :) I'd like someone of his expertise to turn their hand to producing a collections library for the so-called "modern languages" that are already in widespread use, Java and C#, that has all the capabilities he thinks are required to be world-class: the notion of a forward-iterable range is already ubiquitous, but what about reverse iteration exposed in an efficient way? What about mutable collections? What about integrating all this smoothly with Linq? etc.

就我个人而言,我发现这种优秀的工作正在被投入到另一种与现有语言重叠的编程语言中,这让我感到很沮丧,我已经告诉过他了!:)我想让他的专长的人把他们的手产生集合库的所谓“现代语言”已经在广泛使用,Java和c#中,他认为,所有的功能需要世界级的:forward-iterable范围的概念已经无处不在,但反向迭代暴露在一个有效的方法呢?可变集合呢?如何将所有这些顺利地集成到Linq中呢?等。

Anyway, the point is: don't believe anyone who tells you that the standard C++ way is the holy grail, the best it could possibly be. It's just one way among many, and has at least one obvious drawback: the fact that in all the standard algorithms, a collection is specified by two separate iterators (begin and end) and hence is clumsy to compose operations on.

无论如何,关键是:不要相信那些告诉你标准c++方法是圣杯的人,它可能是最好的。它只是许多方法中的一种,并且至少有一个明显的缺点:在所有标准算法中,集合都是由两个独立的迭代器(开始和结束)指定的,因此编写操作很笨拙。

#12


2  

Obviously C++, C#, and Java can enter as many pissing contests as you want them to. The clue as to why the STL is at least somewhat great is that Java was initially designed and implemented without type-safe containers. Then Sun decided/realised people actually need them in a typed language, and added generics in 1.5.

显然,c++、c#和Java可以像您希望的那样输入许多pissing竞赛。关于STL至少有几分优秀的原因,可以看出Java最初是在没有类型安全容器的情况下设计和实现的。然后Sun决定/意识到人们实际上需要一种类型语言,并在1.5中添加了泛型。

You can compare the pros and cons of each, but as to which of the three languages has the "greatest" implementation of generic containers - that is solely a pissing contest. Greatest for what? In whose opinion? Each of them has the best libraries that the creators managed to come up with, subject to other constraints imposed by the languages. C++'s idea of generics doesn't work in Java, and type erasure would be sub-standard in typical C++ usage.

您可以对每种语言的优缺点进行比较,但是对于这三种语言中哪一种实现了泛型容器的“最大”实现——这完全是一场激烈的竞争。最大的为了什么?在谁的意见?它们中的每一个都有创建者设法找到的最好的库,受语言的其他限制。c++的泛型概念在Java中不起作用,在典型的c++使用中,类型擦除是不标准的。

#13


0  

The primary thing is, you can use templates to make using containers switch-in/switch-out, without having to resort to the horrendous mess that is Java's interfaces.

最主要的是,您可以使用模板使使用容器切换到/切换,而不必求助于Java接口的可怕混乱。

#14


0  

If you fail to see what usage the STL has, I recommend buying a book, "The C++ Programming Language" by Bjarne Stroustrup. It pretty much explains everything there is about C++ because he's the dude who created it.

如果您不知道STL有什么用途,我建议您买一本Bjarne Stroustrup的《c++编程语言》。它很好地解释了c++的一切,因为是他创造了c++。