stl-algorithm

std::sort() on a vector of Class pointers

╄→尐↘猪︶ㄣ 提交于 2021-01-26 07:13:34
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

std::sort() on a vector of Class pointers

时光总嘲笑我的痴心妄想 提交于 2021-01-26 07:12:33
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

std::sort() on a vector of Class pointers

本秂侑毒 提交于 2021-01-26 07:12:32
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

Replacing std::transform, inserting into a std::vector

走远了吗. 提交于 2020-07-22 22:07:42
问题 I'm looking to insert values into a std::vector in a way like std::transform. std::transform needs a pre-sized third argument, but in my case, the size depends on transformers() and is not predictable. ... // std::vector<int> new_args(); <-- not working std::vector<int> new_args(args.size()); std::transform(args.begin(),args.end(),new_args.begin(),transformers()); Is there a std:transform-ish way to insert values into a std::vector? 回答1: You do not need to pre size the vector that is going to

Replacing std::transform, inserting into a std::vector

故事扮演 提交于 2020-07-22 22:07:06
问题 I'm looking to insert values into a std::vector in a way like std::transform. std::transform needs a pre-sized third argument, but in my case, the size depends on transformers() and is not predictable. ... // std::vector<int> new_args(); <-- not working std::vector<int> new_args(args.size()); std::transform(args.begin(),args.end(),new_args.begin(),transformers()); Is there a std:transform-ish way to insert values into a std::vector? 回答1: You do not need to pre size the vector that is going to

Using ranges::view::iota in parallel algorithms

烂漫一生 提交于 2020-06-25 09:40:07
问题 Since there is no index based parallel for algorithm in c++17, I'm wondering if ranges::view::iota can be used in combination with std::for_each to emulate that. That is: using namespace std; constexpr int N= 10'000'000; ranges::iota_view indices(0,N); vector<int> v(N); for_each(execution::par_unseq,indices.begin(),indices.end(),[&](int i) { v[i]= i; }); iota_view seems to provide random access for appropriate types ([range.iota.iterator]): iota_view<I, Bound>::iterator::iterator_category is

Converting Between std::bitset and std::vector<bool>

前提是你 提交于 2020-04-09 19:22:30
问题 I have a std::bitset but now I want to use an STL algorithm on it. I could have used std::vector<bool> instead, but I like std::bitset 's constructor and I want std::bitset 's bitwise operations. Do I have to go through a loop and stuff everything in a std::vector<bool> to use STL algorithms, and then copy that back to the std::bitset , or is there a better way? 回答1: If you do not want to write loops using the operator[] of the bitset , then you might try using bitset::to_string() to convert

Find last element in std::vector which satisfies a condition

痞子三分冷 提交于 2020-01-03 04:53:27
问题 I have this requirement to find the last element in the vector which is smaller than a value. Like find_first_of but instead of first i want last. I searched and found that there is no find_last_of but there is find_first_of. Why is that so? Is the standard way is to use find_first_of with reverse iterators? 回答1: Use reverse iterators, like this: #include <iostream> #include <vector> int main() { std::vector<int> v{1,2,42,42,63}; auto result = std::find_if(v.rbegin(), v.rend(), [](int i) {

LCS ALGORITHM ( example )

…衆ロ難τιáo~ 提交于 2020-01-03 02:32:07
问题 There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences. How can I find the LCS algorithm of two sequences X and Y. (Test of correctness) (a) X = ABEDFEESTYH Y=ABEDFEESTYHABCDF (b) X = BFAAAABBBBBJPRSTY Y=ABCDEFGHIJKLMNOPRS (c) X = ϕ (Empty Sequence), Y = BABADCAB 回答1: Here is an online calculator http://igm.univ-mlv.fr/~lecroq/seqcomp/node4.html Java public class LCS { public static void main(String[] args) { String x = StdIn.readString(); String y =

std::copy from a range into itself

喜欢而已 提交于 2020-01-02 17:18:05
问题 This might be a silly question and the answer may simply be "Because," but I'm curious so here it goes. Suppose I want to copy the final elements of an std::vector to the front without regard to what happens elsewhere. I can do this with a simple function call #include <vector> #include <algorithm> std::vector<int> v; . . . std::copy(v.begin() + N, v.end(), v.begin()); What I find surprising is that the standard seems to brand this undefined behavior if N == 0 (unless v.empty() , I suppose).