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".

stl
Filter by
Sorted by
Tagged with
277 votes
21 answers
427k views

How to convert wstring into string?

The question is how to convert wstring to string? I have next example : #include <string> #include <iostream> int main() { std::wstring ws = L"Hello"; std::string s( ws.begin(), ...
0 votes
1 answer
76 views

C++ STL: how does std::unordered set and std::unordered_map hashing work?

I am trying to understand how STL unordered set/map (i.e. hash maps) work. I understood that initial hash table size (i.e. number of buckets) is set to 8 and when more elements are added to the set/...
0 votes
2 answers
366 views

How to correctly implement custom Iterators with Inheritance

I reinvented the STL vector for practice and understanding. Naturally I also had to implement an Iterator System. For re-usability I wanted to implement all Iterator Categorys with Inheritance (for ...
21 votes
2 answers
22k views

Creating a new vector using a transform

I have a vector of integers and I want to convert it to a vector of pairs (pair consists of a bool and a int). My current code is simple like this: std::vector<int> a; std::vector<...
2 votes
2 answers
650 views

When is dequeue size call not thread safe?

In general, operations on the standard containers are not thread safe (mostly). The size call on a std::vector, for example, may fail when reallocations are happening. Since a dequeue does not ...
40 votes
2 answers
47k views

How to solve error for hash function of pair of ints in unordered_map?

I have the following class with an unordered_map member, and a hash function defined for pair<int,int> class abc { public: unordered_map<pair<int,int>, int> rules; unsigned ...
56 votes
4 answers
23k views

Why are C++ STL iostreams not "exception friendly"?

I'm used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g file not found, disk full). I'm porting some code to use C++ STL instead, and have been caught out by iostreams NOT ...
0 votes
2 answers
64 views

Is there a function can modify map's value and return the iterator?

What I want is to insert a pair<key, value> into the map (create it if not existed, modify it otherwise), and got the value's pointer, to let others use. here is a demo code: #include <map>...
40 votes
9 answers
36k views

Breaking in std::for_each loop

While using std::for_each algorithm how do I break when a certain condition is satisfied?
-2 votes
1 answer
8k views

error: no matching function for call to 'std::map

typedef struct { string strDatabaseName; set <string, greater<string> > setDBAccName; } UserDBAInfo_t; typedef struct { map<int, UserDBAInfo_t > mapUserDBAInfo; } ...
0 votes
1 answer
30 views

Calling value functions of map of a custom class [duplicate]

I have STL map of a custom class where the key is an integer and the value is an object of my class. I want to call a function of my class using the map. My code is as follows: #include <map> ...
54 votes
3 answers
36k views

How to correctly use std::reference_wrappers

I am trying to understand std::reference_wrapper. The following code shows that the reference wrapper does not behave exactly like a reference. #include <iostream> #include <vector> #...
0 votes
3 answers
81 views

Check if Array Is Sorted and Rotated on LeetCode

I am trying to solve LeetCode problem 1752. Check if Array Is Sorted and Rotated: Given an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number ...
6 votes
0 answers
222 views

"Weak" atomic operations? [duplicate]

In the new C++11 standard, many atomic operations are defined in "strong/weak" pairs: template< class T > bool atomic_compare_exchange_weak( std::atomic<T>* obj, ...
0 votes
1 answer
55 views

Why my code is working on everything except one instance?

I do homework for my lab on c++, I needed to get a number from keyboard and delete an element after inputted number in STL list. For some reason if I get 7 elements into the list, exactly {1,2,2,3,4,2,...
5 votes
2 answers
6k views

gtest: Undefined symbols for architecture x86_64 error with clang++ and std::vector

I downloaded the gtest 1.6, and compiled it with clang++. export CC=/usr/bin/clang export CXX=/usr/bin/clang++ configure make I got the libgtest.a, and I copied it into /usr/local/lib/...
4 votes
3 answers
9k views

Is there any red black tree or avl tree implementation in c++ standard library?

Just like multiset is a Binary Search Tree implementation in STL, is there any RB tree or AVL tree implementation available?
0 votes
0 answers
99 views

C++ ordered map optimized with index access [duplicate]

In short, my question is, how to maximize the performance of ordered map (e.g. implemented as std::map) with good support of index access? Details: We can of course use some code like std::advance(it, ...
-2 votes
1 answer
65 views

Circular extention to std::array

I want to have an array like object of fixed size that loops and overwrites data as new data come in. I have defined this template class: template <typename T, int N> class CircularArray { ...
442 votes
16 answers
269k views

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement....
2 votes
3 answers
737 views

What is a good application of std::vector::data?

So my friend and I were looking at the std::vector class the other day and noticed that you can use the function std::vector::data to retrieve a pointer to the internal array in c++11. Intuitively ...
22 votes
2 answers
19k views

Where can I download the source code to the GNU C++ Standard Library?

I downloaded the GNU Standard C Library (glibc) source code at http://ftp.gnu.org/gnu/glibc/. Where can I find a similar download for their C++ Library? I have googled around a bit and cannot seem to ...
70 votes
5 answers
68k views

Converting between C++ std::vector and C array without copying

I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data. Does std::vector provide access to the underlying C array? I am looking ...
240 votes
6 answers
158k views

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity.
0 votes
0 answers
44 views

Is there a chance to use a custom std::pmr::polymorphic_allocator to make std::unordered_map’s buckets implemented as arrays?

While liked-list buckets implementation in std::unordered_map works well when one needs to add/remove elements from the container, it still could be speed up significantly if “stable” or “read-only” ...
364 votes
13 answers
608k views

How to sum up elements of a C++ vector?

What are the good ways of finding the sum of all the elements in a std::vector? Suppose I have a vector std::vector<int> vector with a few elements in it. Now I want to find the sum of all the ...
27 votes
7 answers
66k views

Convert "this" pointer to string

In a system where registered objects must have unique names, I want to use/include the object's this pointer in the name. I want the simplest way to create ??? where: std::string name = ???(this);
74 votes
7 answers
139k views

how do you insert the value in a sorted vector?

This question is a continuation of this one. I think that STL misses this functionality, but it just my IMHO. Consider following code: class Foo { public: Foo(); int paramA, paramB; std::...
14 votes
5 answers
28k views

distance between std::set begin() and std::set iterator in O(logn)

I need to find the index of an element in std::set. This index can be visualized as the distance of the iterator from the beginning. One way can be: for(int i = 0, set<int>::iterator it = s....
375 votes
27 answers
594k views

What's the most efficient way to erase duplicates and sort a vector?

I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. I currently have the below code, but it doesn't work. vec.erase( std::unique(vec.begin(), vec.end(...
18 votes
7 answers
29k views

C++ Serializing a std::map to a file

I have a C++ STL map, which is a map of int and customType. The customType is a struct, which has string and a list of string, How can i serialize this to a file. sample struct: struct customType{ ...
23 votes
4 answers
4k views

std::optional::value_or() - lazy argument evaluation

Is it possible to evaluate std::optional::value_or(expr) argument in a lazy way, so the expr were calculated only in the case of having no value? If not, what would be a proper replacement?
1 vote
1 answer
104 views

How to debug some complex c++ stls in watches of CodeBlocks? E.G. vector<vector<int> >v

I wrote a simple program concerning c++ stls. But I found that using CodeBlocks is very hard to debug with some kinds of a little more complex stls E.G. vector<vector >v. When I debug the ...
0 votes
0 answers
56 views

Importing <filesystem> in gcc

Importing filesystem has caused issues with my project as it seems to be clashing with memory. g++ -std=c++23 -fmodules-ts -xc++-system-header memory g++ -std=c++23 -fmodules-ts -xc++-system-header ...
39 votes
3 answers
30k views

How to replicate map, filter and reduce behaviors in C++ using STL?

I suppose we can use std::transform to replicate the map behavior in C++ like this : std::vector<int> in = { 1 , 2 , 3 ,4 }; std::vector<int> out(in.size()); std::transform(in.begin() , ...
28 votes
5 answers
2k views

std::shared_mutex::unlock_shared() blocks even though there are no active exclusive locks on Windows

My team has encountered a deadlock that I suspect is a bug in the Windows implementation of SRW locks. The code below is a distilled version of real code. Here's the summary: Main thread acquires ...
-1 votes
1 answer
49 views

STL: Keeping Only Unique String Characters AND Preserving Order [closed]

std::set and std::unordered_set on a string both pull out the unique characters only, but neither leaves the characters in the original order as they appeared in the string. There's so much of the STL ...
5 votes
5 answers
6k views

Are STL Map or HashMaps thread safe?

Can I use a map or hashmap in a multithreaded program without needing a lock? i.e. are they thread safe? I'm wanting to potentially add and delete from the map at the same time. There seems to be a ...
19 votes
4 answers
2k views

STL algorithms taking the whole container rather than .begin(), end() as arg? [duplicate]

Stand-alone STL algorithms (like std::count_if) take pair of iterators. In all cases where I use those (and in all examples I've seen online!), I find myself typing std::count_if(myContainer.begin(),...
334 votes
16 answers
592k views

Sorting a vector of custom objects

How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably, standard STL algorithm sort along with a predicate (a function or a function object) which would operate ...
117 votes
12 answers
69k views

In std::multiset is there a function or algorithm to erase just one sample (unicate or duplicate) if an element is found

Perhaps this is a duplicate but I did not find anything searching: When erase(value) is called on std::multiset all elements with the value found are deleted. The only solution I could think of is: ...
3 votes
2 answers
93 views

Are there any iterator invalidation rules for <algorithms> operations?

For example, in C++20 vector we have erase and erase_if. Their effects are defined in terms of remove and remove_if respectively (for erase [vector.erasure/1]): Effects: Equivalent to: auto it = ...
7 votes
2 answers
434 views

vector_base inheritance vs composition

Short Question: Is there a reason why C++ STL implementations use a vector_base struct/class (to handle resources and allocators) as a base class of std::vector rather than using composition? ...
68 votes
1 answer
20k views

How can I use Standard Library (STL) classes in my dll interface or ABI?

There have been a few questions before on exporting a class which contains stl classes in relation to visual studio warning C4251: E.g. this question or this question. I have already read the ...
7 votes
3 answers
11k views

Order statistics tree using __gnu_pbds for multiset

I am trying to implement an order statistics tree with using __gnu__pbds. I followed this code TREE_ORDER_STATISTICS But, I need this on a multiset. I was suggested to use a pair to implement this ...
-2 votes
3 answers
1k views

If some key is already present in the map then what will happen [duplicate]

If some key is already present in the map then what will happen If some key is already present in the map and we are trying the insert different value with same key then what will happen
1 vote
3 answers
5k views

Vectors to Priority_queue in STL c++

How to convert a vector v to priority_queue pq? Like we can do as- for(int i=0;i<(int)v.size();i++) pq.push(v[i]); But is it possible to keep it more short and concise?
4 votes
1 answer
105 views

could the type of std::map's key be double or float?

I've writed a test case: //environment: compiler=msvc140-x64 system=win10 #include <map> #include <iostream> int main(int argc, char* argv[]) { std::map<double, std::string> ...
129 votes
23 answers
162k views

How to implode a vector of strings into a string (the elegant way)

I'm looking for the most elegant way to implode a vector of strings into a string. Below is the solution I'm using now: static std::string& implode(const std::vector<std::string>& elems,...
-1 votes
1 answer
83 views

How does 'find()' work in a 'std::set<std::pair<int,int>>'?

I'm solving the LeetCode problem 'Path Crossing', where I use a set<pair<int,int>>, trying to find whether the point x,y is already in the path or not. But I don't get the desired result. ...

1
2 3 4 5
310