stl-algorithm

std::is_sorted and strictly less comparison?

房东的猫 提交于 2020-01-01 08:33:38
问题 I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference, it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What

std::is_sorted and strictly less comparison?

别等时光非礼了梦想. 提交于 2020-01-01 08:33:18
问题 I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference, it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What

STL sort function in C++ wrt strings

别来无恙 提交于 2019-12-31 06:59:46
问题 So I've been trying to sort a string based on the frequency of its characters. However the online judge I've been using shows me the error Line 17: invalid use of non-static member function 'bool olution::helper(char, char)' Why is the call to my function wrong? I have used the sort() function before, but not to strings. Is my helper() function incorrect? class Solution { public: unordered_map<char,int> freq; bool helper(char c1,char c2){ if(freq[c1]>freq[c2]) return false; else return true;

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

时光总嘲笑我的痴心妄想 提交于 2019-12-27 11:43:29
问题 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 insertion is out of my control, so I cannot check for duplicates before inserting. int unsortedRemoveDuplicates(std::vector<int> &numbers) { std::set<int> uniqueNumbers; std::vector<int>::iterator allItr = numbers.begin(); std::vector<int>::iterator

Choosing specific objects satisfying conditions

淺唱寂寞╮ 提交于 2019-12-24 09:24:28
问题 Let's say I have objects which look very roughly like this: class object { public: // ctors etc. bool has_property_X() const { ... } std::size_t size() const { ... } private: // a little something here, but not really much }; I'm storing these objects inside a vector and the vector is rather small (say, at most around 1000 elements). Then, inside a performance critical algorithm, I would like to choose the object that both has the property X and has the least size (in case there are multiple

Parallel algorithm to sum-assign the elements of a vector to the elements of another one

时间秒杀一切 提交于 2019-12-23 20:49:32
问题 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 solution I found so far is to use the two input ranges version of std::transform : std::transform(std::execution::par_unseq, std::begin(u), std::end(u), std::begin(v), std::begin(u), std::plus()) which I don't like at all because it bypasses the += operator of my types and in my real use case leads to much more

std::back_inserter needs const_reference on older GCC. Why?

我只是一个虾纸丫 提交于 2019-12-23 11:46:41
问题 I am currently looking at some code that can be compiled on newer versions of GCC but not on older ones. In my case I am using a std::back_inserter to std::copy some data from one data structure to a custom data structure. If I forget the typedef value_type & const_reference typedef in this custom data structure however, this will not compile on GCC 4.4. The same code compiles and runs just fine on GCC 4.5. What is the difference in between these two compiler versions, that makes the code

std::copy n elements or to the end

五迷三道 提交于 2019-12-20 11:22:33
问题 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)

use n_th element in a container, but with another key

泪湿孤枕 提交于 2019-12-20 04:36:11
问题 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

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

寵の児 提交于 2019-12-20 02:28:32
问题 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