如何使用指针对数组进行排序/复制
How do I sort/copy an array using pointers?
我正试图仅使用<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);
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 二叉排序树无法编译
- 使用strcpy将char数组的元素复制到另一个数组
- 是否可以初始化不可复制类型的成员变量(或基类)
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 使用标准库根据一个数组对两个数组进行排序(避免复制步骤)
- std::对 C 样式字符串进行排序,而不复制字符串
- 初学者C 排序的链接列表使用=操作员进行深层复制
- 计数Heapsort和插入排序中的复制和比较数
- 特征在不复制的情况下对行重新排序
- 合并排序 - 返回新数组,而不是将合并的数组复制到输入数组
- 复制数组和气泡排序 C++
- 复制C++中未排序列表的构造函数
- 矢量排序/唯一/擦除与复制到无序集的性能
- 如何使用指针对数组进行排序/复制
- 如何在不使用复制函数的情况下对vector中存储有日/月/年的实例进行排序
- 复制链表,然后对其排序
- c++并行std::vector排序与昂贵的复制