stl-algorithm

std::next_permutation Implementation Explanation

做~自己de王妃 提交于 2019-12-17 03:21:46
问题 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))

How to detect and save cyclic connectivity in edge vertices (hole detection)?

你。 提交于 2019-12-13 20:26:50
问题 thanks for taking the time to read my question. I am working on detecting holes in a triangular mesh and fill them with new triangles. I have done some of the parts that are, to get a list of edge vertices, etc. Following are the vertices/edges that make holes, please have a look at the image. (9, 62) => vertex # 9 and 62 makes an edge (left hole) (66, 9) => vertex # 66 and 9 makes an edge (left hole) (70, 66) => vertex # 70 and 66 makes an edge (left hole) (62, 70) => vertex # 62 and 70

Are Standard Library algorithms allowed to copy predicate arguments?

喜你入骨 提交于 2019-12-13 14:12:04
问题 Suppose we'd like to remove duplicate values from a vector of int s. The usual solution is to sort the vector and erase duplicates with erase-remove idiom. But we need to mantain the order of the elements that will not be removed, so we can't sort. So one might come up with a predicate like this and use with with remove_if algorithm: struct comp { std::set<int> s; comp() : s() {} bool operator()(int i) { return !(s.insert(i)).second; } }; But this will break if predicate object will be copied

How to find all matching numbers, that sums to 'N' in a given array

时光怂恿深爱的人放手 提交于 2019-12-12 21:08:35
问题 My goal here is to find all possible combinations that sums to a given total. For example, if the array is 2 59 3 43 5 9 8 62 10 4 and if the total is 12, then possible combinations are 2 10 3 9 8 4 5 3 4 Here is the first set of code, that I've written. Wondering the best improvements that can be done on this. int find_numbers_matching_sum(int *number_coll, int total) { int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total); int location = search_till - number_coll; if (

How do I sort a vector with respect to another vector?

核能气质少年 提交于 2019-12-11 18:38:58
问题 I have few vectors with same data type as. v < int > = {5,4,1,2} v2 < int > = {2,4,3,5,1,6,8,7} v3 < int > = {1,4,2,3} There is any way to sort vector v2 , v3 ... with respect to vector v using STL of C++(algorithm) so that after sorting v2 would be {5,4,1,2,3,6,7,8} when it's sorted with respect to v and v3 would be {4,1,2,3} when it's sorted with respect to v . Edit: It may be unclear for some people. let me explain .. sorted vector has two parts , one is A and another one is B . A contains

Removing characters from a string using erase and remove

别说谁变了你拦得住时间么 提交于 2019-12-11 06:09:39
问题 I found this solution on Stack Overflow and other forums for removing characters from a string. Say I wanted to remove the white spaces from a string I'd do: currentLine.erase( std::remove( currentLine.begin(), currentLine.end(), ' ' ), currentLine.end() ); where currentLine is the name of the string. This sort of thing appears to work for people but if I use it I get: /local/yrq12edu/Desktop/Bens_C++_Utilities/simuPOPtoFASTA/simuPOP2FASTA.cpp|54|error: cannot convert 'std::basic_string<char>

How is the 'equal' template function implemented? (predicate version)

一世执手 提交于 2019-12-10 23:19:52
问题 I am working through the book "Accelerated C++" and one of the exercises require us to emulate the 'equal' function in the header and so far I have implemented the simple version which takes three parameters as follows: template <class iterType1, class iterType2> bool cequal(iterType1 begin, iterType1 end, iterType2 e){ while(begin != end){ if(!(*begin == *e)) return false; ++begin; ++e; } return true; } and the second version which can accept a fourth parameter... template <class iterType1,

Use and utility of std::shuffle?

微笑、不失礼 提交于 2019-12-10 15:12:56
问题 If you look to the specifications of random shuffle in C++11, there are 3 functions. My question is what is the typical use and advantage of : template< class RandomIt, class URNG > void shuffle( RandomIt first, RandomIt last, URNG&& g ); compared to: template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); I mean, it seems that whatever URNG is (a uniform distribution), the result will be the same (from a statistical point of view). The only point I see, is that std:

Sort objects of dynamic size

半腔热情 提交于 2019-12-09 14:43:16
问题 Problem Suppose I have a large array of bytes (think up to 4GB) containing some data. These bytes correspond to distinct objects in such a way that every s bytes (think s up to 32) will constitute a single object. One important fact is that this size s is the same for all objects, not stored within the objects themselves, and not known at compile time. At the moment, these objects are logical entities only, not objects in the programming language. I have a comparison on these objects which

Why the sequence-operation algorithms predicates are passed by copy?

南楼画角 提交于 2019-12-08 17:43:27
问题 I'm wondering why functors are passed by copy to the algorithm functions: template <typename T> struct summatory { summatory() : result(T()) {} void operator()(const T& value) { result += value; std::cout << value << "; ";}; T result; }; std::array<int, 10> a {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }}; summatory<int> sum; std::cout << "\nThe summation of: "; std::for_each(a.begin(), a.end(), sum); std::cout << "is: " << sum.result; I was expecting the following output: The summation of: 1; 1; 2;