0

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;
    }
}
4
  • 2
    Please post CBook. Commented Jun 5, 2020 at 4:25
  • I just added CBook. Commented Jun 5, 2020 at 4:44
  • No, add the CBook class declaration, i.e. CBook.h. No one knows what to swap if we don't see the members. Commented Jun 5, 2020 at 4:46
  • I just added. Are they okay? Commented Jun 5, 2020 at 4:55

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.