C++ 用STL算法排序结构的动态数组
我在对结构的动态分配数组进行排序时遇到问题,请帮助我找出问题所在 这是我的密码C++ 用STL算法排序结构的动态数组,c++,algorithm,sorting,C++,Algorithm,Sorting,我在对结构的动态分配数组进行排序时遇到问题,请帮助我找出问题所在 这是我的密码 #include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct Box{ int *dimval; int dim; ~Box(){ delete[] dimval; } friend bool operator
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Box{
int *dimval;
int dim;
~Box(){
delete[] dimval;
}
friend bool operator < (const Box& a, const Box& b){
for(int i=0; i<a.dim;i++){
if(a.dimval[i]>b.dimval[i])
return false;
}
return true;
}
void msort(){
sort(dimval, dimval+dim);
}
} *boxes;
int main(){
int num_box, dim;
scanf("%d%d", &num_box, &dim);
boxes = new Box[num_box];
for(int i=0;i<num_box;i++){
boxes[i].dim = dim;
boxes[i].dimval = new int[dim];
}
for(int i=0;i<num_box; i++){
for(int j=0;j<dim; j++){
scanf("%d", &(boxes[i].dimval[j]));
}
boxes[i].msort();
}
for(int i=0;i<num_box; i++){
for(int j=0;j<dim; j++){
cout<<boxes[i].dimval[j]<<" ";
}
cout<<endl;
}
cout<<"-----"<<endl;
sort(boxes, boxes+num_box);
for(int i=0;i<num_box; i++){
for(int j=0;j<dim; j++){
cout<<boxes[i].dimval[j]<<" ";
}
cout<<endl;
}
cout<<"xxxxxxx"<<endl;
}
上述代码段的输出为
1 2 5 10 20 30
3 7 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
1 2 3 4 5 6
9 18 21 37 47 80
-----
1 2 3 4 5 6
1 2 5 10 20 30
201056 200608 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
201728 200784 19 32 41 44
9 18 21 37 47 80
xxxxxxx
正如您所看到的,在Box结构中进行排序是很好的,但是在Box结构中进行排序是很麻烦的,有人能帮您解释一下这个问题以及应该如何解决它吗。谢谢
----------------------更新---------------------------------------
我知道使用指向盒子的指针可能是个魔鬼,但我不明白为什么,请用理由和例子来启发我。
此外,下面是一个示例解决方案的代码片段。在这里,唯一的区别是它使用了struct数组而不是指向动态分配数组的指针,并且得到了正确的输出
这是密码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Node{
int A[12];
int k;
void Sort(){
sort(A,A+k);
}
friend bool operator < (const Node&a, const Node&b){
for(int i=0; i<a.k; ++i){
if(a.A[i]>b.A[i])return false;
}
return true;
}
}arr[32];
很明显,为什么订购不正确。您的
操作员请避免使用指针(新建/删除)为什么?我想要一个动态数组,所以必须使用指针“<代码> STD::vector < /Cord>”是C++中动态数组的编写方法。示例:int main(){Box it;}///crash今天有人知道如何调试吗?这是一门失传的艺术吗?现在调试是非法的吗,一些法律?不,在我的上下文中,<不是这样定义的。这里我想说的是组件方面的小型。i、 例如,一个盒子里的每个元素都比另一个盒子里的元素小Box@Daniel,好的,我想这是合法的,只要结果总是一致的。@Daniel没有提供严格的弱排序。非法的比较运算符也可能导致问题,因为它很容易导致排序
越界,从一个复制到另一个。(但是违反三的规则才是杀手。这都是因为他没有使用std::vector
)@MarkRansom我试着删除析构函数,结果成功了!非常感谢,我不知道这个规则,结果它与动态数组无关。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Node{
int A[12];
int k;
void Sort(){
sort(A,A+k);
}
friend bool operator < (const Node&a, const Node&b){
for(int i=0; i<a.k; ++i){
if(a.A[i]>b.A[i])return false;
}
return true;
}
}arr[32];
int main(){
scanf("%d%d",&n,&k);
for(int i=0; i<n; ++i){
for(int j=0; j<k; ++j)
scanf("%d",&arr[i].A[j]);
arr[i].k=k;
arr[i].Sort();
}
for(int i=0;i<n; i++){
for(int j=0;j<k; j++){
cout<<arr[i].A[j]<<" ";
}
cout<<endl;
}
cout<<"-----"<<endl;
sort(arr,arr+n);
for(int i=0;i<n; i++){
for(int j=0;j<k; j++){
cout<<arr[i].A[j]<<" ";
}
cout<<endl;
}
cout<<"xxxxxx"<<endl;
}
1 2 5 10 20 30
3 7 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
1 2 3 4 5 6
9 18 21 37 47 80
-----
1 2 3 4 5 6
1 2 5 10 20 30
3 7 9 11 15 23
4 14 24 34 40 50
9 10 11 12 13 14
4 8 17 18 27 31
13 19 19 32 41 44
9 18 21 37 47 80
xxxxxx
struct Box{
int *dimval;
int no;
int dim;
Box(){};
Box(const Box& another){
no = another.no;
dim = another.dim;
dimval = new int[no];
memcpy(dimval, another.dimval, dim*sizeof(int));
}
Box& operator = (const Box& another){
if(this != &another){
no = another.no;
dim = another.dim;
int* tmp = new int[no];
delete [] dimval;
dimval = tmp;
memcpy(dimval, another.dimval, dim*sizeof(int));
}
return *this;
}
~Box(){
delete[] dimval;
}
friend bool operator < (const Box& a, const Box& b){
for(int i=0; i<a.dim;i++){
if(a.dimval[i]>=b.dimval[i])
return false;
}
return true;
}
void msort(){
sort(dimval, dimval+dim);
}
} *boxes;