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
1 answer

Why does std::vector::reserve call the copy constructor?

Why does the following code not work with g++ version 4.9.2? If I try to build it the compiler complains about the missing copy constructor in the line where the vector is told to reserve more memory. #include class Number { public: …
ifschleife
  • 1,055
  • 1
  • 10
  • 21
11
votes
2 answers

Why is there no std::inplace_merge_unique?

I tried looking for an algorithm that would do what std::inplace_merge followed by std::unique would do. Seems more efficient to do it in 1 pass than in 2. Could not find it in standard library or by oogling. So is there implementation…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
11
votes
1 answer

Initializing an array of pair in C++

I want to initialize an array of pair in the following way: pair adjs[4] = {{current_node.first-1, current_node.second}, {current_node.first+1, current_node.second}, {current_node.first, current_node.second-1}, {current_node.first,…
user1465557
  • 329
  • 2
  • 4
  • 11
11
votes
3 answers

How is std::vector faster than a plain array?

I stumbled across this while benchmarking a circular buffer. Can anyone explain how a std::vector manages to outperform a plain array in this instance? #include #include struct uint_pair { unsigned int a, b; uint_pair…
amarcus
  • 226
  • 2
  • 6
11
votes
3 answers

std::deque: How do I get an iterator pointing to the element at a specified index?

I have a std::deque, and I want to insert an element at a specified index (I'm aware that std::list would be better at this). The deque::insert() function takes an iterator to specify the location to insert. Given an index, how can I get an…
11
votes
3 answers

Using a checked STL implementation, anything available for free?

Have you used a checked STL implementation? Did it find bugs you were not expecting? Is there one I can try on Linux for free?
Dan
  • 5,929
  • 6
  • 42
  • 52
11
votes
3 answers

Why does std::priority_queue use max heap instead of min heap?

I always wondered why STL priority queue uses max heap instead of min heap by default. Two obvious use cases that come to mind is pathfinding (Dijkstra) and building Huffman codes. Both algorithms need to pull min elements first. Since sorting…
mar
  • 233
  • 3
  • 11
11
votes
4 answers

Why std::set is an associative container

I've gone through various texts. The only thing I got is that set is an associative container consisting of sorted and unique keys. Now if there are no values to map using key so where is the association in the set.
cbinder
  • 2,388
  • 2
  • 21
  • 36
11
votes
3 answers

Container optimization: Why are STL container method arguments no longer using allocator::const_reference typedef?

BEFORE YOU READ: const_reference is typedef and does not need to be const T& as you can see in std::vector::const_reference = bool. Please keep that in mind while reading the rest to understand it properly (as suggested in commets, this is…
firda
  • 3,268
  • 17
  • 30
11
votes
3 answers

Why may vector.begin() not equal to &vector[0]?

Here http://www.parashift.com/c++-faq/vector-is-contiguous.html is stated that vector.begin() may not be equal to &vector[0]. Why is it defined in this way. What does prevent vector.begin() to be equal to &vector[0]?
Ashot
  • 10,807
  • 14
  • 66
  • 117
11
votes
3 answers

printing stl containers with gdb 7.0

I have installed GDB 7.0 and python per the following instructions. In the same manual, there is a mention of this file stl-views-1.0.3.gdb. What confuses me is where it should be placed in order to enable pretty printing of stl containers. Would…
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
11
votes
8 answers

Does std::vector change its address? How to avoid

Since vector elements are stored contiguously, I guess it may not have the same address after some push_back's , because the initial allocated space could not suffice. I'm working on a code where I need a reference to an element in a vector,…
kunigami
  • 3,046
  • 4
  • 26
  • 42
11
votes
3 answers

Default size of std::vector / Programming books myth?

in a german programming book(dating to 2012) called "C++ für C-Programmierer"(C++ for C programmers, duh!) which I bought as a reference I found the following section in the chapter about the STL(I'll translate right away for you guys): Most STL…
hellerve
  • 508
  • 1
  • 6
  • 30
11
votes
6 answers

When do you use function objects in C++?

I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?
jasonline
  • 8,646
  • 19
  • 59
  • 80
11
votes
4 answers

Rank Tree in C++

We need ADT having search and rank features. That is, in addition to the interface of STL map, a function 'int get_rank(key)' is required. Standard implementation of such function requires supporting and updating an extra integer field in every node…
Slava
  • 827
  • 5
  • 13
1 2 3
99
100