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.


*This reference is non-normative.


15390 questions
7 answers

push_back vs emplace_back

I'm a bit confused regarding the difference between push_back and emplace_back. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); As there is a push_back overload taking a rvalue reference I don't quite…
  • 49,529
  • 25
  • 126
  • 221
29 answers

Concatenating two std::vectors

How do I concatenate two std::vectors?
4 answers

Appending a vector to a vector

Assuming I have 2 standard vectors: vector a; vector b; Let's also say the both have around 30 elements. How do I add the vector b to the end of vector a? The dirty way would be iterating through b and adding each element via…
  • 8,403
  • 3
  • 16
  • 3
29 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can create an array and initialize it like this: int a[] = {10, 20, 30}; How do I create a std::vector and initialize it similarly elegant? The best way I know is: std::vector
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
16 answers

How do I erase an element from std::vector<> by index?

I have a std::vector, and I want to delete the nth element. How do I do that? Example: std::vector vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(???);
  • 7,059
  • 3
  • 17
  • 5
15 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given key is in a map and somewhat can't do it: typedef map::iterator mi; map m; m.insert(make_pair("f","++--")); pair p = m.equal_range("f");//I'm not sure if equal_range does what I…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
12 answers

Initializing a static std::map in C++

What is the right way of initializing a static map? Do we need a static function that will initialize it?
  • 5,331
  • 3
  • 17
  • 8
7 answers

What's the difference between "STL" and "C++ Standard Library"?

Someone brought this article to my attention that claims (I'm paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL. (...) it refers to the "STL", despite the fact that…
  • 31,619
  • 76
  • 167
  • 242
18 answers

Iteration over std::vector: unsigned vs signed index variable

What is the correct way of iterating over a vector in C++? Consider these two code fragments, this one works fine: for (unsigned i=0; i < polygon.size(); i++) { sum += polygon[i]; } and this one: for (int i=0; i < polygon.size(); i++) { sum…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
12 answers

How to check that an element is in a std::set?

How do you check that an element is in a set? Is there a simpler equivalent of the following code: myset.find(x) != myset.end()
  • 15,502
  • 9
  • 54
  • 74
11 answers

Why can't I make a vector of references?

When I do this: std::vector hello; Everything works great. However, when I make it a vector of references instead: std::vector hello; I get horrible errors like error C2528: 'pointer' : pointer to reference is illegal I want to put…
  • 13,428
  • 21
  • 78
  • 107
15 answers

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement...
  • 66,617
  • 42
  • 165
  • 277
16 answers

Best way to extract a subvector from a vector?

Suppose I have a std::vector (let's call it myVec) of size N. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For example, myVec [100000] through myVec [100999] in a vector…
  • 13,375
  • 13
  • 47
  • 46
11 answers

Sorting a vector in descending order

Should I use std::sort(numbers.begin(), numbers.end(), std::greater()); or std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators to sort a vector in descending order? Are there any benefits or drawbacks with one approach…
  • 256,549
  • 94
  • 388
  • 662
26 answers

What's the most efficient way to erase duplicates and sort a vector?

I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. I currently have the below code, but it doesn't work. vec.erase( std::unique(vec.begin(), vec.end()), vec.end()); std::sort(vec.begin(),…
Kyle Ryan
  • 4,271
  • 5
  • 22
  • 20
2 3
99 100