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,092
questions
-1
votes
0
answers
40
views
Checking rvalue insert result for std::map gives unexpected results
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
84
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 ...
0
votes
1
answer
46
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
66
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
100
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)
| ...
-4
votes
0
answers
56
views
Unusual Access Violation when joining a thread [closed]
When terminating my program, the many components join their threads and this particular one gives me an access violation when joined along with other components at the same time.
The code is simple - ...
7
votes
2
answers
140
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
117
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
63
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
115
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
98
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
156
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....
5
votes
1
answer
142
views
Why does take(n) used on an istream_view cause it to skip the next token in C++20?
In C++20, when I use std::ranges::views::take() on a std::ranges::istream_view(), the next token in the istream_view after iterating the take view is skipped.
Consider the following C++20 code snippet:...
3
votes
1
answer
125
views
Is Insertion into a vector while also accessing a vector undefined behavior?
Just stumbled upon a post that talked about reference and iterator invalidation on insertion into a vector.
But will accessing a vector by index also result in undefined behavior?
Is the below code ...