简体   繁体   中英

How to swap elements in an array?

I need to call a swap function in a class. I don't know how to come up with the swap function. Here's how I'm calling swap function:

void    
CLibrary::Sort(CBook* Books, int size)
{
    int         minIdx;    
    string      minVal;    
    Books = new CBook[size];

    for (int i = 0; i < (size - 1); ++i) 
    { 
        minVal = Books[i].GetTitle();
        minIdx = i;        
        for (int k = i + 1; k < size; ++k) 
        { 
            if (Books[k].GetTitle() < minVal)
            { 
                minIdx = k;                
                minVal = Books[k].GetTitle(); 
            } 
        }
        Books[i].Swap(Books[minIdx]);
    }
}

And I need to come up with this function:

void            
CBook::Swap(CBook& Book)
{

}

Any suggestions will be greatly appreciated!

CBook.h is as follows.

class CBook
{    
private:        
    string          mTitle;       
    string          mAuthor;        
    int             mYearPublished;    

public:        
    CBook();
    CBook(string Title, string Author, int YearPublished);        
    ~CBook();
    string          GetTitle() const;        
    string          GetAuthor() const;    
    int             GetYearPublished() const;        
    void            SetBook(string Title, string Author, int YearPublished);        
    void            SetTitle(string Title);        
    void            SetAuthor(string Author);        
    void            SetYearPublished(int YearPublished);        
    void            Swap(CBook &Book);
};

There's also CLibrary class where I'm calling the swap function and where it reads in the input file. Readbooks() function is as follows.

void
CLibrary::ReadBooks()
{
    ifstream    infile;

    string      title;
    string      author;
    int         yearPublished;


    infile.open(mInFileName);

    string s;
    getline(infile, s);
    mNumBooks = stoi(s);

    CBook   Book;
    mBooks = new CBook[mNumBooks];

    while (!infile.eof())
    {
        for (int i = 0; i < mNumBooks; ++i)
        {
            getline(infile, title);
            getline(infile, author);
            getline(infile, s);
            yearPublished = stoi(s);
            mBooks[i].SetBook(title, author, yearPublished);
        }
        infile.close();
        break;
    }
}

If the class CBook has a defined move or a copy assignment then std::swap should fit your needs.

std::swap(Books[i], Books[minIdx]);

Also I am seeing that you are trying to sort an array of CBook . I would recommend using std::sort .

// CBook::GetTitle() must be a const method!
std::sort(Books, Books + size, 
[](const CBook& b1, const CBook& b2) { return b1.GetTitle() < b2.GetTitle(); });

Edit

Looking at your CBook implementation I see you lack the assignment operators necessary.

CBook& operator=(CBook&&) = default;

The above will allow the compiler to automatically generate a move assignment for you.

You can use std::swap on the members of the class:

#include <algorithm>
//...
void CBook::Swap(CBook& Book)
{
    std::swap(Book.mTitle, mTitle);
    std::swap(Book.mAuthor, mAuthor);
    std::swap(Book.mYearPublished, mYearPublished);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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