Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
80 views

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 ...
o_oTurtle's user avatar
  • 1,271
5 votes
4 answers
315 views

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 ...
Elliott's user avatar
  • 2,730
1 vote
0 answers
148 views

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 ...
Benjamin Buch's user avatar
2 votes
1 answer
263 views

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: ...
callum arul's user avatar
1 vote
1 answer
918 views

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 ...
callum arul's user avatar
1 vote
1 answer
307 views

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 ...
lamc's user avatar
  • 367
0 votes
1 answer
70 views

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 ...
user avatar
0 votes
1 answer
216 views

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 ...
jozxyqk's user avatar
  • 17.7k
-1 votes
2 answers
278 views

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,...
revolutionary's user avatar
23 votes
5 answers
4k views

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 ...
dev-here's user avatar
  • 348
1 vote
1 answer
91 views

Using the following containers: std::vector<std::pair<std::string, int>> keyVals = { {"A", 1}, {"B", 2}, {"C", 3} }; std::vector<std::string> keys ...
johnco3's user avatar
  • 2,661
2 votes
1 answer
104 views

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++ ...
Alex Vergara's user avatar
  • 2,335
-1 votes
1 answer
353 views

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 ...
Sap BH's user avatar
  • 91
3 votes
3 answers
333 views

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 ...
cbrng's user avatar
  • 63
2 votes
2 answers
692 views

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. ...
user877329's user avatar
  • 6,296
5 votes
1 answer
218 views

I came up with this question after reading the excellent answer of std::next_permutation Implementation Explanation. Please refer to that post for an explanation of the algorithm used by STL, but I'll ...
nalzok's user avatar
  • 16.4k
3 votes
0 answers
57 views

Looking through the algorithm library, there are a few std::ranges::XXX_n function have basically the same definition as their std::XXX_n counterparts, namely copy_n, fill_n, and generate_n. The only ...
Ranoiaetep's user avatar
  • 6,767
0 votes
1 answer
121 views

Maybe I'm missing something. Is there a function in <algorithm> that does this? If not, what would you call it? It seems like a particular flavor of transform-reduce that is so specific it needs ...
Ben's user avatar
  • 9,893
0 votes
1 answer
187 views

I was just curious how some of the C++ algorithms check the range of the result/output container when you only provide the range of the input? For example, for the following code #include <iostream&...
ThomasF's user avatar
4 votes
1 answer
169 views

The following snipped does not compile with GCC 10 (Compiler Explorer link): #include <vector> #include <algorithm> #include <execution> struct T { int const ID; // Not ...
user avatar
5 votes
1 answer
680 views

In C++14, I have a std::vector of values for which I want to remove all elements that match a given value and I do not care about preserving the order of the elements after doing the remove. The ...
WilliamKF's user avatar
  • 43.6k
2 votes
2 answers
102 views

Short Version Currently, I manually create a lambda function for the new [], as follows: [](){ return new double[3]; } This lambda is later used as a generator function in the call to std::generate: ...
Anton Menshov's user avatar
4 votes
4 answers
4k views

