按字母顺序排序对象数组c ++

时间:2018-10-20 17:24:51

标签: c++ arrays string sorting object

我已经注册了一个面向对象的编程课程,并且使用code :: blocks作为我的IDE。我们有一个项目,要求将对象数组(带有私有名称和标题的Libray图书数组)传递到将按字母顺序对标题或作者进行排序的sort数组。除排序功能外,我所有的组件均应正常工作。我使用了以前在其他程序中使用过的排序功能,但是在使其与该程序一起使用时遇到了麻烦。我仍处于学习如何编码的起步阶段,希望能收到反馈。

头文件:

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

class Book
{
private:
    string title;
    string author;
public:
    Book();
    string getTitle();
    string getAuthor();
    void setTitle(string Title1);
    void setAuthor(string Author1);
};

主要:

#include <iostream>
#include <cstring>
#include <iomanip>
#include "Library.h"

using namespace std;

//Sort function prototype
void s_sort (Book lib_books[], int n);

int main()
{
// Initializes n as the number of books
int n = 15;

//Declaration of array of objects
Book lib_books[15];

// Declartion of the private member "title" for each object
lib_books[0].setTitle("The Alchemist");
lib_books[1].setTitle("The Princess Bride");
lib_books[2].setTitle("The Catcher in the Rye");
lib_books[3].setTitle("1984");
lib_books[4].setTitle("Fahrenheit 451");
lib_books[5].setTitle("The Great Gatsby");
lib_books[6].setTitle("Poland");
lib_books[7].setTitle("The Cantebury Tales");
lib_books[8].setTitle("Leaves of Grass");
lib_books[9].setTitle("Where the Sidewalk Ends");
lib_books[10].setTitle("The Iliad");
lib_books[11].setTitle("Things to Think On");
lib_books[12].setTitle("The Grapes of Wraith");
lib_books[13].setTitle("Hop on Pop");
lib_books[14].setTitle("The Prince and the Pauper");

// Declartion of the private member "author" for each object
lib_books[0].setAuthor ("Paulo Coalho");
lib_books[1].setAuthor ("William Goldman");
lib_books[2].setAuthor ("J.D. Salinger");
lib_books[3].setAuthor ("George Orwell");
lib_books[4].setAuthor ("Ray Bradbury");
lib_books[5].setAuthor ("F. Scott Fitzgerald");
lib_books[6].setAuthor ("James A. Mitchener");
lib_books[7].setAuthor ("Chaucer");
lib_books[8].setAuthor ("Walt Whitman");
lib_books[9].setAuthor ("Shel Silverstein");
lib_books[10].setAuthor("Homer");
lib_books[11].setAuthor("Krishnamurti");
lib_books[12].setAuthor("John Steinbeck");
lib_books[13].setAuthor("Dr. Seuss");
lib_books[14].setAuthor("Mark Twain");

我遇到的主要问题:

// Function to sort the books alphabetically by author
s_sort (lib_books,n);
cout << "\n";                      
// For loop to print all 15 books with author and title
for(int i = 0; i < 14; i++)
{
    cout <<"  "<<setw(25)<<lib_books[i].getTitle()<< "     " <<lib_books[i].getTitle()<< "\n" ;
}
return 0;
}

//Sort function definition
void s_sort (Book::getTitle(), int n)
{
int m;
string hold;// Initializes 'hold' as a 'string' so all the characters will be used when it is sorted

for (int k=0; k<=n-1; k++)// 'For' loop sorts through the index
{
m = k;
    for (int j=k+1; j <= n-1; j++)
    {

    if (lib_books[j].getTitle() < lib_books[m].getTitle());
    //m = j;
    }

// Swaps the lib_book[].getTitle() around and uses 'hold' as a placement
hold = lib_books[m].getTitle();
lib_books[m].getTitle() = lib_books[k].getTitle();
lib_books[k].getTitle() = hold;

hold = lib_books[m].getAuthor();
lib_books[m].getAuthor() = lib_books[k].getAuthor();
lib_books[k].getAuthor() = hold;
}
}

实施文件:

#include <iostream>
#include <cstring>
#include "Library.h"

using namespace std;

Book::Book()
{
    title = " ";
    author = " ";
} 
string Book::getTitle()
{
    return title;
}
string Book::getAuthor()
{
    return author;
}
void Book::setTitle(string Title1)
{
    title= Title1;
}
void Book::setAuthor(string Author1)
{
    author= Author1;
}

我遇到的问题是它无法对数组进行排序。它打印出已定义的相同列表。我试图通过引用传递数组,但是在函数原型和定义中需要一个索引号。

0 个答案:

没有答案
相关问题