如何使用指针对数组进行排序/复制

How do I sort/copy an array using pointers?

本文关键字:排序 复制 数组 何使用 指针      更新时间:2023-10-16

我正试图仅使用<algorithm>排序和复制数组,但无法弄清楚如何完成。这是代码,我一直试图使工作,但没有成功。我错过了什么?

sorted_sc_array(const sorted_sc_array& A);          
template< sorted_sc_array& A, sorted_sc_array& T>   
auto Copy(sorted_sc_array& A, sorted_sc_array& T)   
    ->signed char*(T.begin())   
{  
  auto it1=std::begin(A);  
  auto it2=std::begin(T);
  while(it1 != std::end(A)){
    *it2++ = *it1++;
  }
  return it2;
}

对数组进行排序:

const unsigned int CAPACITY = 1024;
int array[CAPACITY];
//...
std::sort(&array[0], &array[CAPACITY]);

复制数组:

int destination[CAPACITY];
//...
std::copy(&array[0], &array[CAPACITY], &destination[0]);

地址可以代替<algorithm>中大多数函数中的指针或迭代器。

带指针

int * p_begin = &array[0];
int * p_end   = &array[CAPACITY];
//...
std::sort(p_begin, p_end);
//...
int * p_dest_begin = &destination[0];
std::copy(p_begin, p_end, p_dest_begin);