stl-algorithm

order a vector of points based on another vector

眉间皱痕 提交于 2019-12-04 03:11:49
问题 I am working on a C++ application. I have 2 vectors of points vector<Point2f> vectorAll; vector<Point2f> vectorSpecial; Point2f is defined typedef Point_<float> Point2f; vectorAll has 1000 point while vectorSpecial has 10 points. First Step: I need to order the points in vectorSpecial depending on their order in vectorAll. So something like this: For each Point in vectorSpecial Get The Order Of that point in the vectorAll Insert it in the correct order in a new vector I can do a double loop

Sort objects of dynamic size

狂风中的少年 提交于 2019-12-04 01:01:22
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 consists of a lexicographical comparison of most of the object data, with a bit of different

std::copy n elements or to the end

我只是一个虾纸丫 提交于 2019-12-03 01:25:28
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++ = *first++; --c; } return result; } is there a way to do this with std functions? I could also: template< class InputIt, class Size, class OutputIt> OutputIt myCopy_n(InputIt first, InputIt last, Size count, OutputIt result) { if(std::distance(first, last) > count) return std::copy_n(first,count,result); return std::copy(first,last,result); } however, besides being

Composability of STL algorithms

╄→尐↘猪︶ㄣ 提交于 2019-12-02 20:05:39
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<pair<int, int>> and want to transform that to a vector<int> containing only the second member of the pair. That's simple enough: std::vector<std::pair<int, int>> values = GetValues(); std::vector<int> result; std::transform(values.begin(), values.end(), std::back_inserter(result), [] (std::pair<int, int> p) { return p.second; }); Or maybe I want to filter the vector for only those pairs whose first member is even. Also pretty

use n_th element in a container, but with another key

空扰寡人 提交于 2019-12-02 04:39:00
I have two vectors. One that actually holds the data (let's say floats) and one that holds the indices. I want to pass at nth_element the indices vector, but I want the comparison to be done by the vector that actually holds the data. I was thinking about a functor, but this provides only the () operator I guess. I achieved that by making the data vector a global one, but of course that's not desired. std::vector<float> v; // data vector (global) bool myfunction (int i,int j) { return (v[i]<v[j]); } int find_median(std::vector<int> &v_i) { size_t n = v_i.size() / 2; nth_element(v_i.begin(), v

Why fill_n() does not work with vector.reserve()?

早过忘川 提交于 2019-12-01 20:30:40
I'm learning about the standard library algorithms recently and have a question about the function fill_n(iter, n, val) . This function requires the container has at least n elements starting from iter . Here is the testing code: // Version 1, Error vector<int> vec; vec.reserve(10); // Only allocate space for at least 10 elements fill_n(vec.begin(), 10, 0); // Version 2, OK vector<int> vec; vec.resize(10); // Value initialized 10 elements fill_n(vec.begin(), 10, 0); // Version 3, OK vector<int> vec; fill_n(back_inserter(vec), 10, 0); // Push back 10 elements via back_inserter Why the version 1

Passing a C++ Member Function Pointer to an STL Algorithm

别等时光非礼了梦想. 提交于 2019-12-01 18:36:23
问题 I have a member function as follows: class XYZ{ public: float function(float x); private: float m_DensityMin; float m_DensityMax; }; Now, I'm trying to transform a std::vector<float> foo using the std::transform STL algorithm by passing the member function function , and storing the resulting values in a vector bar . If I use the function as a global function, with random data that should denote the member variables of the class, it works fine. However, as the function requires the use of

Why does std::max_element require a ForwardIterator?

岁酱吖の 提交于 2019-12-01 16:06:08
The C++ standard library's max_element algorithm requires the iterators passed as inputs to model ForwardIterator . My understanding is that ForwardIterator refines InputIterator by specifying that you can use a ForwardIterator to iterate over the same range multiple times. Therefore, multi-pass algorithms require ForwardIterator s. However, max_element is not a multi-pass algorithm - it is sufficient to iterate over a range once to determine its maximum element. So why does max_element need the additional capabilities of ForwardIterator ? std::max_element returns an iterator to the maximum

order a vector of points based on another vector

僤鯓⒐⒋嵵緔 提交于 2019-12-01 15:55:45
I am working on a C++ application. I have 2 vectors of points vector<Point2f> vectorAll; vector<Point2f> vectorSpecial; Point2f is defined typedef Point_<float> Point2f; vectorAll has 1000 point while vectorSpecial has 10 points. First Step: I need to order the points in vectorSpecial depending on their order in vectorAll. So something like this: For each Point in vectorSpecial Get The Order Of that point in the vectorAll Insert it in the correct order in a new vector I can do a double loop and save the indexes. and then order the points based on their indexes. However this method is taking

set_union with multiset containers?

巧了我就是萌 提交于 2019-12-01 05:45:41
What's the return of the algorithm std:set_union when one or both input containers are multisets with duplicated objects? Do dups get lost? Let's suppose for example: multiset<int> ms1; ms1.insert(1); ms1.insert(1); ms1.insert(1); ms1.insert(2); ms1.insert(3); multiset<int> ms2; ms2.insert(1); ms2.insert(1); ms2.insert(2); ms2.insert(2); ms2.insert(4); vector<int> v(10); set_union( ms1.begin(), ms1.end(), ms2.begin(), ms2.end(), v.begin() ); What would the output be? From the standard, 25.3.5: The semantics of the set operations are generalised to multisets in a standard way by defining union(