15,485
questions
0
votes
1
answer
26
views
string::find_last_of with count parameter fails to find the target char [duplicate]
My business is to find the last occurrence of a char in a range of a large text, so I have to specify off and count to limit the range.
In this case, I'm trying to find the last space within 100 chars....
1
vote
2
answers
59
views
string::rfind with count parameter fails to find the target char
I'm trying to find the last occurrence of a char in a range from a large text, so I have to specify off and count to limit the range.
string s = "a abc test";
cout << s.find_last_of(&...
0
votes
1
answer
131
views
Why are lists<T> so much slower than vector<T*>?
The other day, I was playing with this quickbench: https://quick-bench.com/q/fwF5nkc4ORmYwSXSoVy2d-ekN5U
Basically, the code just measures the time it takes to iterate and sum over different cases : ...
1
vote
2
answers
285
views
Copy happened when pass char * to string?
I need to call a function, whose parameters are type string.
I thought std::string is a good choice, because it provide a lot of interface.
But recently, I found there is a copy happened when i pass ...
3
votes
1
answer
83
views
See hash distribution of std::unordered_map in C++
What is the best way to see how well my hash function is working in an unordered_map in C++? I am using Visual Studio 2022 and in the debugger there is no easy way of seeing the distribution within ...
0
votes
1
answer
56
views
In the c++ vector, why cannot we print iterator value individually?
In the following code, I get an error if I type either of the following two
cout << "value of it: " << vec.begin() << endl;
cout << "value of it: " <&...
-2
votes
1
answer
76
views
c++ unordered set range-erase bug
std::unordered_set<Vector2i> basicWalls = FindWalls(floorPositions, cardinal2D);
std::unordered_set<Vector2i> cornerWalls = FindWalls(floorPositions, Diagonal2D);
printf("basic ...
1
vote
1
answer
45
views
getting index of current tuple item in std::apply
there is a call of std::apply for std::tuple,
inside the lambda, there is another container: std::array, each element of that is an object and I need to call this member with a parameter - tuple value....
3
votes
1
answer
100
views
How to implement an array that can grow up to a limit and never allocates?
I need to implement a container which has most of the functionality of vector (especially, push_back, insert, erase and sort) but never allocates memory.
I want it to start with an internal buffer of ...
4
votes
2
answers
110
views
Move objects from a set to another set with a different comparison functor
I have a large number of large objects in my program. They are currently stored in an std::set with a customer comparison functor. The set starts empty and I keep emplacing objects into it. I also ...
2
votes
0
answers
66
views
How to deal with a third party DLL exporting STL symbols?
I am working on a project that is dependent on a third-party DLL. For reasons unknown, this DLL exports (most but not all) of the symbols for std::vector<unsigned char>:
$ dumpbin.exe -exports ...
4
votes
0
answers
71
views
What exception guarantee level does std::vector::operator=() have? [duplicate]
When I assign one std::vector to another std::vector, and std::bad_alloc raises internally, does the C++ specification guarantee that my vector will be left in the correct state? Does it have a basic ...
0
votes
1
answer
67
views
Allowing STL algorithms to swap when iterators dereference to a wrapper class (i.e. an rvalue)
I am implementing a C++ wrapper for a C library that has a "vector of vectors" container. In the C++ interface, I would like iterators to dereference not to a raw C pointer, but to a ...
0
votes
2
answers
118
views
C++ Rule of Zero with guaranteed/C ABI
I love and often use C++ Rule of Zero, but now there's a use case where I find it challenging to use.
I have 2 libraries, both maintained separately and even compiled with different compilers to ...
2
votes
2
answers
81
views
Unclear behaviour of std::next_permutation with std::wstring
Given this program to play around with Unicode, I found results that don't make sense to me:
#include <iostream>
#include <algorithm>
#include <cassert>
int main() {
std::locale:...
0
votes
1
answer
86
views
reference_wrapper in c++ containers
Consider following snippet:
#include <bits/stdc++.h>
using namespace std;
int main() {
int x=3;
int y=1;
int z=2;
cout<<(&x)<<' '<<(&y)<<' '<&...
1
vote
1
answer
77
views
Using an initializer list to init a vector
I can not figure out why it does not allow me to directly initialize a vector using the following syntax:
stl::vector<int> v{1, 2, 3};
I have implemented from scratch both the vector and the ...
0
votes
0
answers
59
views
set c++ own comparator function find could not find an already inserted element [duplicate]
I wrote an comparator function for my set which will store an vector of 3 elememts.
Those 3 elements will be sorted always.
Now i insert the 2 elements in the set. But when i try to find the 2nd ...
0
votes
1
answer
147
views
Address sanitizer shows a memory error in valid C++ code
#include <iostream>
#include <set>
#include <iterator>
using std::cout, std::endl;
using std::multiset;
int main() {
multiset<int> m;
m.insert(1);
m.insert(2);
...
-2
votes
1
answer
67
views
is it mandatory to specify size of the vector when we using vector of vector? [duplicate]
vector<vector<int>>res(size);
I was working on some problem when I run the code with size its gave correct result but without size it throws seg-fault
vector<vector<int>> ...
3
votes
0
answers
115
views
Efficient nested parallelism
I am transitioning to parallel STL from my own thread pool. One thing in need is efficient nested parallelism. Consider the following code
std::for_each(std::execution::par_unseq, begin, end, f(i));
...
1
vote
0
answers
217
views
Why does each Block in the MSVC implementation of std::deque have only 16 bytes? Can I modify it?
I learned this fact after reading Inside STL: The deque, implementation, and I am very shocked by it. 16 bytes!? That’s only twice the size of a pointer! I know that the major implementations of the ...
3
votes
2
answers
104
views
How to inplace initialize std::array<std::pair<const Key, Val>>?
I am trying to create a static map for where the type of the key is an enum, and the values and keys are stored inside an std::array.
The below code works nicely for trivially copiable types, but ...
2
votes
2
answers
98
views
Merge and sort while remembering types and (maybe) also positions
I have two std::vector<double> v1, v2 already sorted and without duplicates (for double's ==). Let's say that the numbers in v1 are of type 1 and that the numbers in v2 are of type two. How can ...
1
vote
0
answers
89
views
A question in writing is_container and output STL data
My code is
#include <type_traits>
#include <iostream>
template<typename T, typename = void>
struct is_container : std::false_type {};
template<typename T>
struct is_container ...
0
votes
1
answer
82
views
Is it possible to resize an xarray adaptor of a vector?
After seeing the example of xtensor matrix adaptors of 1D containers, I was wondering if it is also possible to have a resizeable adaptor of STL containers like vectors.
In the process, I would like ...
4
votes
3
answers
221
views
Why does std::vector have 2 constructors instead of 1 with default argument?
I looked at cppreference.com and found this
vector();
explicit vector( const Allocator& alloc );
why not just
explicit vector(const Allocator& alloc = Allocator());
1 constructor instead of ...
0
votes
1
answer
170
views
Errors when trying to get "import std;" to work in a CMake project
I have managed to get my own simple module to work by following Andreas Weis' great talk on getting started with modules, but I am having trouble with what I actually want to achieve, which is ...
5
votes
1
answer
149
views
Why is make_unique<T[N]>() illegal while make_shared<T[N]>() is legal?
The proposal for shared_ptr<T[N]> has this paragraph:
https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3920.html
The unfortunate occurence of unique_ptr having lost its support for U[N]...
0
votes
2
answers
79
views
Seamlessly using maps with different comparators
I was using an alias to refer to one of two maps:
map<int, int> & map_alias = use_map_a ? map_a : map_b;
but then changed map_b to use std::greater as its comparator (instead of the default ...
0
votes
0
answers
65
views
Read from txt file and write into map<int, string>
C++
I'm trying to read data from .txt file and store it in a map variable.
But when I try to print map, I get exactly 8 empty line.
I used gpt to create certain part of code (I noted it in comment ...
1
vote
3
answers
171
views
Why don't STL containers have methods for general funcitons?
I understand that there are general functions on iterators that accomplish everything you would want to do, such as std::find, std::count etc. but why don't the standard containers such as std::vector ...
0
votes
0
answers
49
views
ref counting with map based node objects and pointers
struct ptr{
const node *ptr; static size_t _c;
ptr(const node *_ptr = nullptr) : ptr(_ptr) { if(ptr) _c++; }
ptr(const ptr& rhs) { ptr = rhs.ptr; if(ptr) _c++;}
ptr(const ptr&...
0
votes
1
answer
61
views
adding pointers to keys in map in map
struct ndptr_t {
const pnode *ptr;
ndptr_t(const pnode* _p = nullptr) : ptr(_p) {}
~ndptr_t() {
// if no ndptr_t used outside map nid, clear nid
}
};
static std::map<...
0
votes
1
answer
42
views
Population a large table in random order efficiently
Given a 2D table consisting for example of
struct POD
{
int someData;
float someOtherData;
bool someMoreData;
// Other basic numerical types...
};
How would I efficiently populate a ...
-2
votes
1
answer
41
views
Multimap will not accept player inputted name
I'm currently trying to implement a leader board using multi-map which lets the player input their name.
it works when I give it a normal string but for some reason wont accept the player inputted ...
4
votes
1
answer
79
views
C++20: Standard-compliant way of getting the size of the allocation std::allocate_shared performs
I'm trying to use a boost::pool_allocator to (efficiently) allocate std::shared_ptr<T>. Conceptually, I want something like this:
struct Foo {};
std::shared_ptr<Foo> create() {
static ...
2
votes
0
answers
82
views
How to implement simple allocator for use with STL containers?
I have an allocator, that fine works with std::vector, but has compile errors with std::basic_string. There is my code:
#include <vector>
#include <string>
#include <iostream>
...
0
votes
0
answers
101
views
Deque vs list for LRU Cache Implementation
I was trying to implement LRU Cache for a Leetcode question. My final code is below and it works fine. However, my first choice was deque instead of list with exactly same procedure and I was getting ...
5
votes
1
answer
103
views
std::map/std::set and equal_range(): what's the reasoning here? [duplicate]
I just noticed that std::map and std::set have the member function equal_range() returning an iterator range of values for a certain key. How does this make sense when std::map and std::set are always ...
1
vote
2
answers
164
views
Why C++ allocators use reinterpret_cast and how to avoid it?
I was trying to implement my own small allocator for testing purposes, and while designing it I thought that I don't know how to implement it without violating strict aliasing rule.
In most open-...
0
votes
1
answer
90
views
Why there no constructors for associative containers to initialize a container whose size is defined when constructing it?
Sequence containers, such as vector, deque and list, all have constructors to create a container containing a specific number of elements.
std::vector:
//Constructs the vector with count copies of ...
0
votes
1
answer
44
views
Constructor for a struct with exactly N arguments, where N is a template parameter [duplicate]
I want to create a very small math library, which has good support for constexpr.
To simplify the problem, let's just talk about vectors here:
template <typename T, unsigned int N> struct vec {
...
0
votes
0
answers
77
views
How can I get data from binary file?
I get 'binary file' made with write() member function.
'binary file' have 2'000'000 of Player.
class Player {
string name; // name, length[3, 15]
int score; // score
...
0
votes
1
answer
28
views
Can IWYU be told to ignore headers altogether?
IWYU (include-what-you-use) normally demands that you directly include every header that exports every symbol that you are using. This is fine for your own project's headers and is actually the state ...
1
vote
1
answer
81
views
How to implement a stashing movable input iterator?
I have an input iterator that can walk through a file and parse records from it along the way. Each parsed record is stored in the iterator and owned by it ("stashed").
The records are not ...
0
votes
1
answer
72
views
Binary File Read with Large Data Set in C++ Throws std::bad_alloc
title:
Binary File Read with Large Data Set in C++ Throws std::bad_alloc
Body:
I'm working on a C++ project where I need to read approximately 2 million Player objects from a binary file and store ...
-1
votes
1
answer
79
views
How to write iterable class with sentinel to use it in find_if?
I have C++ code written around March 2023. It compiled fine back then. Now, I have to make a few changes to it, but it doesn't compile anymore. The used language standard was C++20. I don't remember ...
0
votes
2
answers
109
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
72
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>...