实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数出错“应输入 2 个参数，却提供了 3 个)

``````#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<memory>
bool compare_int(int  &lhs, int  & rhs)
{
return  lhs > rhs;
}
bool compare_first_letter(const std::unique_ptr<std::string > &lhs, const std::unique_ptr<std::string > & rhs)
{
return  ((*lhs) < (*rhs));
}

bool compare_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;
//}

int main()
{
//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 (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<unique_ptr<string>>
std::vector<std::unique_ptr<std::string>> unique_ptr_string_vector;
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 (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.begin(), 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<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 (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;
}

//输出：
5 4 3 6 9
9 6 5 4 3
adr abc abr aar ebr dbr cbr bbr
aar abc abr adr bbr cbr dbr ebr
5 4 3 6 9
3 4 5 6 9``````

sort函数出错“应输入 2 个参数，却提供了 3 个。

``````bool compare_unique_ptr_int(  std::unique_ptr<int > &lhs,   std::unique_ptr<int > & rhs)
{
return  *lhs < *rhs;
}``````

``````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);``````

``sort(v.begin(), v.end(), [](std::unique_ptr<int > &lhs,   std::unique_ptr<int > & rhs)(){return  *lhs < *rhs;});``

实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)

stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double darray[10]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9}; vector<double> vdoubl

实战c++中的vector系列--将迭代器转换为索引

stl的迭代器非常方便 用于各种算法. 可是一想到vector.我们总是把他当做数组,总喜欢使用下标索引,而不是迭代器. 这里有个问题就是怎样把迭代器转换为索引: #include <vector> typedef std::vector<char *> MYARRAY; // This does the trick inline const int iterator_to_index(MYARRAY &a, MYARRAY::iterator it) { return i