帮助排序指向c ++中字符串数组的指针数组

时间:2013-10-01 20:22:59

标签: c++ arrays string sorting pointers

我一直试图找到一种方法来对指针数组进行排序(指向字符串),然后显示未排序的列表和排序列表,但是没有我尝试的第二个打印列表总是与原始列表相同未排序的列表。非常感谢您提供的任何帮助(如果我的代码很乱,我很抱歉,我是新生)

这是我的主要(lab5.cpp)

#include <cstdlib>
#include <iostream>
#include "student.h"
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    student stu;
    stu.list();
    system("PAUSE");
    return EXIT_SUCCESS;
}

这是我的标题(student.h)

#include <string>
class student
{
public:
    student( );
    void setnameage();
    int getage(int);
    std::string getname(int);
    void sort();
    void list();

 private:
     std::string name[50];
     std::string nameL[50];
     int age[50];
     std::string * Pname ; 
     int * Page; 
     int amount;   
 };

这是我的对象(student.cpp)

#include <iostream>
#include <iomanip>
#include "student.h"
#include <string>

using namespace std;
//constructor
student::student()
{
    int i = 0;
    amount = 0;
    Pname = name;
    Page = age;
    while (i != 50)
    {
        age[i] = 0;
        name[i] = "A";
        i = i +1 ;
    }
    std::cout << "Enter number of students(max 50) \n" << ">";
    std::cin >> amount;
}

//sets the neame and the age
void student::setnameage()
{
    int i = 0;
    while (i != amount)
    {
        std::cout << "Enter name " << i+1 <<" (last, first):";
        std::cin >> name[i] >> nameL[i];
        std::cout << "enter age";
        std::cin >> age[i];
        i++;
    }
}

//get age
int student::getage(int i)
{
    return age[i];
}

//get name   
std::string student::getname(int i)
{
    return name[i];
}

//sorts the aray of pointers
void student::sort()
{
    std::string tempL;
    int tempN;
    i = 0
    for (int i = 1; i <= amount-1; i++)
    {
        for(int j=i+1; j <= amount; j++)
        {
            if(Pname[i].compare(Pname[j]) > 0)
            {
                tempN = Page[i];
                Page[i] = Page[j];
                Page[j] = tempN;
                // tempL = Pname[i];
                Pname[i].swap(Pname[j]);
                //Pname[j] = tempL;
            }
        }
    }
}

//displayes the final results         
void student::list()
{
    setnameage();
    int i = 0;
    std::cout << "original list\n-------------";
    while(i != amount)
    {
        std::cout<< "\n" << getname(i) << ">" << getage(i);
        i++;
    }
    sort();
    i = 0;
    std::cout << "\nAlphabetized list\n-------------";
    while(i != amount)
    {
        std::cout<< "\n" << Pname[i] << ">" << Page[i];
        i++;
    }
}

1 个答案:

答案 0 :(得分:0)

首先让我说你的程序有很多设计问题,但要回答你的实际问题:

问题是你没有50个指针的数组,你只有一个指向数组开头的指针。在sort函数中,您可以使用此行来交换字符串指针:

Pname[i].swap(Pname[j]);

但是这不会交换指针,它会交换原始字符串。因此,不是以原始字符串数组结束,而是指向这些字符串的重新排序数组,而是最终得到一个重新排序的字符串数组。

您应该将std::string* pName;更改为std::string* pName[50];。在程序开始时,初始化数组以指向字符串。

for (int i = 0; i < 50; i++) pName[i] = &name[i];

然后在sort函数中,你应该使用std::swap()交换指针本身:

std::swap(pName[i], pName[j]);

最后,由于pName[i]现在是一个指针,每当你真正想要访问该字符串时,你必须取消引用指针。例如,

if(Pname[i].compare(Pname[j]) > 0)

变为

if(Pname[i]->compare(*Pname[j]) > 0)

您的年龄分类方法存在同样的问题。

为您的计划设计一个更好的设计是使用std::list<std::pair<std::string, int>>来存储名称和年龄。然后,您可以使用内置的排序功能对列表进行排序(如果您还需要保留原始列表,也可以轻松复制它)。

相关问题