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

Filter by
Sorted by
Tagged with
0 votes
0 answers
17 views

std::find in vector with structures

I have a structure struct position { int x; int y; }; and vector of such structures std::vector<position> pos; When I try to check whether a certain element is present in this vector, ...
user43283's user avatar
  • 203
0 votes
0 answers
62 views

Better way of inserting in a map of pointers c++

I have the following structure and a class struct myStruct { int a; int b; int c; }; class myClass { map<int, myStruct*> myMap; int i; //and few other member variables ...
arjun gulyani's user avatar
-1 votes
2 answers
41 views

difference between `explicit vector( size_type count, const Allocator& alloc = Allocator() );` and `std::vector<T,Allocator>::reserve`

What's the difference between std::vector<uint8_t> vec1(size); and std::vector<uint8_t> vec2; vec2.reserve(size); Do they both allocate memory on heap?
mewo1234's user avatar
  • 309
0 votes
0 answers
134 views
+50

Inconsistent access specifiers in STL GCC implementation

While reading about the implementation of STL containers in GCC, I noticed a discrepancy in the use of access specifiers in the base classes. Specifically, some classes (_Vector_base) are completely ...
LoS's user avatar
  • 474
0 votes
2 answers
77 views

Templates core headers bloating and explicit specialization of std::hash in cpp file in c++17

Suppose I got a core library in which I'm trying to add explicit specialization of std::hash for my core classes, including class Foo (and many others I'd like to also specialize std::hash with). Foo....
pandaman1234's user avatar
1 vote
1 answer
73 views

Why do they use pointer, void_pointer and other typedef inside allocator and vector classes of STL?

Looking at the https://en.cppreference.com/w/cpp/memory/allocator value_type is typedef(or alias) of T, pointer is typedef(or alias) of T*, void_pointer is typedef(or alias) of void*, Why do we need ...
graveman's user avatar
  • 111
4 votes
1 answer
87 views

Why is the KeyEqual of std::unordered_map not used by its operator==?

In the following code, I have defined template arguments Hash and KeyEqual for unordered_map. I expect the output to be 1 1 1 1 but it is actually 1 1 0 1. Why does this happen? Is it because std::...
Trams's user avatar
  • 285
1 vote
1 answer
50 views

why element in std::priority_queue deconstructed so many times?

I am using std::priority_queue to do file sort, which i think heap is the best way to sort the stream data. here is the code: #include <bits/stdc++.h> using namespace std; struct A { A(int a,...
nothingisme's user avatar
0 votes
1 answer
53 views

Are there drawbacks of using C++ containers and smart pointers instead of C style arrays as buffers?

I have often seen that when a C++ function wants to return a buffer of data, the caller have to provide a pointer to the first element of the buffer and the count as function arguments. Sometimes this ...
Yashoja Lakmith's user avatar
-1 votes
0 answers
39 views

Can std map's comparator bool < merge two different keys into one like one key has three values and other key has four values something like this

Is there a possibility or is there any way by which I can design a comparator such that for following below values: 509 398 33a2 0 398 509 243e 33a2 398 509 0 33a2 509 398 0 243e 509 398 0 ...
manu shrivastava's user avatar
0 votes
1 answer
44 views

What is loki "Assoc Vector", how does it work, and how is it related to flat_map?

https://github.com/snaewe/loki-lib/blob/master/include/loki/AssocVector.h Apparently this code was written by Andrei Alexandrescu. There is a popular answer for benchmarking flat_map, but few details ...
jokoon's user avatar
  • 6,227
0 votes
1 answer
55 views

How to remove previous element in an std::list with a reverse_iterator?

