如何在C 中自定义排序数组,但将其中的一部分放在未分类中
How to custom sort an array in C++, but leave a portion of it unsorted?
我想对数组进行排序,但要省略其中的一部分。被排除在外的零件应由开始索引(n
(和端索引(m
(指定。不得对这两个指数(包括指定的两个指定的指定索引(之间的所有字段进行分类。所有其他人,包括间隔之前的距离,以及之后的时间,都应将其分类。
例如:
- 输入串行
{10 , 4 , 11 , 7 , 6 , 20}
- 非小区间启动索引
n = 1
- 非小区间端索引
m = 3
, -
输出:
{ 6 , 4 , 11 , 7 , 10 , 20 }
index 1到3的字段未对4, 11, 7
进行排序。
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int arr[5] = {10, 4, 11, 7, 6, 20};
sort (arr,arr+5);
for (int i = 0; i < 5; i++){
cout << arr[i] << " ";
}
return 0;
}
我该怎么做?
这是一种使用std::rotate
和std::sort
的方法。
它旋转不应排序的元素,然后对开始部分进行分类,然后旋转我们移动的元素。
#include <iostream>
#include <iterator>
#include <array>
#include <algorithm>
int main() {
std::array<int, 6> arr = {10, 4, 11, 7, 6, 20};
unsigned from = 1;
unsigned to = 3;
unsigned distance = to - from + 1;
if (to + 1 != arr.size())
std::rotate(arr.begin(), arr.begin() + to + 1, arr.end());
std::sort(arr.begin(), arr.end() - distance);
std::rotate(arr.begin() + from, arr.end() - distance, arr.end());
for (auto v : arr)
std::cout << v << ' ';
}
我使用了std::array
而不是C风格数组。也可以在std::vector
上工作。
这是范围库的不错的表演案例,例如boost.range。
我们首先创建一个切片范围,以使零件向左和向右排序固定零件。
auto left = arr | sliced(0, n);
auto right = arr | sliced(m, 6);
然后,我们创建了一个新范围,该范围连接了这两个切片
auto s = join(left, right);
和排序
auto r = sort(s);
与输出一起使用boost.range提供以下代码:
#include<iostream>
#include<algorithm>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/join.hpp>
using namespace boost;
using namespace boost::adaptors;
int main(){
//define original and temp arrays
int arr[6] = {10, 4, 11, 7, 6, 20};
auto n = 2;
auto m = 4;
auto left = arr | sliced(0, n);
auto right = arr | sliced(m, 6);
auto s = join(left, right);
auto r = sort(s);
std::cout << "sorted range: ";
boost::copy(r, std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
std::cout << "array: ";
boost::copy(arr, std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
return 0;
}
运行代码打印
sorted range: 6,7,10,20,
array: 6,4,11,7,10,20,
由于没有涉及的数据的额外移动或复制,也应该非常有效。我也觉得它非常表现力。
您可以取出要排序的元素并将它们存储在临时数组中,然后以所需的形式重组数组。我提供了一个粗制的代码解决方案;但是,您应该考虑使用std :: vector或另一个std容器(如果要更改Orignal数组的长度,也需要使用硬编码,则需要使用sizeof(a)/sizeof(a[0])
之类的内容(。
#include<iostream>
#include<algorithm>
int main(){
//define original and temp arrays
int arr[6] = {10, 4, 11, 7, 6, 20};
int temp[3] = {0};
//cycle through arr, remove the static elements, and store in temp array
int k = 0;
for(int i = 0; i < 6; i++)
{
if(i > 0 && i < 4)
{
continue;
}
temp[k] = arr[i];
k++;
}
//sort the temp array
std::sort(temp, temp + 3);
//restructure the array by placing the static elements back into the original array
k = 0;
for (int i = 0; i < 6; i++)
{
if (i > 0 && i < 4)
{
continue;
}
arr[i] = temp[k];
k++;
}
//print out the sorted array with static elements
for(int i = 0; i < 6; i++)
{
std::cout << arr[i] << " ";
}
return 0;
}
我提供了两个解决方案:
- 对临时副本进行分类,然后将排序的项目放回原始列表
- 创建数据的几个视图,然后对数据的视图之一进行排序
解决方案ONE - 丢失条目1-3的临时数组。对临时性进行排序,然后将临时性放回原始数组中。为了制作此"挽回",我使用一个索引阵列,这是我原始数组中的查找,以从哪里拉出值并将值推回:
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[]) {
int arr[6] = { 10, 4, 11, 7, 6, 20 };
int index[3] = { 0, 4, 5 };
int tmp[3];
for (int i = 0; i < 3; i++)
tmp[i] = arr[index[i]];
sort(tmp, tmp + 3);
for (int i = 0; i < 3; i++)
arr[index[i]] = tmp[i];
for (int i = 0; i < 6; i++)
cout << arr[i] << " ";
cout << "n";
return 0;
}
解决方案两个 - 使用自定义比较器对数组的子集进行分类:
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(int *a, int *b) {
return *a < *b;
}
int main(int argc, char* argv[]) {
int arr[6] = { 10, 4, 11, 7 , 6, 20 };
int* sorted[3] = { &arr[0], &arr[4], &arr[5] };
int* skip[3] = { &arr[1], &arr[2], &arr[3] };
int** arr2[6] = { &sorted[0], &skip[0], &skip[1], &skip[2], &sorted[1], &sorted[2] };
sort(sorted, sorted + 3, compare);
for (int i = 0; i < 6; i++)
cout << **arr2[i] << " ";
return 0;
}
- C++:如何使函数只返回作为列表一部分的字符串
- std::sort()函数无法对向量的一部分进行排序
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 更改 C++ 中的组合分类变量
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 只有级联分类器会发出未定义的引用错误
- 反转整数的一部分(一半)的函数
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- C 指针和对象作为不同类的一部分
- 如何分析代码的哪一部分创建了线程?
- 分类还是划分枚举?
- 如何使用接口指针调用方法,该指针是其具体类的一部分,而不是接口的一部分
- 复制矩阵的一部分并粘贴到另一个矩阵C++上
- 如何删除部分类?
- 我可以将c ++清理器仅应用于程序的一部分而不是第三方库吗?
- C++向量的哪一部分在传递给函数时被复制
- C++结构的一部分
- 在 MPI 中共享数组的一部分
- 如何检测文件夹是否太重,如果是,请删除其中的一部分
- 如何在C 中自定义排序数组,但将其中的一部分放在未分类中