ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I've been working my way through my program and i've got a list of strings (list<string> stringList) and it contains a few hundred words. Now I want to put them in alphabetical order. Is the best way to do this just a generic bubble sort? Just comparing the first one to the next, if it's higher swap them out and keep going down the list? Any thoughts would be appreciated. Thanks.
You tried using stringList.sort() ? I got from here that you could use that with strings.
As for the bubble sort, it is a very rudimentry type of sort. It might be OK for a short list on a modern CPU, but for long lists and big apps, I'd use more efficient algorithms like quicksort and heapsort. On a small scale, an insertion sort is better than bubble, and you can sort each element as it's being added to the list.
Sorting a list is a very slow task btw, even with quicksort. But for a few hundred entries, who cares. I would use a list for large collections that need to be sorted though.
Doesn't seem like that wants to work on my list<string> stringList; ... Hmm.. This is what I have so far
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include "myList.h"
using namespace std;
int main ()
{
char line[150], *temp;
ifstream inputText;
string words;
int longestWord = 0, totalWords = 0;
int wordIndex = 0, y = 0;
list<string> stringList;
list<string>::iterator itString;
list<listIndex> indexList;
list<listIndex>::iterator itIndex;
listIndex *tempList;
word *tempWord;
inputText.open("blah.txt");
while(!inputText.eof())
{
inputText.getline(line, 150);
temp = strtok(line, " 1234567890!;\"./'()?,-");
while(temp != NULL)
{
words = temp;
//lowercase all words
for(unsigned int i = 0; i < words.size( ); i++ )
words[i] = tolower(words[i]);
//get longest word length to ascertain number of indecies
if(words.size() > longestWord)
longestWord = words.size();
stringList.push_back(words);
temp = strtok(NULL, " 1234567890!;\"./'()?,-");
}
}
//get total number of words in list
for(itString = stringList.begin(); itString != stringList.end(); itString++)
totalWords++;
//This is where I want to Sort the stringList...
sort(stringList.begin(), stringList.end());
and when I compile it with that sort() function in there, this is what I get:
Code:
usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h: In function 'void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]':
main.cpp:52: instantiated from here
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2570: error: no match for 'operator-' in '__last - __first'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h: In function 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]':
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2571: instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]'
main.cpp:52: instantiated from here
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2214: error: no match for 'operator-' in '__last - __first'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2216: error: no match for 'operator+' in '__first + _S_threshold'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2217: error: no match for 'operator+' in '__first + _S_threshold'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]':
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2220: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2571: instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]'
main.cpp:52: instantiated from here
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2130: error: no match for 'operator+' in '__first + 1'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2220: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]'
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2571: instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_List_iterator<std::string>]'
main.cpp:52: instantiated from here
/usr/include/gcc/darwin/4.0/c++/bits/stl_algo.h:2136: error: no match for 'operator+' in '__i + 1'
Theres also a list function:
list::sort(CompFunc op)
For sorting objects for you, you just provide a comparison function although I'm a little rusty on how to use them. I've done similiar stuff in C with the library function qsort() but I haven't written one in C++. Just something to keep in mind for the future, it could prove to be handy.
Well, if you don't mind treading off the path of the STL, I've got a list class that can sort just as fast as anything. I use it for everything myself. It's memory-safe since the allocation is strictly managed internally by STL containers. It's much easier than messing with iterators, which, in my opinion, can introduce hard to trace bugs. Let me know if you like it if you do decide to try it out. Thanks. https://sourceforge.net/projects/clist-ta0kira/
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.