std Questions

3

EDIT: total re-edit because the original was becoming an unstructured mess :) Thanks for everyone's input so far; I hope I worked it into the text below. Question I'm in search for a lazily-creat...
Nedranedrah asked 17/9, 2015 at 21:29

5

Solved

std::unordered_map<std::string, bool> str_bool_map = { {"a", true}, {"b", false}, {"c", true} }; can we use std::any_of on this map to see any of its values is false? Or any of its key i...
Calise asked 7/1, 2020 at 8:11

2

Solved

unsigned int Tick = GetTickCount(); This code is running only on Windows, but I want to use the C++ Standard library so it can run elsewhere. I searched std::chrono, but I can't find a function ...
Diminution asked 25/7, 2014 at 13:57

8

I'm trying to read a line using the following code: while(fscanf(f, "%[^\n\r]s", cLine) != EOF ) { /* do something with cLine */ } But somehow I get only the first line every time. Is this a ba...
Asbestosis asked 14/5, 2009 at 6:12

2

The "four basic kinds" of C++ sequence containers (vector, forward_list, list, and deque) all have an assign method. For example, std::vector has (quoting cppreference.com): void as...
Lyric asked 4/8 at 19:51

2

Solved

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&l...
Massingill asked 10/7 at 11:44

1

Solved

I've reduced my code to the following example, which fails -O3 C++20 compilation on my g++ (x64 12.3) as well as apparently 14.1 when using godbolt: Again according to godbolt, clang works without ...
Testimony asked 22/5 at 10:26

2

Solved

I would like to know what's the best practice to remove an element from a vector in C++. I have seen many times people using std::remove to find and delete the element, and then using erase to rem...
Klausenburg asked 3/6, 2014 at 9:29

1

I know about this question, but mine is a bit different. Why does rehash have quadratic complexity, but operator [] (which can call rehash) has linear complexity in worst case? Sorry, but I don't h...
Hep asked 9/2 at 18:40

19

Solved

How do I replace part of a string with another string using the standard C++ libraries? QString s("hello $name"); // Example using Qt. s.replace("$name", "Somename"); ...
Quiles asked 5/8, 2010 at 19:6

0

When I allocate large space with std::vector it takes significant time because std::vector default-initializes the objects in it. I see that a long time ago people discussed the topic in the ...
Desultory asked 21/3 at 22:8

4

Solved

I wanted to do this #include <vector> #include <span> struct S { std::vector<int> v; void set(std::span<int> _v) { v = _v; } }; But it does not compile. What are the ...
Ehrman asked 1/9, 2020 at 10:46

5

I am beginning with c++11, constexpr and template metaprogramming seems a nice way to save scarce ram on tiny microcontroler. Is there a way to write a template to flatten a list of constexpr arr...
Gosling asked 31/7, 2014 at 20:37

11

Solved

I'm trying to declare a priority_queue of nodes, using bool Compare(Node a, Node b) as the comparator function (which is outside the node class). What I currently have is: priority_queue<Node,...
Partner asked 19/4, 2013 at 18:33

6

How do I write a template function that operates on a arbitrary container of a arbitrary type? For example how do I generalize this dummy function template <typename Element> void print_size...
Displacement asked 26/9, 2011 at 12:55

2

Solved

I need to construct a multi-string (REG_MULTI_SZ) in C++ from a list of values represented in a std::vector<std::wstring>. This is what first came to mind but was of course incorrect: std::ws...
Boyhood asked 24/1 at 15:41

3

I would like to swap two arrays of ints of some fixed size. Counterintuitively, the following does not compile because no matching instance of swap has been found. #include <array> #include &...
Organism asked 19/1 at 9:3

3

Solved

If there is a std::map<std::pair<std::string, std::string>, some_type> what is the best way to find its values? I guess the most obvious one is to do something like this: map.find(std::...
Obscuration asked 14/1 at 0:48

5

Solved

I tried to use the operator[] access the element in a const map, but this method failed. I also tried to use at() to do the same thing. It worked this time. However, I could not find any reference ...
Volkan asked 27/2, 2011 at 17:17

2

I have a variable called last_timestamp_ which is declared as follows: using TimePoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<double>> TimePoint last...
Peccable asked 9/6, 2021 at 7:2

5

Solved

I have a string like this: std::string string1 = "xjdfhfakdjs%54k34k.-jk34"; I need to get only ""xjdfhfakdjs", but the string is dynamic, not hardcoded so I don't know what is it, the length et...
Nimble asked 1/5, 2012 at 1:11

1

Solved

In libstdc++ and libc++ the internal nodes for lists (e.g. list and forward_list) and other trees are constructed in (at least) two parts: a node base class; and the node class itself. For example,...
Kerley asked 27/5, 2020 at 8:58

2

Solved

If I want to write a function summing two std::array, I would do something like: template<class T, std::size_t N> auto sum(const std::array<T, N>& a, const std::array<T, N>&am...
Nonet asked 28/11, 2023 at 14:27

2

Solved

I wanted to use the parallel version of std::sort where I can specify an execution policy like std::execution::par_unseq. I'm currently using clang++-10 and g++ 7.5.0 under Ubuntu Linux, but both d...
Paraphrast asked 5/6, 2021 at 11:3

4

Solved

I'm trying to figure out a nice way to find the index of a certain object in a vector - by comparing a string to a member field in the object. Like this: find(vector.begin(), vector.end(), [obje...
Wapiti asked 20/3, 2013 at 7:49

© 2022 - 2024 — McMap. All rights reserved.