This tutorial shows how to order a vector of strings in alphabetical order.
Lets say you have a list of strings that you have stored in a vector. You need to order the strings but you do not have a function to do it with. Well ordering strings is actually easier than you might think. It works alot like ordering numbers in an array. I have modified a bubble sort function to
show you how it works.
First we include the vector and string header files, and use the std namespace so we can access vectors and strings.
For those not familier with vectors, they work like arrays except they are treadted as objects. Unlike arrays they can be returned from functions and are also expandable. You add to them by using their push_back member function and you declare them by writing vector<data type>. I have also included the psuedo code for the bubble sort algorithm for refrence (Taken from Starting out With C++ 3rd Eddition).Code:#include <vector> #include <string> using namespace std;
Now that we have that done, we can start working on our sorting function. First we have to declare the function prototype and then the definition.Code:Do Set count variable to 0 For count is set to each subscript in Array from 0 to the next-to-last subscript If array[count] is greater than array[count+1] swap them set swap flag to true end if End for While any elements have been swapped.
As you can see, the function accepts a vector of strings as it's only argument. It then returns a sorted vector of strings. Now following the psuedo code for the bubble sort, we can make a sorting function. We start by initializing all the variables we need for the function. In this case, we needCode:vector<string> sortAlpha(vector<string>); //Main Program Here vector<string> sortAlpha(vector<string> sortThis) {
a swap flag and a temporary storage place.
Next we open our do-while loop and set our swap flag to 0. After this we open up our for loop.Code:int swap; string temp;
Now we can set up our if statment, our swap procedure, and close our remaining brackets.Code:swap = 0; for (int count = 0; count < sortThis.size() - 1; count++) {
Now if you are like me, you may be thinking "You can't compare two strings with the less than operator!", but actually you can. It compares them letter by letter and works just like if you compared two integers. It isCode:if (sortThis.at(count) < sortThis.at(count + 1) { temp = sortThis.at(count); sortThis.at(count) = sortThis.at(count + 1); sortThis.at(count + 1) = temp; swap = 1; } } }while (swap != 0);
perfect for use in this sorting algorithm. Now that the main part of the program is taken care of, all we need to do is return our sorted vector and close our last brace.
And thats it. If you want to test the program, just write this in your main function:Code:return sortThis; }
And don't forget to include <iostream>. Well thats my first tutorial. I doubt it will help many people as I bet I'm the only one who didn't know you can compare strings with the less than/greater than operatorCode:{ vector<string> stringList; vector<string> sortedStringList; string buffer; for(int i = 0; i < 5; i++) { cout << "Enter the " << i << "th string in the list: "; cin >> buffer; stringList.push_back(buffer); } sortedStringList = sortAlpha(stringList); cout << "The sorted string list is: " << endl << endl; for (int i = 0; i < 5; i++) cout << sortedStringList.at(i) << endl; system("pause"); }.
However I thought it was interesting and that I should share it. This source code was written and compiled using Dev-C++ compiler so it may need to be edited to work with other compilers. I have attatched a complete source code listing for convenience. I also have a simple challange to anyone who needs to burn about a minute or so. Try making the program sort words in reverse alphabetical order. If anyone has any revisions, our would like to suggest a better way of accomplishing the goal, please let me know.
Very nice, +rep.
good tutorial +rep
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Thank youI have my next tutorial planned as an introduction to namespaces. If anyone is interested, I will post it here.
Thank you.
good tutorial
great tutorial, a need it !
Very Nice, indeed.
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
nice one. thanx. n gudluck![]()
There are currently 3 users browsing this thread. (0 members and 3 guests)
Bookmarks