Look at this simple algorithm that remove elements around specific ones : void forwardRemoveAlgorithm(std::list<int> &list, int removeAround) { for(std::list<int>::iterator it=list....
GholGoth21's user avatar
2 votes
1 answer
24 views

Destination of std::ctype::narrow() fills by default value

I'm using const CharT* narrow(const CharT* beg, const CharT* end, char dflt, char* dst) const of a std::ctype<wchar_t> facet, but dst is filled by dflt value. It's my code: std::wstring str = L&...
Yury's user avatar
  • 31
-2 votes
0 answers
48 views

`std::vector` constructor with two iterators [duplicate]

The following is a simplification of my code: #include <iostream> #include <algorithm> #include <vector> using std::cout; using std::endl; using std::vector; int main(int, char**){ ...
Trams's user avatar
  • 285
1 vote
0 answers
28 views

Value of std::money_base::pattern for std::moneypunct facet is different with output of money value

I'm trying figure out format of output of a negative money value for std::moneypunct facet. #include <locale> #include <fstream> #include <iomanip> #include <iostream> int ...
Yury's user avatar
  • 31
5 votes
2 answers
89 views

Is it possible to use std::visit on variant that hold same type more than once?

For example, I'm trying to implement an AST using std::variant, where a Token can be a number, keyword, or variable. Where numbers are represented by the int and keywords and variables are represented ...
Jemtaly's user avatar
  • 51
1 vote
1 answer
58 views

Why do I need a const lambda when calling std::partition

I have this function which uses std::partition to split a container into two based on a predicate. template<class Container, class Predicate> void split_partition(Container& src, Container&...
Slava's user avatar
  • 85
1 vote
1 answer
42 views

Reading files with istreambuf_iterator vs copy_n

I want to parse different kinds of chunks in a file with varying length, so I created a function to read out a chunk by passing in the ifstream, like this: void parse_next(std::ifstream& ...
SupAl's user avatar
  • 531
-1 votes
1 answer
40 views

Return type of `std::unordered_map::emplace` [duplicate]

I'm using std::unordered_map in my own class. The code is like the following: #include <iostream> #include <unordered_map> template<class T> class MSet { public: std::...
Trams's user avatar
  • 285
3 votes
0 answers
119 views

C++ STL: The third parameter of sort(), why the functor is faster than inline function? [duplicate]

inline bool mycmp(int i, int j) { return (i < j); } class mycmp2 { public: bool operator()(int i, int j) { return (i < j); } }; above is my example. I want know why the ...
xf h's user avatar
  • 39
1 vote
1 answer
69 views

can't compile with shared_ptr<int[N]> sp(int(*)[N])

The following code fails to compile with the error: "/usr/include/c++/9/bits/shared_ptr.h:106:8: error: no type named 'type' in 'struct std::enable_if<false, void>'". I am new to C++ ...
ConnellyM's user avatar
  • 173
-3 votes
1 answer
48 views

get lower bound iterator of search string from set. Here, strings length in set are less than search string length [closed]

I want the lower bound iterator of search string from set. Here, strings length in set are less than search string length. I mean, set has strings like { "/Applications", "/Bpplications&...
Rednam Nagendra's user avatar
2 votes
1 answer
46 views

Conversion to std::function fails from a function receiving a class with a non-copyable member variable and an explicitly-declared destructor by value

Note: this is a variation of this similar question, but with a movable object and a explicitly declared destructor. The following code #include <functional> #include <memory> struct S { ...
LHLaurini's user avatar
  • 1,737
-2 votes
0 answers
45 views

Is there any way for me to detect the correctness of a std::wstring before initializing an std:: wregex to avoid initialization failure [duplicate]

I want to initialize a std::wregex with a std::wstring, but if I give a std::wstring that do not conform to regular expression syntax(for example, std::wstring(L"*")), the initialization ...
key zhao's user avatar
-1 votes
1 answer
69 views

Time complexity of std::set, std::multiset, std::map and std::multimap iterator increment in C++ standard library

It's well known, that next element search complexity in binary tree is O(log(N)). But whether tree nodes are also double linked list nodes, complexity would be O(1). So, the question is: are std::set ...
Misha T's user avatar
  • 159
3 votes
0 answers
185 views

Why the size of vector can be so huge?

I have a puzzle for the following code: #include <iostream> #include <vector> int main() { const size_t LARGE = 12000000000; std::vector<long> vec(LARGE); vec[0] = -1; ...
CPW's user avatar
  • 175
-1 votes
1 answer
63 views

How do I privately implement an operator for an STL container?

Say we have something like this: template<typename T> class SparseMatrix { // Where the first (K,V) is (row_idx, columns) and the second is (col_idx, numeric type) using Entries = std::...
Ders's user avatar
  • 13
0 votes
3 answers
51 views

error: invalid operands to binary expression ('book' and 'const int') in find in c++

class book{ public: void setNameOfAuthor(string nameOfAuthor); string getNameOfAuthor(); void setBookId(int bookid); int getBookId(); bool operator==(const book& rhs)const; const ...
farida fathima's user avatar
2 votes
1 answer
60 views

Understanding the Behavior of std::end() and Memory Allocation in Vectors

What exactly does std::end() point to? When using a std::vector, does it allocate extra memory to accommodate the end position of the elements?
Elhazin's user avatar
  • 41
1 vote
1 answer
44 views

Unable to use to_chars with clang++ on MacOs

I've tried to use g++ and clang++, I've tried various c++ versions with the -std flag. I have tried reading the charconv file to see what the issue may be. I could use some help, I'm really unsure of ...
Adam Bujak's user avatar
3 votes
1 answer
129 views

Why does std::function accept a data member pointer?

This code is extracted and simplified from std::function #include <functional> #include <iostream> struct Foo { int num_; }; int main() { const Foo foo(314159); // store a ...
Zhang's user avatar
  • 3,050
0 votes
1 answer
31 views

CUDA Thrust How can I combine copy_if and transform without materialize data

Let's say we have two inputs, the first one is an array, and the second is a bitmap thrust::device_vector<point_t> points; Bitset bits; // Imagine this can be accessed within the kernel. What I ...
geng liang's user avatar
0 votes
1 answer
65 views

std::transform in copying data from json array to a float array C++

I have a json array as shown below { { "Data":[1.0, 2.0, 3.0] } } I am trying to copy this json array to a float array using std::transform. In code - #include <nlohmann/json.hpp> #...
Nitron_707's user avatar
2 votes
1 answer
117 views

How to add pointer semantics into C++ STL?

I am working on C++ STL recently, and I will discuss an example using deque. Note that deque is just an example, we could also use vector or others. Background The minimal reproducible example is a ...
Ethan L.'s user avatar
  • 395
3 votes
1 answer
56 views

What is key difference between regex_iterator and regex_token_iterator?

Looking at the regex_iterator and regex_token_iterator I see that the key difference is the value_type which is: match_results<BidirIt> for the regex_iterator sub_match<BidirIt> for the ...
Damir Tenishev's user avatar
1 vote
1 answer
83 views

When to use std::numeric_limits<T>::infinity() vs. cmath's INFINITY [duplicate]

C++ provides both the std::numeric_limits<T>::infinity() function and the INFINITY macro for representing infinity. In general, what is the difference between the two? When would one be ...
charlescochran's user avatar
2 votes
1 answer
54 views

Can the allocator of `std::pmr::map` be used by container elements for their allocations?

Say we have an std::map as follows: std::pmr::map<std::string, std::unint8_t> test; How can memory for std::string be allocated by the polymorphic allocator of the std::map rather than by std::...
Abhijit's user avatar
  • 107
3 votes
2 answers
123 views

Replacing loop in a loop to find match in two vectors with STL

Given structure: struct bob { int a {1}; std::string b{"bob"}; }; std::vector<bob> list1{{1, "a"}, {2, "b"}, {3, "c"}}; std::vector<bob> ...
code_fodder's user avatar
  • 15.3k
5 votes
3 answers
130 views

`std::views::split` on MSVC

I want to split a string by a token with std::views::split, and for each retrieved substring invoke the std::from_chars function. Here is an MRE (https://godbolt.org/z/1K71qo9s4) that compiles ...
Andrew's user avatar
  • 311
3 votes
1 answer
104 views

Ways to represent float infinity in C++

I want to express floating-point infinity in C++ for my program. I came across two ways to achieve this: using INFINITY and std::numeric_limits<float>::infinity(). Both options seem to work, but ...
Cythonista's user avatar
0 votes
0 answers
54 views

how to trace stl src code, for example std::string , with IDE(vscode)?

I am a c++ beginer;Here is my issue; I want to track down the implementation of some containers in stl, such as constructors, copy control members, and so on; to See how it is implemented; I copy a ...
YC_tao's user avatar
  • 1
0 votes
0 answers
62 views

why std::allocator::rebind doesn't cause recursion definition?

I'm confused in this piece code coming from GCC, why the other here doesn't cause recursion definition? It looks like related to meta-template programming, but I'm not sure. //ext/alloc_traits.h 206 ...
akanesora's user avatar
-1 votes
1 answer
116 views

C++: Adding a sleep causes deadlock

I have this code. std::thread t1(func1); std::this_thread::sleep_for( std::chrono::seconds( 2 ) ); std::thread t2(func2); std::thread t3(func3); t1.join(); t2.join(); t3.join(); In this code t1 ...
VinayakaB's user avatar
-5 votes
3 answers
86 views

what is the point of saying that the complexity of insert() and erase() in the list container is constant time

I'm learning the C++ standard library, and while learning about std::list I came across that the complexity of insert() and erase() is constant time. Now, at first, I didn't know how that is possible, ...
Mowafk Mha's user avatar
0 votes
2 answers
93 views

c++17 filesystem, concat multiple strings

this code std::filesystem::path = std::filesystem::current_path(); (((path += "str1") += "str2") += "str3"); looks so messy. How do I add multiple strings to a path ...
user3600124's user avatar
2 votes
2 answers
81 views

Is there any idiom way to cast between `unique_ptr<T>*` and `unique_ptr<const T>*`?

I have an array of type shared_ptr<T> (or unique_ptr<T>). I want to use this array as a parameter for a function which will not modify its contents, so I want the input parameter type to ...
Zz Tux's user avatar
  • 610
1 vote
1 answer
76 views

Will copy constructor be called when calling insert of std::unordered_map

I have a std::unordered_map and I found that the object I insert into it is different from that I got from it by using for range traversing. I doubt that there might be some object copying happening ...
Chansy's user avatar
  • 29
2 votes
0 answers
58 views

How does gcc std::set use stl_tree.h to store node keys?

I'm trying to implement some STL containers as an exercise. I have implemented a red-black-tree that takes as template parameter the type of the Key that the nodes will store. This has been enough to ...
edugomez102's user avatar
0 votes
1 answer
90 views

How to get the index of element in std::map?

I used std::map to save some data, the reason i used map is: i need to keep the key in order, I need to delete and insert element frequently. here is a demo: std::map<int, int> a; for (int i = 0;...
nothingisme's user avatar
-3 votes
1 answer
59 views

How to create vector on the fly and store it as vector of pointers?

I want to create vector on the fly and store it inside another vector as pointers. I am trying below 2 approaches. Approach 1: (crashing while accessing) std::vector<std::vector<int> *> ...
sarabjot singh's user avatar

1
2 3 4 5
309