373 questions
1 vote
1 answer
80 views
Can output range of algorithms in <numeric> in C++ overlap with the input range?
There are six algorithms that will output a range(which is denoted by an output iterator) in <numeric>: adjacent_difference, partial_sum, inclusive_scan, exclusive_scan, transform_inclusive_scan ...
5 votes
4 answers
316 views
Is there a safe (defined behaviour) way to use the STL to reduce the boilerplate of efficiently filtering a vector based off its indices?
I very often find myself wanting to filter a vector based on its index rather than its values. auto some_values = std::vector{1, 0, 4, 6, 2}; // Somewhere I figure out which items to remove. // This ...
1 vote
0 answers
148 views
How can I call ExecutionPolicy algorithms in a constexpr context?
I want to call standard library algorithms with the ExecutionPolicy for vectorization. At the same time the call should also work in a constexpr context. Unfortunately the ExecutionPolicy overloads of ...
2 votes
1 answer
263 views
Flatten vector of classes which contain vectors of structs
I have a vector of a class Planters which contain vectors of Plants. My goal is to return a vector of plants containing the plants from planter 1, followed by plants from planter 2, etc. example: ...
1 vote
1 answer
919 views
How to filter and transform cpp vector to another type of vector?
I have a class called InfoBlob and two enums called Action and Emotion. My function is supposed to take in a vector<InfoBlobs> blobs, and return a vector<Action> actions, corresponding to ...
1 vote
1 answer
307 views
STL algorithm and (end of) arrays
I am new to C++. I was trying using the accumulate algorithm from the numeric library. Consider the following code segment. int a[3] = {1, 2, 3}; int b = accumulate(a, a + 3, 0); It turns out that ...
0 votes
1 answer
70 views
How should I assume which iterator category an algorithm uses?
Let's say : std::sort(beg1, beg2, pred); This algorithm takes a range of iterators for the container and a predicate. It takes an LegacyRandomAccessIterator. I do understand the the 5 iterator ...
0 votes
1 answer
216 views
STL algorithm to get a per-vector-component min/max
I have a std::vector<vec3> points where vec3 has float x, y, z. I want to find the min/max bounds of all the points. I.e. the min and max of all vec3::x, vec3::y, vec3::z separately in the ...
-1 votes
2 answers
278 views
How to replace a for-loop with STL based algorithm or range based loop?
Given a std::vector of vertices containing some 3d integer data: struct Vertex{ int x; int y; int z; } std::vector<Vertex> vertices = {{10, 10, 10}, {20,20,20}, {30,30,30}, {40,40,...
23 votes
5 answers
4k views
How can I convert std::vector<T> to a vector of pairs std::vector<std::pair<T,T>> using an STL algorithm?
I have a vector of integers: std::vector<int> values = {1,2,3,4,5,6,7,8,9,10}; Given that values.size() will always be even. I simply want to convert the adjacent elements into a pair, like ...
1 vote
1 answer
93 views
how to create custom intersected container from 2 distinct container types
Using the following containers: std::vector<std::pair<std::string, int>> keyVals = { {"A", 1}, {"B", 2}, {"C", 3} }; std::vector<std::string> keys ...
2 votes
1 answer
104 views
C++ loop breaked 'cause the std::find algorithm
I have the next C++ code snippet: ... static constexpr const char* my_char_array [10] { // Some literals here... } // Member of a class std::vector<std::string> splitted_input { // Contains C++ ...
-1 votes
1 answer
353 views
Lambda function, arguments and logic in c++ [duplicate]
I am new to using lambda functions in C++. I have been researching the web, and found several articles, explaining the syntax and purpose of lambda function, but I have not come found articles which ...
3 votes
3 answers
334 views
How to erase non-alpha chars and lowercase the alpha chars in a single pass of a string?
Given a string: std::string str{"This i_s A stRIng"}; Is it possible to transform it to lowercase and remove all non-alpha characters in a single pass? Expected result: this is a string I ...
2 votes
2 answers
696 views
Standard algorithm to operate on adjacent elements
std::adjacent_find looks for the first two consecutive elements that satisfies the given predicate. I am looking for other algorithms that also has a predicate that takes the (previous, current) pair. ...