简体   繁体   English

对字符串数组进行排序

[英]Sort an array of strings

I need a quick hint on this function. 我需要有关此功能的快速提示。 So basically I have this struct, used by a class. 因此,基本上我有一个由类使用的结构。

#include <cstring>
#include <iostream>
using namespace std;

struct postazione{
    char* nome;
    bool occupato;
};

class Aula{
    int qntpst;
    postazione * vett;
    bool full(const Aula&);
public:
    Aula(int);
    bool aggiungi(const char*);
    friend ostream& operator<<(ostream&, const Aula&);
    Aula& elimina(int);
    Aula(const Aula&); 
    Aula& operator!();
    ~Aula();
};

That is, an array of which each element is a string and a bool, but the the last one is not important now. 也就是说,每个元素都是字符串和布尔值的数组,但是最后一个元素现在并不重要。

The ! operator has to sort the array in alphabetical order. 操作员必须按字母顺序对数组进行排序。

Here's how I tried to do it. 这是我尝试执行的操作。

Aula& Aula::operator!(){
    int qnt=0;
    for(int i=0;i<qntpst;i++)
            if(vett[i].occupato)
                    qnt++;
    if(qnt!=qntpst)
            return *this;
    char *temp;
    for(int i=0;i<qntpst-1;i++){
            for(int j=i+1;j<qntpst;j++){
                    if(strcmp(vett[i].nome,vett[j].nome)>0){
                            temp=new char[strlen(vett[i].nome)+1];
                            strcpy(vett[i].nome,temp);
                            delete [] vett[i].nome;
                            vett[i].nome=new char[strlen(vett[j].nome)+1];
                            strcpy(vett[i].nome,vett[j].nome);
                            delete [] vett[j].nome;
                            vett[j].nome=new char[strlen(temp)+1];
                            strcpy(vett[j].nome,temp);
                            delete temp;
                    }
            }
    }
    return *this;
}

First 7 lines check if each bool of each element of the array is true, otherwise it won't execute. 前7行检查数组每个元素的布尔值是否为true,否则将不执行。 Then it starts to sort. 然后开始排序。 That's how I tried to do it but it doesn't work. 那就是我试图做到的方式,但是没有用。

PS The solution has to use an auxilary pointer, something like: aux=i; i=j; j=aux; PS该解决方案必须使用aux=i; i=j; j=aux;指针,例如: aux=i; i=j; j=aux; aux=i; i=j; j=aux;

Quick hints. 快速提示。

  1. Don't use operator!() to sort. 不要使用operator!()进行排序。 Use a function named something like sort() . 使用一个名为sort()类的函数。 operator!() generally does a very different thing, and using it to sort will make your code harder to understand. operator!()通常会做非常不同的事情,使用它进行排序会使您的代码更难以理解。
  2. Don't use using namespace std in a header file (or before class definitions that rely on it). 不要在头文件中(或在依赖它的类定义之前) using namespace std There are plenty of explanations as to why on the internet. 关于为什么要上网的解释很多。
  3. Use standard C++ library capabilities rather than rolling your own as you have. 使用标准的C ++库功能,而不要像自己那样滚动。

For example, the following omits constructors and the like, but does 90% of what you seek. 例如,以下代码省略了构造函数等,但是却完成了您所寻求的90%。 With no worrying about memory management, getting the algorithm to sort right, etc etc. 无需担心内存管理,正确排序算法等。

#include <string>
#include <vector>
#include <algorithm>

struct postazione
{
    std::string nome;
    bool occupato;
    bool operator<(const postazione & other) const
      {
           return nome < other.nome;   // std::string supports this
      };
};


class Aula
{
       std::vector<postazione> data;
   public:
      // constructors, destructors, etc

       void sort()    // sort the current vector
       {
            std::sort(data.begin(), data.end());  // this sorts using the order defined by postazione::operator<()
       }         

       Aula Sorted() const  // return a sorted copy of ourselves
       {
           Aula temp(*this);    // copy ourself
           temp.sort();         // sort the copy
           return temp;         //  return sorted copy
       };
};

The thing to realise is that std::string , std::vector (and other containers in the C++ library) keep track of their size, and resize themselves cleanly when required. 要实现的是, std::stringstd::vector (和C ++库中的其他容器)会跟踪它们的大小,并在需要时调整其大小。

Read up on std::string and std::vector to work out what you need to do to manage them (set contents, update, etc). 阅读有关std::stringstd::vector内容,以了解管理它们(设置内容,更新等)所需要做的事情。

You are not copying anything to your temporary pointer. 您没有将任何内容复制到临时指针。

Change 更改

strcpy(vett[i].nome,temp);

to

strcpy(temp, vett[i].nome);

Also, let me just say the method you're using is extremely inefficient and likely to cause problems. 另外,我只能说您使用的方法效率极低,并且很可能会引起问题。 I would define a comparison function for your class and use std::sort(). 我将为您的类定义一个比较函数,并使用std :: sort()。 And please, do provide more information next time, an input and an output with the expected output, it helps. 并且,请在下次提供更多信息,输入和输出以及预期的输出时,它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM