c++ - How to apply a sort function to a string.find( ) to print results alphabetically? -


i have program reads text file struct (members- str author , str title) , gives user option display records in file, search author, or search title. need integrate sort function process, when user searches author or title results listed alphabetically.

the following sort function works showall function, have absolutely no idea how can modify search functions alphabetize results.

sort function code:

void sortbytitle(int counter){     //variable     int a, b, minindex;     string temp;       (a = 0; < counter; a++){         minindex = a;         (b = + 1; b < counter - 1; b++){             if (books[b].title < books[minindex].title){                 minindex = b;             }        }         if(minindex != a) {             temp = books[a].title;             books[a].title = books[minindex].title;             books[minindex].title = temp;             cout << books[a].title << endl;         }     } } 

and current title search function:

int showbooksbytitle(int counter, string booktitle){     int recordcount = 0;     //find user-input string inside booktitle     (int = 0; < counter; a++){ //loop through whole file        if (books[a].title.find(booktitle) != string::npos){              //print matching record             cout << books[a].title << " " << "(" << books[a].author << endl;              //keep track of number of matching records             recordcount++;         }     }     return recordcount; } 

my assignment specifies these function headers, search functions return number of records found, , data read struct (rather vector). have leave aspects are.

how can apply selection sort search function can print records in order?

any appreciated!

here set

smallestindex = index; 

then check if

books[index].title < books[smallestindex].title 

it appears trying implement selection sort. snippet can found here.

furhtermore:

  • i don't see need declaring loc outside for-loop. for(int loc....
  • there abundance of sorting algorithms on wikipedia , can use std::sort.
  • in second line shadow index same variable in for-loop.
  • i don't know how store books, if use std::vector don't have pass counter every time. can use books.size().

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -