stl-algorithm

std::next_permutation Implementation Explanation

廉价感情. 提交于 2019-11-26 15:35:27
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 <vector> #include <iostream> #include <algorithm> using namespace std; template<typename It> bool next_permutation(It begin, It end) { if (begin == end) return false; It i = begin; ++i; if (i == end) return false; i = end; --i; while (true) { It j = i; --i; if (*i < *j) { It k = end; while (!(*i < *--k)) /* pass */; iter_swap(i, k); reverse(j, end); return true; } if (i == begin) { reverse(begin, end);

STL algorithms: Why no additional interface for containers (additional to iterator pairs)?

一个人想着一个人 提交于 2019-11-26 11:26:32
问题 I\'m wondering why the STL doesn\'t overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass begin + end iterators. I of course understand why we also want to use an iterator pair for processing subsequences of a container / array, however, almost all calls to these methods are using a whole container: std::for_each(myVector.begin(), myVector.end(), doSomething); I\'d find it more convenient, readable and

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

做~自己de王妃 提交于 2019-11-26 10:48:41
问题 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 : #include <vector> #include <algorithm> using namespace std; struct ha { int i; explicit ha(int a) : i(a) {} }; int main() { vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector // GOAL : make a vector of pointers to elements with i < 2 vector

Using local classes with STL algorithms

我是研究僧i 提交于 2019-11-26 08:26:35
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 local types to be used as arguments ' Example code: int main() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<int> v( array, array+10 ); struct even : public std::unary_function<int,bool> { bool operator()( int x ) { return !( x % 2 ); } }; std::remove_if( v.begin(), v.end(), even() ); // error } Does anyone know where in the standard

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

混江龙づ霸主 提交于 2019-11-26 08:16:07
问题 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 the username I want to test and overloading the == operator and then trying to use find/find_if on the object: std::vector<Nick> userlist; std::string username = \"Nicholas\"; if (std::find(userlist.begin(), userlist.end(), new Nick(username, false

Using local classes with STL algorithms

旧时模样 提交于 2019-11-26 01:59:33
问题 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 local types to be used as arguments \' Example code: int main() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<int> v( array, array+10 ); struct even : public std::unary_function<int,bool> { bool operator()( int x ) { return !( x % 2