C++ C++;使用回调开关对向量进行冒泡排序
我一直在尝试对取自多行文本文件的int数据向量进行冒泡排序。我已经设法将数据从向量中分离出来,这样我就只有需要排序的元素了,但是我在使用向量运行代码时遇到了一个问题。我重用了我之前在排序数组时编写的代码,但似乎无法让它很好地处理向量 我有一个单独的排序类:C++ C++;使用回调开关对向量进行冒泡排序,c++,vector,bubble-sort,C++,Vector,Bubble Sort,我一直在尝试对取自多行文本文件的int数据向量进行冒泡排序。我已经设法将数据从向量中分离出来,这样我就只有需要排序的元素了,但是我在使用向量运行代码时遇到了一个问题。我重用了我之前在排序数组时编写的代码,但似乎无法让它很好地处理向量 我有一个单独的排序类: #pragma once #include <iostream> #include <vector> using namespace std; class Sorter { public: int chec
#pragma once
#include <iostream>
#include <vector>
using namespace std;
class Sorter {
public:
int checker;
//Callback function for bubble sort
int compare(vector<int> a, vector<int> b) {
//Do decending sort
if (a > b) {
return -1;
}
//Do ascending sort
else {
return 1;
}
}
//Bubble sort - best case complexity omega(n)
void bubbleSort(vector<int> &A, int n, int(*compare)(int, int)) {
int i, j, temp;
for (i = 0; i < n; i++){
for (j = 0; j < n - 1; j++) {
if (compare(A[j], A[j + 1]) > 0) {
temp = A[j];
A[j + 1] = temp;
}
}
}
}
};
Sorter sorter;
sorter.checker = inputVector[0];
vector<int> sortingVector;
cout << inputVector.size() << endl;
for (int i = 3; i <= inputVector[2]; i++) {
sortingVector.push_back(inputVector[i]);
}
sorter.bubbleSort(sortingVector, inputVector[2], sorter.compare());
#pragma一次
#包括
#包括
使用名称空间std;
分类机{
公众:
整数检查器;
//用于冒泡排序的回调函数
整数比较(向量a,向量b){
//下位排序
如果(a>b){
返回-1;
}
//升序排序
否则{
返回1;
}
}
//冒泡排序-最佳情况复杂度ω(n)
void bubbleSort(向量&A,整数n,整数(*比较)(整数,整数)){
int i,j,温度;
对于(i=0;i0){
温度=A[j];
A[j+1]=温度;
}
}
}
}
};
以及我的主源文件中调用它的部分:
#pragma once
#include <iostream>
#include <vector>
using namespace std;
class Sorter {
public:
int checker;
//Callback function for bubble sort
int compare(vector<int> a, vector<int> b) {
//Do decending sort
if (a > b) {
return -1;
}
//Do ascending sort
else {
return 1;
}
}
//Bubble sort - best case complexity omega(n)
void bubbleSort(vector<int> &A, int n, int(*compare)(int, int)) {
int i, j, temp;
for (i = 0; i < n; i++){
for (j = 0; j < n - 1; j++) {
if (compare(A[j], A[j + 1]) > 0) {
temp = A[j];
A[j + 1] = temp;
}
}
}
}
};
Sorter sorter;
sorter.checker = inputVector[0];
vector<int> sortingVector;
cout << inputVector.size() << endl;
for (int i = 3; i <= inputVector[2]; i++) {
sortingVector.push_back(inputVector[i]);
}
sorter.bubbleSort(sortingVector, inputVector[2], sorter.compare());
分拣机;
sorter.checker=输入向量[0];
向量分类向量;
我已经修改了冒泡排序函数以进行优化
int compare(int a, int b) {
//Do decending sort
if (a > b) {
return -1;
}
else {
return 1;
}
}
void bubbleSort(vector<int> &A, int n) {
int temp;
bool exe=false;
for(int i=0;i<n-1;i++){
exe=false;
for(int j=0;j<n-1-i;j++){
if(compare(A[j],A[j+1]) > 0){
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
exe=true;
}
}
if(exe==false)
break;
}
}
int比较(int a,int b){
//下位排序
如果(a>b){
返回-1;
}
否则{
返回1;
}
}
void bubbleSort(向量&A,整数n){
内部温度;
bool exe=false;
对于(int i=0;i而言,编译错误是因为调用了没有参数的比较函数,并且没有将其作为参数传递
您还存在一个问题,即非静态成员功能需要一个分拣机
(此将指向该分拣机)
比较函数的参数必须是向量的元素
我建议不要使用类来保存自由函数
int compare(int a, int b) {
if (a > b) {
return -1;
}
else {
return 1;
}
}
void bubbleSort(vector<int> &A, int(*compare)(int, int)) {
for (int i = 0; i < A.size(); i++){
for (int j = 0; j < A.size() - 1; j++) {
if (compare(A[j], A[j + 1]) > 0) {
std::swap(A[j], A[j + 1]);
}
}
}
}
旁白:如果要使用std::sort
,则不需要从inputVector
复制到sortingVector`
if (ascending) {
std::sort(inputVector.begin() + 3, inputVector.end(), std::less<int>{});
} else {
std::sort(inputVector.begin() + 3, inputVector.end(), std::greater<int>{});
}
if(升序){
std::sort(inputVector.begin()+3,inputVector.end(),std::less{});
}否则{
std::sort(inputVector.begin()+3,inputVector.end(),std::greater{});
}
为什么下面有一个*呢?你是想用“&”来传递作为引用吗?void bubbleSort(vector*a,int n,int(*compare)(int,int))@revelationnow是的,我已经设法解决了:)我现在唯一遇到的问题是sorter.compare()的错误。告诉我:int类型的参数与int类型的参数不兼容(*)(int,int)“不需要向它传递比较函数,你可以直接引用它,不传递n。使用n=A.size()之类的size方法来获取长度。上面的冒泡排序按降序排序,根据你的意愿进行修改。希望它能帮到我:)如果你有其他问题,可以将其注释下来。几乎可以让它工作了,只是得到了这个问题在code if(比较(a[j],a[j+1])>0处,“无效参数被传递给认为无效参数致命的函数”
line@DevParzival这显然是一个学习练习。内联比较不允许您对升序和降序使用相同的函数sorts@Caleth上面只比较了按降序排序时的if。是的,理想情况下我会使用std::sort,不幸的是,我必须根据我所做的应用不同的排序函数我从文件中得到nput。文件的第一行给出了一个1或0,我需要使用它来让程序决定它是否应该按升序/降序排序。然后我必须为最佳情况/最坏情况复杂度创建一个函数。不过,您和开发人员帮了我一把,非常感谢:)您完全可以使用std::sort
并选择ascen正在或正在下降。您当前的实现无法在两者之间进行选择。