15,500 questions
3
votes
3
answers
115
views
Why does std::filesystem::absolute resolve . and .. on Windows but not on POSIX platforms?
I'm using C++17's std::filesystem::absolute to convert relative paths to absolute ones. I noticed that on Windows (MSVC), this function seems to resolve . and .. components in the path, but on Linux (...
-2
votes
1
answer
130
views
Removing elements from std::multimap [closed]
I'm using std::multimap with tens of millions of elements. The key is a pointer, and the data is a shared pointer to a class. Sometimes I need to remove a few elements, sometimes many elements, and ...
0
votes
1
answer
98
views
get iteration as reference is okay with stl functions? [duplicate]
const auto& it = container.find( value );
Is this code safe? I'm confused because find() returns a temporary object (iterator). Could it be dangling?
4
votes
1
answer
100
views
std::list with a custom allocator crashes when removing items
I wrote a memory allocator to use with standard containers. However, every time I try to remove an element from a std::list, the program crashes at the line l.remove(elt).
I spent time investigating ...
1
vote
0
answers
168
views
Nested std::vector<std::vector> brings too many memory fragmentation and pollutes the cache
In my projects, there are several std::vector<std::vector<int>> dynamic arrays. The first dimension is about 10 million, the second dimension is about 10-100. Both of the dimensions are ...
-1
votes
1
answer
85
views
Circular dependency where two containers store iterators to each other's elememts
I wrote quick impl of LRU cache, where all the data (eg key -> value pairs) is stored in unordered map, and the order list simply stored pointers to these items.
if it was regular C data structs, ...
2
votes
1
answer
105
views
Implementation of std::list::insert: casting away constness?
Lately I was implementing my own linked list class and emulating the behavior of std::list for practicing purpose. I came across a problem about constness when implementing the insert method. I've ...
1
vote
1
answer
104
views
Cannot convert <brace-enclosed initializer list> to class
I'm writing my custom initializer_list and pair. However, I met the converting fail problem:
error: could not convert ‘{test::pair<std::__cxx11::basic_string<char>, std::__cxx11::...
2
votes
2
answers
177
views
C++ How to convert a for loop to std::count_if
Here's a code that, for each element, counts the number of elements that are not connected to it.
bool conn[100][100];
for(int i=0; i<N; i++){
int cnt=0;
for(int j=0; j<N; j++) cnt+=!(conn[i]...
3
votes
3
answers
124
views
Will adding std::string items to std::deque with std::move be more efficient?
In this code:
// build string requiring a bunch of processing
std::wstring xmlstr=xml->GetXml();
{
std::lock_guard<std::mutex> guard(my_mutex);
m_deque.push_back(std::move(xmlstr)); // &...
1
vote
1
answer
102
views
Can liveliness analysis cause a c++ object to be destructed before leaving scope?
I was thinking of a situation e.g. like this:
void example() {
vector<unique_ptr<int>> v;
v.emplace_back(make_unique<int>(1));
int *a = v.back().get();
{
// rest of code, ...
0
votes
2
answers
62
views
How to replace some columns of a matrix using std::valarray and std::gslice
I have a set of consecutive numbers from 1 to 24. I interpret this set as a 4 by 6 matrix using std::valarray:
std::valarray va{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
0
votes
1
answer
92
views
C++/STL: get warnings about empty iterator ranges
I had a bug that boiled down to
vector<T> to_be_sorted;
std::sort(to_be_sorted.begin(), to_be_sorted.begin(), [](const auto &lhs, const auto &rhs) { /* sort code* /});
(yes, I passed ...
3
votes
3
answers
84
views
How do I initialize an inplace key value pair in std::map
I want to insert key value pairs into a std::map with uint32_t as the key type and a custom type for values. Based on the documentation of std::map, I wrote the following code:
#include <cstdint>...
0
votes
0
answers
38
views
C++ set with user defined type and custom compare [duplicate]
I have a class like this
class Point {
private:
int x;
int y;
public:
Point() = default;
Point(int x_arg, int y_arg):x(x_arg), y(y_arg) {};
bool operator<(const Point& ...