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

Is STL empty() threadsafe?

I have multiple threads modifying an stl vector and an stl list. I want to avoid having to take a lock if the container is empty Would the following code be threadsafe? What if items was a list or a map? class A { vector items …
cpp dev
  • 163
  • 1
  • 5
11
votes
3 answers

Replace vector of vector with flat memory structure

I have the following type: std::vector> indicies where the size of the inner vector is always 2. The problem is, that vectors are non-contiguous in memory. I would like to replace the inner vector with something contiguous so that…
Stein
  • 3,179
  • 5
  • 27
  • 51
11
votes
6 answers

memory use of STL data structures, windows vs. linux

I have a program that heavily uses std::map. Under Windows, much more memory is used as under Linux. Has anyone an idea why this happens? Linux: Last process took 42.31 s and used not more than 909 MB (RSS 900 MB) of memory Windows: Last process…
swegi
  • 4,046
  • 1
  • 26
  • 45
11
votes
2 answers

Is there an allocator that uses alloca and is otherwise C++ STL compliant?

I have two questions: 1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant? If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there…
Bjoern
  • 119
  • 3
11
votes
3 answers

Why is there no "Iterable" interface in the STL?

The C++ STL does not seem to use purely abstract base classes (aka interfaces) very often. I know that most things can be achieved with the STL algorithms or clever template metaprogramming. But still, for some use cases (for example, in an API, if…
summentier
  • 456
  • 4
  • 13
11
votes
5 answers

What makes this bucket sort function slow?

The function is defined as void bucketsort(Array& A){ size_t numBuckets=A.size(); iarray buckets(numBuckets); //put in buckets for(size_t i=0;i!=A.size();i++){ buckets[int(numBuckets*A[i])].push_back(A[i]); } ////get back…
luoq
  • 307
  • 2
  • 10
11
votes
2 answers

When do I use node_type with std::map::insert?

I'm accustomed to the existing interface of std::map. Inserting elements returns a bool describing successful insertion, as well the iterator as to where the inserted element would be. template< class P > std::pair insert( P&& value…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
11
votes
3 answers

Forward declaration of objects with STL containers

Consider the following code snippet, where the first line serves only as forward declaration class A; followed by defining new class class B { vector Av; //line 1 map Am; //line 2 pair Ap; //line 3 }; line 1 and line 2…
11
votes
11 answers

STL in embedded environment

I am a C++ programmer and over the years have been subjected to hearing the notion that STL is not good for use in embedded environments and hence usually prohibited in usage for embedded environment based projects.I believe STL libraries like Boost…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
11
votes
5 answers

C++ deque vs vector and C++ map vs Set

Can some one please tell me what is the difference between vector vs deque. I know the implementation of vector in C++ but not deque. Also interfaces of map and set seem similar to me. What is the difference between the two and when to use one.
brett
  • 5,379
  • 12
  • 43
  • 48
11
votes
6 answers

C++ vector, return vs. parameter

Possible Duplicate: how to “return an object” in C++ I am wondering if there is a difference between the three following approaches: void FillVector_1(vector& v) { v.push_back(1); // lots of push_backs! } vector FillVector_2() { …
Marc Andreson
  • 3,405
  • 5
  • 35
  • 51
11
votes
1 answer

Difference between std::end(myVector) and myVector.end()

I've noticed there are 2 ways to get the end iterator of a vector (or other container class): std::end(myVector) and myVector.end() The same goes for various other container iterator functions, begin, cend, cbegin, rend, rbegin, crend, crbegin,…
Darrel Hoffman
  • 4,436
  • 6
  • 29
  • 41
11
votes
2 answers

How is std::is_empty implemented in VS2015 (or any compiler)?

My current question has been inspired by attempting to understand how std::unique_ptr utilizes template mechanics to instantiate a template class the size of a T* when D (the deleter type) is a lambda function type, but a larger size when D is…
Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
11
votes
2 answers

static member function make_shared of shared_ptr

Using libc++ I find out std::shared_ptr::make_shared() static member function in public section. It is very handy when I have already defined type alias to std::shared_ptr's specialization: using T = int; using P = std::shared_ptr< T >; auto p =…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
11
votes
4 answers

Inject namespace experimental to std

Is it bad or good parctice to inject namespace std::experimental into std like following? namespace std { namespace experimental { } using namespace experimental; } #include int main() { std::optional< int > o; …
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169