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

What is a pure-C alternative to STL containers?

Possible Duplicate: Container Class / Library for C One of the primary reasons to use C++ over C is the superbly convenient containers that STL provides. However, if I want to write my code in pure C and not have to write all my containers from…
Sandeep
  • 1,745
  • 3
  • 20
  • 30
11
votes
1 answer

Is `string.assign(string.data(), 5)` well-defined or UB?

A coworker wanted to write this: std::string_view strip_whitespace(std::string_view sv); std::string line = "hello "; line = strip_whitespace(line); I said that returning string_view made me uneasy a priori, and furthermore, the aliasing here…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
11
votes
8 answers

C++ cannot convert 'const char*' to 'std::string*'

I have this code below and I'm getting the error upon compilation: error: cannot convert 'const char*' to 'std::string*' for argument '1' to 'void sillyFunction(std::string*, int)' #include #include using namespace std; int…
Pwnna
  • 9,178
  • 21
  • 65
  • 91
11
votes
3 answers

Strange concurrency issue with STL / OpenMP in 64 bit builds

I have an odd issue when I build one of our projects in a 64 bit debug config. It appears to produce some strange behaviour which looks a little like a single iterator is getting incremented multiple times. I've narrowed it down to the following…
Peter
  • 7,216
  • 2
  • 34
  • 46
11
votes
6 answers

Should I use functions or stateless functors?

These 2 piece of code do same thing. And it will be used in sort function as you can see. Which is better? I usually write latter one. But I saw some coders do it like former one. struct val_lessthan : binary_function, pair
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
11
votes
1 answer

What is multimap::emplace() and move()?

I was viewing the MSDN doc about multimap and find that it has a member function multimap::emplace(). Below is the example of that member function. int main( ) { using namespace std; multimap m1; pair is1(1,…
MorrisLiang
  • 702
  • 2
  • 7
  • 20
11
votes
2 answers

Alternative to find() for determining whether an unordered_set contains a key

Suppose I have an unordered_set S and I wanna to check if it contains a certain int x. Is there a way for me to write something like if(S.contains(x)){ /* code */ } that works like if(S.find(x) != S.end()){ /* code */ } ? It can be a macro or…
Daniel
  • 7,357
  • 7
  • 32
  • 84
11
votes
3 answers

C++ std::list: Erasing / removing elements while iterating

Possible Duplicate: Can you remove elements from a std::list while iterating through it? I have a loop in a function, that iterates over an std::list from begin to end. In each cycle, I perform some checks and maybe do a few manipulations on the…
Jarx
  • 113
  • 1
  • 1
  • 4
11
votes
3 answers

C++ function object to return `p->first` and `p->second`

Is there a builtin function object that returns p->first and p->second, so that I can happily write transform(m.begin(),m.end(),back_inserter(keys),get_first); transform(m.begin(),m.end(),back_inserter(vals),get_second); STL-based solution is the…
Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169
11
votes
1 answer

Why do I get a constant instead of logarithmic curve for an insert time benchmark of the RB-tree based C++ std::set?

I was comparing BST to Heap at: Heap vs Binary Search Tree (BST) but when I tried to benchmark both and compare results, I could not interpret the data for BST. First, I confirmed that the standard library does use a Red-black tree: What is the…
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
11
votes
6 answers

How do I know if std::map insert succeeded or failed?

I have a map in a multithreaded app mapping a class called uuid to pointer. What I want to know if an insert operation succeeded for failed. e.g. _mymap.insert(hint, MyMap::value_type(entry.uuid, itemptr)); Does it throw an exception or something…
hookenz
  • 36,432
  • 45
  • 177
  • 286
11
votes
1 answer

Fixed-size std::span vs std::array

C++20 includes std::span, which "describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero". Its interface is very close to std::array, though it supports dynamic extent as well as…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
11
votes
2 answers

Could it be possible to have types with move operations that throw in containers?

While explaining move operations on objects with a colleague, I basically said that move operations should not throw exceptions in a container because if the move operation fails, then there is no way to bring back the original object reliably. …
Adrian
  • 10,246
  • 4
  • 44
  • 110
11
votes
2 answers

Move iterators for containers?

C++98 containers defined two kinds of iterator, ::iterators and ::const_iterators. Generally, like this: struct vec{ iterator begin() ; const_iterator begin() const; }; In C++11 this part of the design seems to be unchanged. The…
alfC
  • 14,261
  • 4
  • 67
  • 118
11
votes
3 answers

What is std::set::equal_range for?

Since std::set cannot contain duplicate elements and is always sorted, std::set::equal_range will always return the range that has either none or 1 element. Technically, yes, that's still a range, but what is the purpose of this algorithm? For…
rubix_addict
  • 1,811
  • 13
  • 27