Previous blogs have written about vectors using the sort algorithm, and vector<unique_ptr<string>>
some of the processing methods that have been written before.
Just write it today vector<unique_ptr<string>>
. Sort by using the sort algorithm.
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <memory>BOOLCompare_int (int&LHS,int& RHS) {returnLHS > RHS;}BOOLCompare_first_letter (Const STD::unique_ptr<STD::string> &LHS,Const STD::unique_ptr<STD::string> & RHS) {return((*LHS) < (*RHS));}BOOLCompare_unique_ptr_int (STD::unique_ptr<int> &LHS,STD::unique_ptr<int> & RHS) {return*LHS < *RHS;}//bool compare_by_uniqptr (const unique_ptr<int>& A,//const unique_ptr<int>& B) {//Return *a < *b;//}intMain () {//sort vector<int> STD:: vector<int>Int_vector; Int_vector.push_back (5); Int_vector.push_back (4); Int_vector.push_back (3); Int_vector.push_back (6); Int_vector.push_back (9); for(inti =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(inti =0; I < int_vector.size (); i++) {STD::cout<< (Int_vector[i]) <<" "; }STD::cout<<STD:: Endl;//Sort vector<unique_ptr<string>> STD:: Vector<std::unique_ptr<std::string>> unique_ptr_string_vector;STD::unique_ptr<STD::string> unique_ptr_string (New STD::string("ADR")); Unique_ptr_string_vector.push_back (STD:: Move (unique_ptr_string));STD::unique_ptr<STD::string> Unique_ptr_string8 (New STD::string("ABC")); Unique_ptr_string_vector.push_back (STD:: Move (UNIQUE_PTR_STRING8));STD::unique_ptr<STD::string> Unique_ptr_string7 (New STD::string("ABR")); Unique_ptr_string_vector.push_back (STD:: Move (Unique_ptr_string7));STD::unique_ptr<STD::string> Unique_ptr_string6 (New STD::string("AAR")); Unique_ptr_string_vector.push_back (STD:: Move (Unique_ptr_string6));STD::unique_ptr<STD::string> Unique_ptr_string2 (New STD::string("EBR")); Unique_ptr_string_vector.push_back (STD:: Move (Unique_ptr_string2));STD::unique_ptr<STD::string> Unique_ptr_string3 (New STD::string("DBR")); Unique_ptr_string_vector.push_back (STD:: Move (Unique_ptr_string3));STD::unique_ptr<STD::string> Unique_ptr_string4 (New STD::string("CBR")); Unique_ptr_string_vector.push_back (STD:: Move (Unique_ptr_string4));STD::unique_ptr<STD::string> Unique_ptr_string5 (New STD::string("BBR")); Unique_ptr_string_vector.push_back (STD:: Move (UNIQUE_PTR_STRING5)); for(inti =0; I < unique_ptr_string_vector.size (); i++) {STD::cout<< * (Unique_ptr_string_vector[i]) <<" "; }STD::cout<<STD:: Endl; Sort (Unique_ptr_string_vector.begin (), Unique_ptr_string_vector.end (), compare_first_letter); for(inti =0; I < unique_ptr_string_vector.size (); i++) {STD::cout<< * (Unique_ptr_string_vector[i]) <<" "; }STD::cout<<STD:: Endl;//sort vector<unique_ptr<int>> STD:: Vector<std::unique_ptr<int>> v;STD::unique_ptr<int> Unique_ptr_int (New int(5)); V.push_back (STD:: Move (Unique_ptr_int));STD::unique_ptr<int> Unique_ptr_int1 (New int(4)); V.push_back (STD:: Move (Unique_ptr_int1));STD::unique_ptr<int> Unique_ptr_int2 (New int(3)); V.push_back (STD:: Move (Unique_ptr_int2));STD::unique_ptr<int> Unique_ptr_int3 (New int(6)); V.push_back (STD:: Move (UNIQUE_PTR_INT3));STD::unique_ptr<int> Unique_ptr_int4 (New int(9)); V.push_back (STD:: Move (UNIQUE_PTR_INT4)); for(inti =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(Autoi = 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
It's quite simple, but there are a few issues to be aware of in order to use the sort algorithm in a class.
You will be able to meet such a mistake:
The sort function has an error "you should enter 2 parameters, but provide 3."
In the class you wrote the comparison function:
bool compare_unique_ptr_int( std::unique_ptr<int > &lhs, std::unique_ptr<int > & rhs){ return *lhs < *rhs;}
Then, in one of the member functions of the class, the sort method is used, and the third parameter uses the Compare_unique_ptr_int function, and the error is described above.
But how to improve it?
There are two ways of doing this:
Method One: Change the Compare_unique_ptr_int function to a static method:
static bool compare_unique_ptr_int( std::unique_ptr<int > &lhs, std::unique_ptr<int > & rhs){ return *lhs < *rhs;}sort(v.begin(), v.end(), compare_unique_ptr_int);
Method Two: Use a lambda expression to
sort(v.begin(), v.end(), [](std::unique_ptr<int > &lhs, std::unique_ptr<int > & rhs)(){return *lhs < *rhs;});
That's it!!!
Actual combat Vector Series in C + +-sort the vector<unique_ptr<string>> using the sort algorithm (the Sort function error "should enter 2 parameters, but provides 3)