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
1146
votes
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…
ronag
  • 49,529
  • 25
  • 126
  • 221
969
votes
29 answers

Concatenating two std::vectors

How do I concatenate two std::vectors?
yigal
836
votes
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…
sub
  • 8,403
  • 3
  • 16
  • 3
705
votes
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
704
votes
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(???);
dau_man
  • 7,059
  • 3
  • 17
  • 5
621
votes
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
531
votes
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?
Nithin
  • 5,331
  • 3
  • 17
  • 8
509
votes
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…
Pieter
  • 31,619
  • 76
  • 167
  • 242
491
votes
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
461
votes
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()
fulmicoton
  • 15,502
  • 9
  • 54
  • 74
445
votes
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…
Colen
  • 13,428
  • 21
  • 78
  • 107
434
votes
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...
Roddy
  • 66,617
  • 42
  • 165
  • 277
388
votes
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…
An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
386
votes
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…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
365
votes
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
1
2 3
99 100