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

Finding any element with specific first coordinate in set >

I'm trying to figure out the following problem. Suppose I have the following container in C++: std::set > my_container; This set (dictionary) is sorted with respect to the order < on std::pair, which is the…
Jytug
  • 1,072
  • 2
  • 11
  • 29
11
votes
3 answers

Most efficient way of copying a raw byte array into an empty byte vector

I have a scenario in which I need to copy the contents of a raw dynamically allocated uint8_t array into a vector (which is guaranteed to be empty whenever this scenario happens). vector myVector; const uint8_t* myRawArray; It is really…
mk33
  • 351
  • 1
  • 2
  • 6
11
votes
1 answer

Will std::multimap preserve the insert order if the key of 2 elements equal to each other?

I am wondering whether this is true? If it is, is this behavior guaranteed by the c++ standard?
Thomson
  • 20,586
  • 28
  • 90
  • 134
11
votes
2 answers

Is it safe in C++ to subtract from container.end()?

That is, providing that container is not empty, can I safely do this: std::vector container; container.push_back( 0xFACE8D ); auto last = container.end() - 1; and this: EDIT: replaced -1 with -- here: std::list
Michael
  • 5,775
  • 2
  • 34
  • 53
11
votes
3 answers

How to override std::hash for an enum defined inside a class?

I've got an enumeration type defined inside of a class, and I want to create an unordered_set of those objects as a member of the class: #include class Foo { public: enum Bar { SOME_VALUE }; // Error: implicit…
Talin
  • 1,397
  • 2
  • 15
  • 30
11
votes
2 answers

Compile time array from C++ template parameter pack

How can I at compile time create for example an std::array from a template parameter pack? This shows what I need, but without a parameter pack. template struct ToInfoArray { static constexpr…
Mathias
  • 1,446
  • 2
  • 16
  • 31
11
votes
4 answers

How to call constructor of objects contained in a std::vector?

When I create a std::vector of objects, the constructor of these objects is not always called. #include #include using namespace std; struct C { int id; static int n; C() { id = n++; } // not called // C() { id =…
Pietro
  • 12,086
  • 26
  • 100
  • 193
11
votes
9 answers

How do I get STL std::string to work with unicode on windows?

At my company we have a cross platform(Linux & Windows) library that contains our own extension of the STL std::string, this class provides all sort of functionality on top of the string; split, format, to/from base64, etc. Recently we were given…
NSA
  • 5,689
  • 8
  • 37
  • 48
11
votes
5 answers

Iterating over 2-dimensional STL vector c++

I'm currently trying to print out a history of movements for players in a game I am working on. At the end of each round every player has moved some amount in the positive or negative direction and this gets recorded as an int in the movement…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
11
votes
1 answer

C++11 / C++03 and std::vector thread safety

I am reading about thread safety of various stl containers from this link Now I came across this point which states for C++11 only Different elements in the same container can be modified concurrently by different threads, except for the elements…
MistyD
  • 16,373
  • 40
  • 138
  • 240
11
votes
4 answers

What is the use for buckets interface in std::unordered_map?

I've been watching this video from CppCon 2014 and discovered that there is an interface to access buckets underneath std::unordered_map. Now I have a couple of questions: Are there any reasonable examples of the usage of this interface? Why did…
Kostya
  • 1,536
  • 1
  • 13
  • 22
11
votes
2 answers

C++ using std::vector across boundaries

Assuming that EXE and DLL use the same compiler and STL version. If I use a std::vector in my EXE and use reserve to reserve memory. Then I pass it as reference to a DLL. I do a push_back in the DLL to add an element to my vector. If I do not…
FSeywert
  • 111
  • 5
11
votes
1 answer

Are std::find and std::map.find both O(logN)?

Are std::find and std::map.find both O(logN)? If they are, how does std::find implement search over std::map in logarithmic time? Is the implementation of std::find specialized for the std::map use case?
user855
  • 19,048
  • 38
  • 98
  • 162
11
votes
3 answers

Why does while(std::ifstream >> s) work?

I've used statements such as this quite a bit in my C++ programming: std::string s; std::ifstream in("my_input.txt"); if(!in) { std::cerr << "File not opened" << std::endl; exit(1); } while(in >> s) { // Do something with s } What I…
bobroxsox
  • 902
  • 2
  • 10
  • 26
11
votes
3 answers

How can I marshall a vector from a C++ dll to a C# application?

I have a C++ function that produces a list of rectangles that are interesting. I want to be able to get that list out of the C++ library and back into the C# application that is calling it. So far, I'm encoding the rectangles like so: struct…
mmr
  • 14,781
  • 29
  • 95
  • 145