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
11
votes
4 answers

How do I copy binary data to a stringstream

I have a std::vector and I want serialize it. For this purpose I am trying to use a std::stringstream vector v; v.resize(10); for (int i=0;i<10;i++) v[i]=i; stringstream ss (stringstream::in | stringstream::out…
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49
11
votes
2 answers

How does iterator category in C++ work?

I tried to understand iterator implementation, and while playing around with the source, I saw this statement: typedef output_iterator_tag iterator_category; I don't understand how this typedef work within the class? What's the side effect does it…
roxrook
  • 13,511
  • 40
  • 107
  • 156
11
votes
2 answers

Why code using local struct as parameter for STL function does not compile in g++?

I have such code which works well: #include #include char x[11]= "ABCDEFGHIJ"; char y[11]; struct F { char operator () (char c) const { return c+1; } }; int main() { std::transform(x, x+10, y, F()); y[10] =…
RnMss
  • 3,696
  • 3
  • 28
  • 37
11
votes
3 answers

Generic Container in C++

I'm attempting to create a generic container type to provide a single common interface, as well as hide the internal containers I'm using as they are subject to change. Basically I have plugins that return collections of items and I don't wish the…
Ben Crowhurst
  • 8,204
  • 6
  • 48
  • 78
11
votes
2 answers

Correct way to initialize a container of std::byte

What is the correct way of initializing a container with predetermined std::byte values? std::array arr{0x36, 0xd0} for array results in Enum std::byte has not constant to represent the integer value of X and compiler errors. Vector…
mmatous
  • 482
  • 6
  • 16
11
votes
6 answers

How can I iterate over an STL map inside an STL map?

I have an STL map definition as follows: map > info; I iterate that map using the following code: for( map >::iterator ii=info.begin(); ii!=info.end(); ++ii){ for(map::iterator…
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
11
votes
6 answers

Container initialization in C++98

I have to construct an ordered container (which must be iterable) with the following rule: If the condition is true, the container is {1,0}, else it's {0,1} I have the following code, but I don't find it "elegant": vector orderedSides; …
youyou
  • 173
  • 1
  • 11
11
votes
1 answer

C++ What is the role of std::ctype::widen()?

According to the C++ standard (§30.7.5.2.4 of C++17 draft (N4659)), out << ch will not perform a widening operation on ch, if ch is a char and out is a std::ostream. Does this imply that std::ctype::widen() (i.e., char -> char) is guaranteed…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
11
votes
2 answers

Aligned types and passing arguments by value

Passing aligned types or structures with aligned types by value doesn't work with some implementations. This breaks STL containers, because some of the methods (such as resize) take their arguments by value. I run some tests with Visual Studio 2008…
watson1180
  • 2,015
  • 1
  • 18
  • 24
11
votes
5 answers

Is C++ STL thread-safe for distinct containers (using STLport implementation)?

I'm using Android 2.2, which comes with a version of STLport. For some reason, it was configured to be non-thread safe. This was done using a #define _NOTHREADS in a configuration header file. When I constructed and initialized distinct non-shared…
Ravi
  • 3,718
  • 7
  • 39
  • 57
11
votes
1 answer

Why does STL set have count() when all elements are supposed to be unique?

I can understand that multiset has count(), for counting the number of occurrences of a value, because elements can be repeated in multiset. But what's the point of having count() in set, when all the values are already unique?
Nav
  • 19,885
  • 27
  • 92
  • 135
11
votes
5 answers

How to learn to write idiomatic c++ code

I recently forced myself to study C++ and I just finished reading the book C++: The Complete Reference, by Herbert Schildt. I liked the book and think I got more or less the big picture. I noticed though that when I try to check with other people…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
11
votes
3 answers

Why does every STL container have a swap function defined as a member function?

Consider the queue container in STL. It is my understanding that swap() available in the header would work just fine. I understand that swap() will only copy the queue instances superficially, that is, only the front and rear pointers…
Aayush Mahajan
  • 3,856
  • 6
  • 25
  • 32
11
votes
4 answers

Using an unordered_map with arrays as keys

I don't understand why I can't have an unordered_map with an array as the key type: #include using namespace std; int main() { array key = {0,1,2}; unordered_map< array , int > test; test[key] =…
Adrien
  • 324
  • 1
  • 3
  • 10
11
votes
9 answers

What's your favorite STL trick?

I've been programming in C++ for quite a while now, but every now and then I stumble upon a code snippet using STL that would have taken myself quite some time and a lot more code to accomplish. STL takes quite a while to get used to, and there are…
chris
  • 3,986
  • 1
  • 26
  • 32