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
2 answers

Performing set_difference on unordered sets

The set_difference algorithm requires the following The elements in the ranges shall already be ordered according to this same criterion which is not the case for hash tables. I'm thinking of implementing a set difference A-B in terms of…
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
11
votes
1 answer

Is there a way to specify a simpler JSON (de-)serialization for std::map using Cereal / C++?

The project I'm working on is a C++ application that manages a large number of custom hardware devices. The app has a socket/port interface for the client (like a GUI). Each device type has its own well-defined JSON schema and we can serialize those…
Lee Jenkins
  • 2,299
  • 3
  • 24
  • 39
11
votes
2 answers

Why does the naming convention of STL use so many leading underscore?

For example, template inline void sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { typedef typename iterator_traits<_RandomAccessIterator>::value_type …
Yantao Xie
  • 12,300
  • 15
  • 49
  • 79
11
votes
2 answers

C++ "group where" algorithm

Is there a function in the STL that will divide a sequence into contiguous subsequences where some predicate is valid? For example the following sequence: 1 1 1 0 1 1 0 0 1 1 1 1 Given a predicate v == 1, should return three subsequences: 1 1 1 1…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
11
votes
2 answers

Why is undefined behavior allowed in the STL?

By default, the "underlying container" of an std::stack is an std::deque. Therefore anything that is undefined behavior for a std::deque is undefined behavior for a std::stack. cppreference and other sites use the terminology "effectively" when…
user3358854
  • 113
  • 5
11
votes
5 answers

Iterate Multiple std::vector

I've read here and other places that when iterating a std::vector using indexes you should: std::vector x(20,1); for (std::vector::size_type i = 0; i < x.size(); i++){ x[i]+=3; } But what if you are iterating two vectors of different…
Mark
  • 106,305
  • 20
  • 172
  • 230
11
votes
4 answers

Add std::pair with + operator

Is there a simple way to make a+b work in the following example: #include #include int main () { std::pair a=std::make_pair(1,2); std::pair b=std::make_pair(3,3); std::pair c = a+b; …
titus
  • 5,512
  • 7
  • 25
  • 39
11
votes
2 answers

Are STL headers written entirely by hand?

I am looking at various STL headers provided with compilers and I cant imagine the developers actually writing all this code by hand. All the macros and the weird names of varaibles and classes - they would have to remember all of them! Seems error…
onqtam
  • 4,356
  • 2
  • 28
  • 50
11
votes
1 answer

Is it safe to put an std::array in a union?

I have a union declared like that: union { int all[4]; struct { int a, b, c, d; }; }; The point of the all array is simply to make iteration over the 4 fields simpler. To make it even simpler, I'd like to replace it with an…
zneak
  • 134,922
  • 42
  • 253
  • 328
11
votes
3 answers

what is auto_ptr_ref, what it achieves and how it achieves it

auto_ptr_ref documentation here says this This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions. Can somebody explain how auto_ptr_ref helps in achieving this. I just…
Yogesh Arora
  • 2,216
  • 2
  • 25
  • 35
11
votes
1 answer

Implementations of count_until and accumulate_until?

Given an input sequence, the standard algorithms std::count and std::accumulate count the number of occurances of a specific value (or predicate matches for std::count_if) and the accumulation of a given associative operation (sum, product, Boolean…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
11
votes
2 answers

STL Map with custom compare function object

I want to use the STL's Map container to lookup a pointer by using binary data as a key so I wrote this custom function object: struct my_cmp { bool operator() (unsigned char * const &a, unsigned char * const &b) { return…
dvl
  • 233
  • 1
  • 4
  • 6
11
votes
2 answers

Missing const_iterator overload of std::vector::erase() with g++ 4.8

The following example will not compile using g++ 4.8.2: #include #include using namespace std; int main() { vector v {1, 2, 3}; v.erase(v.cbegin()); // Compiler complains return 0; } The compiler says the…
George Hilliard
  • 15,402
  • 9
  • 58
  • 96
11
votes
3 answers

Two std::vectors pointing at nearly the same data, is that possible

I have an algorithm which expects a std::vector (call it A). However, I already have B with N + 2entries and what I basically want is to pass B.data() + 2, so the algorithm gets the last N entries from B. If A is modified, then so is B. When using…
wal-o-mat
  • 7,158
  • 7
  • 32
  • 41
11
votes
4 answers

vector::iterator - how to find position of an element

I am using the following code to find a string in an std::vector of string type. But how to return the position of particular element? Code: #include #include #include using namespace std; int main() { …
user2754070
  • 509
  • 1
  • 7
  • 16
1 2 3
99
100