LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2005, 01:23 AM   #1
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
alphabetize my list c++


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.

-aquaman
 
Old 10-21-2005, 02:58 AM   #2
Winno
Member
 
Registered: Sep 2003
Location: Australia
Distribution: Fedora Core 3 / Mandrake 10.1 / Gentoo 2005.0
Posts: 100

Rep: Reputation: 15
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.
 
Old 10-21-2005, 03:24 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Yes you should use C++'s sort algorithm, which uses introsort. Here is the correct way to use it:
Code:
#include <algorithm>

sort( stringList.begin(), stringList.end() );

Last edited by spooon; 10-21-2005 at 03:25 AM.
 
Old 10-21-2005, 10:31 AM   #4
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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.
 
Old 10-21-2005, 01:57 PM   #5
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by spooon
Yes you should use C++'s sort algorithm, which uses introsort. Here is the correct way to use it:
Code:
#include <algorithm>

sort( stringList.begin(), stringList.end() );
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'
any thoughts?

Last edited by AquamaN; 10-21-2005 at 01:59 PM.
 
Old 10-21-2005, 02:12 PM   #6
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Ok, proper code is:

Code:
stringList.sort();
thanks all!
 
Old 10-21-2005, 02:25 PM   #7
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Heres an example you can play with. I recommend a book on the STL, or at least a generic C++ book with an excellent chapter on the subject.

Code:
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
 list<string> li;

 li.push_back("joe");
 li.push_back("bob");
 li.push_back("amy");
 li.push_back("ned");
 li.push_back("steph");

 li.sort();
 for(list<string>::iterator i = li.begin(); i != li.end(); ++i)
 {
  cout << *i << endl;
 }

 return 0;
}
Outputs:
amy
bob
joe
ned
steph

Last edited by lowpro2k3; 10-21-2005 at 02:26 PM.
 
Old 10-21-2005, 02:30 PM   #8
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Quote:
Originally posted by AquamaN
Ok, proper code is:

Code:
stringList.sort();
thanks all!
Ah I was too late

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.
 
Old 10-25-2005, 01:20 AM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 10-26-2005 at 01:25 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can OpenOffice alphabetize? joshknape Linux - Software 10 08-02-2010 07:50 AM
alphabetize reviews Present LQ Suggestions & Feedback 2 01-09-2005 06:50 PM
get list of latest list of packages bobwall Linux - Distributions 1 11-30-2004 03:48 PM
wireless channel list different from router's list heluani Linux - Laptop and Netbook 1 08-29-2004 10:04 PM
Inserting element to a list of list in C suwandy_chandra Programming 2 03-09-2004 03:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration