Questions tagged [stl-algorithm]

A collection of algorithms defined as template functions in the header of C++'s standard library. These functions are designed to work with ranges of elements usually in the form of iterators. They are designed to be flexible and reusable, encompass a wide scope of functionality, and adhered to certain performance guarantees.

A collection of algorithms defined as template functions in the <algorithm> header of the C++ standard library. These functions are designed to work with ranges of elements usually in the form of iterators. They are designed to be flexible and reusable, encompass a wide scope of functionality, and adhered to certain performance guarantees.

357 questions
116
votes
5 answers

std::next_permutation Implementation Explanation

I was curious how std:next_permutation was implemented so I extracted the the gnu libstdc++ 4.7 version and sanitized the identifiers and formatting to produce the following demo... #include #include #include using…
Andrew Tomazos
  • 58,923
  • 32
  • 156
  • 267
99
votes
5 answers

how to find the intersection of two std::set in C++?

I have been trying to find the intersection between two std::set in C++, but I keep getting an error. I created a small sample test for this #include #include #include #include using namespace std; int main()…
ILikeTacos
  • 13,823
  • 16
  • 52
  • 81
87
votes
9 answers

Why is there no transform_if in the C++ standard library?

A use case emerged when wanting to do a contitional copy (1. doable with copy_if) but from a container of values to a container of pointers to those values (2. doable with transform). With the available tools I can't do it in less than two steps :…
Nikos Athanasiou
  • 24,831
  • 11
  • 72
  • 136
68
votes
4 answers

What is the difference between std::transform and std::for_each?

Both can be used to apply a function to a range of elements. On a high level: std::for_each ignores the return value of the function, and guarantees order of execution. std::transform assigns the return value to the iterator, and does not guarantee…
bendervader
  • 2,329
  • 2
  • 17
  • 21
54
votes
4 answers

Why are std::shuffle methods being deprecated in C++14?

According to the cppreference.com reference site on std::shufle, the following method is being deprecated in c++14: template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); Why will we no longer be able to call the following…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
50
votes
2 answers

Using local classes with STL algorithms

I have always wondered why you cannot use locally defined classes as predicates to STL algorithms. In the question: Approaching STL algorithms, lambda, local classes and other approaches, BubbaT mentions says that 'Since the C++ standard forbids…
David Rodríguez - dribeas
  • 192,922
  • 20
  • 275
  • 473
41
votes
5 answers

Composability of STL algorithms

The STL algorithms are a pretty useful thing in C++. But one thing that kind of irks me is that they seem to lack composability. For example, let's say I have a vector> and want to transform that to a vector containing only the…
Sven
  • 19,833
  • 3
  • 50
  • 60
31
votes
3 answers

What does std::includes actually do?

From the standard, std::includes: Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1). Returns false otherwise. Note: as this is under [alg.set.operations], the…
Justin
  • 21,374
  • 12
  • 83
  • 129
30
votes
4 answers

C++ implementation explained

When I like to know how a algorithm in the C++ Standard Library could be implemented, I always look at http://en.cppreference.com/w/cpp/algorithm, which is a great source. But sometimes I don't understand some implementation details and I would need…
Christian Ammer
  • 7,082
  • 6
  • 41
  • 98
29
votes
6 answers

How to use std::find/std::find_if with a vector of custom class objects?

I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object included with the same username I pass in. I did a few attempts by trying to create a new Nick object for…
Speed
  • 1,364
  • 1
  • 15
  • 23
26
votes
3 answers

Uniform initialization by tuple

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 entries of the tuples will exactly match the uniform…
Aleph0
  • 5,058
  • 4
  • 20
  • 55
26
votes
8 answers

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
24
votes
4 answers

std::copy n elements or to the end

I would like to copy up to N elements. template< class InputIt, class Size, class OutputIt> OutputIt myCopy_n(InputIt first, InputIt last, Size count, OutputIt result) { Size c = count; while (first != last && c > 0) { *result++ =…
aivision2020
  • 569
  • 4
  • 14
22
votes
5 answers

Is there a safe alternative to std::equal?

std::equal() is unsafe because the function cannot know whether it will overrun the length of the second container to be compared. That is: std::vector< int > v( 100 ); std::vector< int > w( 10 ); bool same = std::equal( v.begin(), v.end(),…
OldPeculier
  • 9,811
  • 8
  • 42
  • 71
21
votes
1 answer

Difference between std::merge and std::inplace_merge?

What is the difference between std::merge and std::inplace_merge in terms of complexity and result when it is executed on two consecutive ranges with elements that are all different ? (I am not a native english speaker and I am not sure to clearly…
Vincent
  • 50,257
  • 51
  • 171
  • 339
1
2 3
23 24