Questions tagged [std]
The C++ Standard Library, and its namespace. Use it in conjunction with: [c++], [c++11], [c++14], [c++17], [c++20], [c++23].
6,094
questions
-8
votes
0
answers
70
views
Why does caching/memoization in a sorting comparator increase runtime instead of reducing it? [closed]
I'm solving a problem on Leetcode where I need to sort an array of integers based on their mapped values using a custom digit mapping. I’ve implemented two versions of the solution to address this ...
1
vote
3
answers
110
views
Is std::move_only_function thread safe?
Is it safe to call a std::move_only_function object in one thread and replace the function it points to in another thread?
My code:
#include <future>
#include <functional>
int main() {
...
1
vote
1
answer
83
views
Standard queue misunderstanding
I have a simple C++ structure that build a std::queue with argv from the main.
It then provide a method ::cunext to return current value before poping it from the queue.
It appears that if I call this ...
-2
votes
1
answer
64
views
Checking rvalue insert result for std::map gives unexpected results [duplicate]
Test code snippet below (CHECK is macro from test framework and works fine):
typedef std::map<int, double> M;
typedef M::value_type VT;
const VT v0;
M mm;
CHECK(mm.begin() == mm.insert(v0)....
0
votes
2
answers
90
views
How to binary search a std::vector BUT that return a RandomAccessIterator?
I'm looking for a log2(N) way of searching a std::vector and receive a RandomAccessIterator. I want it to work exactly as std::lower_bound() but return a RandomAccessIterator instead of a ...
1
vote
1
answer
54
views
Design a Data Structure for Efficient Key-Value Operations with Top K Check in c++
Data Structure Requirements
Design a data structure that supports the following operations:
Insertion: Insert a key-value pair.
Deletion: Delete a key-value pair.
Check Top K: Given an integer k and ...
0
votes
0
answers
69
views
std::unordered_set with memory location control?
I would like to have a data structure that stores items of some type 'Key' but additionally provides an access to the memory location where it is stored. More specifically, consider the following ...
2
votes
1
answer
108
views
In C++ std::ranges, how do I build a map out of a views::join result?
I'm using GCC 14 in C+23 mode. In the following code, I create a view of views of pairs, which I then flatten with views::join and put into a vector:
auto c = std::ranges::views::iota(1, 5)
| ...
7
votes
2
answers
146
views
Does it make any sense to define operator< as noexcept?
I know that it makes perfect sense to define e.g. move constructor as noexcept (if possible) and I think I understand the effect of that.
But I have never seen a similar discussion about operator<()...
-2
votes
0
answers
46
views
Replacing direct usage of std::_Dist_type from old msvc100 code for msvc142
I'm working on upgrading some old c++ code from msvc100 to msvc142. My problem is I am not a C++ developer, but come from C#, and I'm running into a problem with a specific type that was used directly ...
4
votes
2
answers
119
views
Idiomatic ways of using tuples and std::tie
I am trying to implement the modular multiplicative inverse using the extended Euclidean algorithm in C++. In Python, the code for this is concise and idiomatic, using tuple unpacking:
def inv_modulo(...
-1
votes
0
answers
64
views
Initializing vector and array with initializing list of objects that only have move constructor [duplicate]
For the following class with only move constructor:
class Foo {
public:
Foo(const Foo& foo) = delete;
Foo(Foo&& foo) = default;
Foo() = default;
};
Why would initializing a ...
5
votes
2
answers
117
views
Is it undefined behavior to directly remove elements from the underlying range of filter_view?
For the following code:
void foo(std::set<int>& s) {
for (int val : s | std::views::filter([](int i) { return i > 0; })) {
s.erase(val - 3);
}
}
This code is not intended ...
1
vote
1
answer
99
views
Constructor with initializer list for std::map
Following the advice of not extending std containers from this question , I am creating my own type and using std::map as a class attribute.
I am re-creating the public interface functions I want from ...
0
votes
2
answers
159
views
C++ vector: for loop to std::transform [duplicate]
I got a code in the following form
vector<int> vec{ 1,2,3 };
vector<int> vec2{ 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < vec2.size(); i++) {
if (vec2[i] % 2 == 0)
vec....