Vector series in practice c ++ -- sort the vector (unique_ptr (string) using the sort algorithm (sort function error "two parameters should be input, but three parameters are provided)
Previously, I wrote a blog post on sorting the vector using the sort algorithm.vector >
.
Today, let's write a pairvector >
Use the sort algorithm for sorting.
# Include
# Include
# Include
# Include
Bool compare_int (int & lhs, int & rhs) {return lhs> rhs;} bool compare_first_letter (const std: unique_ptr
& Lhs, const std: unique_ptr
& Rhs) {return (* lhs) <(* rhs);} bool compare_unique_ptr_int (std: unique_ptr
& Lhs, std: unique_ptr
& Rhs) {return * lhs <* rhs;} // bool compare_by_uniqptr (const unique_ptr
& A, // const unique_ptr
& B) {// return * a <* B; //} int main () {// sort vector
Std: vector
Int_vector; counts (5); int_vector.push_back (4); int_vector.push_back (3); int_vector.push_back (6); counts (9); for (int I = 0; I <int_vector.size (); I ++) {std: cout <int_vector [I] <"";} std: cout <std: endl; sort (int_vector.begin (), int_vector.end (), compare_int); for (int I = 0; I <int_vector.size (); I ++) {std: cout <(int_vector [I]) <"";} std: cout <std: endl; // sort vector
> Std: vector
> Unique_ptr_string_vector; std: unique_ptr
Unique_ptr_string (new std: string ("adr"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string); std: unique_ptr
Unique_ptr_string8 (new std: string ("abc"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string8); std: unique_ptr
Unique_ptr_string7 (new std: string ("abr"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string7); std: unique_ptr
Unique_ptr_string6 (new std: string ("aar"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string6); std: unique_ptr
Unique_ptr_string2 (new std: string ("ebr"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string2); std: unique_ptr
Unique_ptr_string3 (new std: string ("dbr"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string3); std: unique_ptr
Unique_ptr_string4 (new std: string ("cbr"); unique_ptr_string_vector.push_back (std: move (unique_ptr_string4); std: unique_ptr
Unique_ptr_string5 (new std: string ("bbr"); Evaluate (std: move (unique_ptr_string5); for (int I = 0; I <unique_ptr_string_vector.size (); I ++) {std: cout <* (unique_ptr_string_vector [I]) <";} std: cout <std: endl; sort (), unique_ptr_string_vector.end (), compare_first_letter); for (int I = 0; I <unique_ptr_string_vector.size (); I ++) {std :: cout <* (unique_ptr_string_vector [I]) <";} std: cout <std: endl; // sort vector
> Std: vector
> V; std: unique_ptr
Unique_ptr_int (new int (5); v. push_back (std: move (unique_ptr_int); std: unique_ptr
Unique_ptr_int1 (new int (4); v. push_back (std: move (unique_ptr_int1); std: unique_ptr
Unique_ptr_int2 (new int (3); v. push_back (std: move (unique_ptr_int2); std: unique_ptr
Unique_ptr_int3 (new int (6); v. push_back (std: move (unique_ptr_int3); std: unique_ptr
Unique_ptr_int4 (new int (9); v. push_back (std: move (unique_ptr_int4); for (int I = 0; I <v. size (); I ++) {std: cout <* v [I] <"";} std: cout <std: endl; // sort (std: make_move_iterator (v. begin (), std: make_move_iterator (v. end (), compare_unique_ptr_int); // for (auto I = v. begin (); I! = V. end (); I ++) // {// std: cout <** I <"; //} // std: cout <std:: endl; sort (v. begin (), v. end (), compare_unique_ptr_int); for (auto I = v. begin (); I! = V. end (); I ++) {std: cout <** I <"";} std: cout <std: endl; return 0 ;} // output: 5 4 3 6 99 6 5 4 3adr abc abr aar ebr dbr cbr bbraar abc abr adr bbr cbr dbr ebr5 4 3 6 93 4 5 6 9
In fact, it is quite simple, but you need to pay attention to some issues when using the sort algorithm in the class for sorting.
You may encounter the following errors:
An error occurred in the sort function. "Two parameters should be input, but three parameters are provided.
You have written a comparison function in the class:
bool compare_unique_ptr_int( std::unique_ptr
&lhs, std::unique_ptr
& rhs){ return *lhs < *rhs;}
Then, in a member function of the class, the sort method is used for sorting. The third parameter uses the compare_unique_ptr_int function, and the preceding error occurs.
But how can we improve it?
There are two methods:
Method 1: Change the compare_unique_ptr_int function to a static method:
static bool compare_unique_ptr_int( std::unique_ptr
&lhs, std::unique_ptr
& rhs){ return *lhs < *rhs;}sort(v.begin(), v.end(), compare_unique_ptr_int);
Method 2: Use a lambda expression
sort(v.begin(), v.end(), [](std::unique_ptr
&lhs, std::unique_ptr
& rhs)(){return *lhs < *rhs;});
This is all done !!!