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:
Comments
Post a Comment