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
356
votes
13 answers

How to sum up elements of a C++ vector?

What are the good ways of finding the sum of all the elements in a std::vector? Suppose I have a vector std::vector vector with a few elements in it. Now I want to find the sum of all the elements. What are the different ways for the same?
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
343
votes
24 answers

How to retrieve all keys (or values) from a std::map and put them into a vector?

This is one of the possible ways I come out: struct RetrieveKey { template typename T::first_type operator()(T keyValuePair) const { return keyValuePair.first; } }; map m; vector keys; //…
Owen
  • 3,851
  • 2
  • 18
  • 12
342
votes
4 answers

std::string length() and size() member functions

I was reading the answers for this question and found that there is actually a method called length() for std::string (I always used size()). Is there any specific reason for having this method in std::string class? I read both MSDN and CppRefernce,…
Naveen
  • 74,600
  • 47
  • 176
  • 233
338
votes
4 answers

C++ Erase vector element by value rather than by position?

vector myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of "8", I think I would do this: myVector.erase(myVector.begin()+4); Because that would…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
325
votes
15 answers

Sorting a vector of custom objects

How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably, standard STL algorithm sort along with a predicate (a function or a function object) which would operate on one of the fields (as a key for sorting) in…
Ankur
  • 11,239
  • 22
  • 63
  • 66
322
votes
11 answers

Determine if map contains a value for a key?

What is the best way to determine if a STL map contains a value for a given key? #include using namespace std; struct Bar { int i; }; int main() { map m; Bar b = {0}; Bar b1 = {1}; m[0] = b; m[1] = b1; …
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
304
votes
17 answers

vector vs. list in STL

I noticed in Effective STL that vector is the type of sequence that should be used by default. What's does it mean? It seems that ignore the efficiency vector can do anything. Could anybody offer me a scenario where vector is not a feasible…
skydoor
  • 25,218
  • 52
  • 147
  • 201
302
votes
9 answers

Is std::unique_ptr required to know the full definition of T?

I have some code in a header that looks like this: #include class Thing; class MyClass { std::unique_ptr< Thing > my_thing; }; If I include this header in a cpp that does not include the Thing type definition, then this does not…
Klaim
  • 67,274
  • 36
  • 133
  • 188
284
votes
2 answers

Why can I not push_back a unique_ptr into a vector?

What is wrong with this program? #include #include int main() { std::vector> vec; int x(1); std::unique_ptr ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. …
user383352
  • 5,033
  • 4
  • 25
  • 20
281
votes
27 answers

Why use iterators instead of array indices?

Take the following two lines of code: for (int i = 0; i < some_vector.size(); i++) { //do stuff } And this: for (some_iterator = some_vector.begin(); some_iterator != some_vector.end(); some_iterator++) { //do stuff } I'm told that the…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
276
votes
16 answers

C++ sorting and keeping track of indexes

Using C++, and hopefully the standard library, I want to sort a sequence of samples in ascending order, but I also want to remember the original indexes of the new samples. For example, I have a set, or vector, or matrix of samples A : [5, 2, 1, 4,…
Mingus
  • 2,903
  • 3
  • 18
  • 9
269
votes
19 answers

Remove spaces from std::string in C++

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?
Steve Hanov
  • 11,316
  • 16
  • 62
  • 69
269
votes
19 answers

How to convert wstring into string?

The question is how to convert wstring to string? I have next example : #include #include int main() { std::wstring ws = L"Hello"; std::string s( ws.begin(), ws.end() ); //std::cout <<"std::string = …
BЈовић
  • 62,405
  • 41
  • 173
  • 273
253
votes
10 answers

C++ equivalent of StringBuffer/StringBuilder?

Is there a C++ Standard Template Library class that provides efficient string concatenation functionality, similar to C#'s StringBuilder or Java's StringBuffer?
An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
245
votes
22 answers

Is std::vector so much slower than plain arrays?

I've always thought it's the general wisdom that std::vector is "implemented as an array," blah blah blah. Today I went down and tested it, and it seems to be not so: Here's some test results: UseArray completed in 2.619 seconds UseVector completed…
kizzx2
  • 18,775
  • 14
  • 76
  • 83