Questions tagged [stl]

Do NOT use for questions about 3D CAD model. Use [stl-format] instead. The Standard Template Library, or STL, is a C++ library of generic containers, iterators, algorithms, and function objects. Use in conjunction with [c++]. When C++ was standardised, large parts of the STL were adopted into the Standard Library, and these parts in the Standard Library are also sometimes erroneously referred to collectively as "the STL".

The Standard Template Library, or STL, is a C++ library of generic containers, iterators, algorithms, and function objects. Originally designed by Alexander Stepanov and Meng Lee and published by HP in 1995. Large parts of the STL were adopted with modifications into the ISO C++ Standard Library.

Note that the name STL is ambiguous, as it may refer to different things. The following are the typical intended meanings (suggested tagging in brackets):

The last two definitions are, strictly speaking, incorrect; the C++ standard never mentions either "STL" or "Standard Template Library". In practice, however, people rarely need to refer to the HP library, and so "STL" is nearly always used to describe the STL-derived parts of the standard library instead (the algorithms, iterators and containers), or the template portions.

Programming Elements

Most of the STL's programming elements are in the std namespace. Containers, algorithms, iterators and other helper constructs exist in various headers within the std namespace. The basic and most common used container, vector can be used by:

  • Including the header <vector>
  • Declaring the variable by its full scoped name:
    std::vector<int> IntV;
    
  • Including whole of std (considered bad practice):
    using namespace std;
    vector<int> IntV;
    
  • Using the specific symbol:
    using std::vector;
    vector<int> IntV;
    

All other programming elements can be used by a similar pattern.

The beauty of STL is that containers (list, unordered_map, etc.), algorithms (sort, count_if, etc.), and iterators (reverse iterator, const iterator, etc.) are not dependent on each other but can be used together without knowing the internals of the other element. Containers and algorithms are connected by iterators.

Resources

*This reference is non-normative.

Books

15390 questions
244
votes
8 answers

What really is a deque in STL?

I was looking at STL containers and trying to figure what they really are (i.e. the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would allow insertion and deletion from both ends in…
Zonko
  • 3,365
  • 3
  • 20
  • 29
240
votes
5 answers

C++ STL Vectors: Get iterator from index?

So, I wrote a bunch of code that accesses elements in an stl vector by index[], but now I need to copy just a chunk of the vector. It looks like vector.insert(pos, first, last) is the function I want... except I only have first and last as ints. Is…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
238
votes
7 answers

What is the purpose of std::make_pair vs the constructor of std::pair?

What is the purpose of std::make_pair? Why not just do std::pair(0, 'a')? Is there any difference between the two methods?
user542687
238
votes
5 answers

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector for the sake of simplicity.
John Carter
  • 53,924
  • 26
  • 111
  • 144
236
votes
13 answers

Thou shalt not inherit from std::vector

Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from std::vector. I need about 10 customized algorithms for vector and I want them to be directly members of the vector. But naturally I want also…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
231
votes
8 answers

Is std::vector copying the objects with a push_back?

After a lot of investigations with valgrind, I've made the conclusion that std::vector makes a copy of an object you want to push_back. Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?! Thanks
benlaug
  • 2,691
  • 2
  • 17
  • 15
228
votes
13 answers

Why is the STL so heavily based on templates instead of inheritance?

I mean, aside from its name the Standard Template Library (which evolved into the C++ standard library). C++ initially introduce OOP concepts into C. That is: you could tell what a specific entity could and couldn't do (regardless of how it does it)…
OB OB
  • 3,289
  • 6
  • 21
  • 16
226
votes
6 answers

Why is std::map implemented as a red-black tree?

Why is std::map implemented as a red-black tree? There are several balanced binary search trees (BSTs) out there. What were design trade-offs in choosing a red-black tree?
Denis Gorodetskiy
  • 2,884
  • 3
  • 21
  • 23
225
votes
5 answers

When vectors are allocated, do they use memory on the heap or the stack?

Are all of the following statements true? vector vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack vector *vect = new vector; //allocates vect on heap and each of the Type will…
Phelodas
  • 3,853
  • 5
  • 25
  • 30
224
votes
6 answers

C++ Double Address Operator? (&&)

I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h: vector& operator=(vector&& __x) // <-- Note double ampersands here { // NB: DR 675. this->clear(); …
Anarki
  • 4,983
  • 4
  • 19
  • 9
224
votes
6 answers

How to initialize std::vector from C-style array?

What is the cheapest way to initialize a std::vector from a C-style array? Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style array: class Foo { std::vector w_; public: …
Frank
  • 64,140
  • 93
  • 237
  • 324
224
votes
6 answers

Why is it wrong to use std::auto_ptr<> with standard containers?

Why is it wrong to use std::auto_ptr<> with standard containers?
Uhall
  • 5,673
  • 7
  • 24
  • 19
222
votes
7 answers

Why use non-member begin and end functions in C++11?

Every standard container has a begin and end method for returning iterators for that container. However, C++11 has apparently introduced free functions called std::begin and std::end which call the begin and end member functions. So, instead of…
Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102
217
votes
7 answers

maximum value of int

Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?
d3vdpro
  • 2,887
  • 4
  • 25
  • 29
215
votes
7 answers

Why are Standard iterator ranges [begin, end) instead of [begin, end]?

Why does the Standard define end() as one past the end, instead of at the actual end?
Puppy
  • 144,682
  • 38
  • 256
  • 465