C++20 added starts_with, ends_with to std::string. Is there a nice way to get it to be case insensitive? Note that perf matters so I do not want to lowercase/uppercase both strings(or std::min(len1, ...
NoSenseEtAl's user avatar
  • 30.9k
0 votes
1 answer
182 views

Why swap() in STL algorithm don't take address of a variable and passing variable work just fine but in C++ call by value don't change actual value but call by address changes actual parameter?
Sahil Verma's user avatar
2 votes
1 answer
215 views

Given this piece of code: struct T { void f(int const); }; void f(std::vector<T> &u, std::vector<int> const &v) { for (std::size_t i = 0; i < u.size(); ++i) u[i].f(v[i]...
user avatar
0 votes
1 answer
633 views

I have N numbers and I want to sort each number by digit. ( In my original problem I want to make the largest number by these (greedy approach)) For ex - If we have 5 numbers 9 1000 845 8000 56 In ...
Varun Hegde's user avatar
2 votes
1 answer
654 views

If I have a std::string: std::string s{"hello"}; and a loop that modifies it in-place, like this: for (auto &c: s) c = std::toupper(c); I can replace it with the equivalent transform: std::...
cigien's user avatar
  • 61.2k
1 vote
2 answers
1k views

I'd like to know if it is possible to use std::back_inserter to create a vector of only a single element from a more complex struct. For example, in the following code: struct Person { std::string ...
cbuchart's user avatar
  • 11.8k
0 votes
1 answer
169 views

I have a nested loop and I am hoping to rewrite it using STL algorithms. Can someone help me out? std::bitset<array_size * 8> bitset {}; short bitsetIndex {0}; for (int i = 0; i < array_size;...
Frogical's user avatar
  • 185
2 votes
0 answers
75 views

The following program does not compile: #include <algorithm> #include <array> #include <sstream> int main() { std::stringstream s; s << "1 2 3"; std::array<int,...
francesco's user avatar
  • 7,617
6 votes
1 answer
194 views

I am trying to use std::remove_if to remove spaces from a simple string, but I am getting weird results. Could someone help me figure out what's going on? The Code is: #include <iostream> #...
Paradox's user avatar
  • 2,045
3 votes
0 answers
95 views

I stumbled upon this while trying something equivalent to #include <vector> #include <algorithm> struct pred { //not copyable pred () {} bool operator()(int) const { return false;...
nnolte's user avatar
  • 1,790
26 votes
3 answers
1k views

Today, I arrived at a situation, where I have a vector of tuples, where the tuples might contain several entries. Now I wanted to convert my vector of tuples to a vector of objects, such that the ...
Aleph0's user avatar
  • 6,104
3 votes
1 answer
489 views

Consider: std::vector<double> u, v; #pragma omp parallel for for (std::size_t i = 0u; i < u.size(); ++i) u[i] += v[i]; To express similar code with the C++17 parallel algorithms, the ...
metalfox's user avatar
  • 6,536
0 votes
1 answer
409 views

I've tried copying the contents of a std::vector into a std::multiset like this: std::vector<unsigned> v(32768); std::generate(v.begin(), v.end(), rand); std::multiset<unsigned> m(v....
Matthewj's user avatar
  • 2,033
0 votes
1 answer
720 views

This is a part of the code, that I'm in trouble with #include<iostream> #include<fstream> #include<conio.h> #include<list> #include<iterator> #include<map> #...
Кристиян Илиев's user avatar
10 votes
1 answer
1k views

cppreference documentation https://en.cppreference.com/w/cpp/algorithm/for_each says that: If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is ...
alfC's user avatar
  • 16.8k
0 votes
2 answers
277 views

On Mac OSX, clang version 7.0.2 the code compiles. On Ubuntu clang version 7.0.0 it does not. Is there really a difference in some default library, this seems weird? MWE: #include <string> ...
Dov's user avatar
  • 8,644
0 votes
2 answers
461 views

I have a vector of QPointF and I need to find the minimum and maximum y values because I need to know what is the amplitude of the data in the vector. I use QPointF but for adding each new element I ...
DEKKER's user avatar
  • 933
0 votes
1 answer
658 views

I am trying to use std::upper_bound with a vector defined by the Eigen libraries. I get some errors on visual studio 2017 #include <stdafx.h> #include <stdio.h> #include <Eigen/Dense&...
SlowCar's user avatar
8 votes
1 answer
442 views

I have a parallel code that can be reduced to basically: #include <algorithm> #include <vector> struct TKeyObjPtr; class TObj { public: virtual void Calculate(TKeyObjPtr const &) =...
metalfox's user avatar
  • 6,536
4 votes
2 answers
221 views

I'm trying to generate a random unordered_multimap of size 10 using the following code: #include <algorithm> #include <unordered_map> #include <cstdlib> int main() { auto m = ...
Przemysław Czechowski's user avatar
2 votes
1 answer
141 views

I recently learned that ForwardIterators require operator * to return by reference, which means that iterators that return proxies, such as std::vector<bool>, cannot be ForwardIterators ([...
metalfox's user avatar
  • 6,536
-1 votes
2 answers
99 views

I have a vector of structs like this: struct Item { int id; string name; } vector<Item> v= ....; Now I need to see how many unique "id" are in the vector and create another vector ...
DEKKER's user avatar
  • 933
-1 votes
2 answers
266 views

I wish to sort the a std::map using the stl std::sort() but getting error on geeksforgeekside(cannot paste the whole error please see the link) #include <bits/stdc++.h> using namespace std; ...
John's user avatar
  • 29
0 votes
1 answer
497 views

I have a class Student with 2 member variables, one of which is grade. I create some students by the corresponding constructors and then put them in a list<Student>. I then want to use the sort()...
A.Antonov's user avatar
8 votes
3 answers
3k views

Is the behavior for passing an empty container to std::lower_bound defined? I checked cppreference.com and an old version of the C++ standard that I found online, but couldn't find a definite answer. ...
FelEnd's user avatar
  • 888
9 votes
4 answers
2k views

If I have something like: vector<int> longVector = { ... }; vector<int> newVector; transform(longVector.begin(), longVector.end(), back_inserter(newVector), [] (int i) { return ...
javidcf's user avatar
  • 59.9k
0 votes
1 answer
266 views

I have a sorted list of items below. I need to determine how to pick [inclusive, exclusive) item pairs from this list such that the difference between them exceeds some fixed value (for example 5 in ...
johnco3's user avatar
  • 2,661
2 votes
1 answer
606 views

I have a vector<int*> and I want to place all pointed at elements into a vector<int>. All non-pointed at elements are set to nullptr. I was thinking about doing something like this: ...
Adrian's user avatar
  • 11.1k

1
2 3 4 5
8