Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 string sort
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Jan 02, 2006 2:08 pm   Post subject: (No subject)

code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>

using namespace std; //typo should be "namespace"

int main()
{
   ifstream input_file("file_name");
   vector<string> words =
      vector<string>(istream_iterator<string>(input_file),
                     istream_iterator<string>());

   sort(words.begin(), words.end());
   copy(words.begin(),
        words.end(),
        ostream_iterator<string>(cout, "\n"));

   return 0;
}


Now it compiles.
Sponsor
Sponsor
Sponsor
sponsor
Geminias




PostPosted: Tue Jan 03, 2006 12:36 am   Post subject: (No subject)

oh i see, you left out an equal sign too.. thats why i was getting all these errors.

I like the vector class, its cool.

One question though.. what is istream_iterator?
wtd




PostPosted: Tue Jan 03, 2006 12:53 am   Post subject: (No subject)

The entire Standard Template Library very heavily uses iterators. It essentially is a value which represents a position in a collection.

In this case we're dealing with an input stream iterator.

code:
istream_iterator<string>(input_file)


Creates a new iterator pointing to the beginning of the input_file input stream.

code:
istream_iterator<string>()


Represents the end of the stream.
Geminias




PostPosted: Tue Jan 03, 2006 1:07 am   Post subject: (No subject)

thanks.. it has become more clear except how do iterators differ from pointers?

and why do they use the underscore convention?

So "istream_iterator <string>" is an overloaded function and when you pass it no parameter it defaults to pointing to the end of a istream?
wtd




PostPosted: Tue Jan 03, 2006 1:17 am   Post subject: (No subject)

Geminias wrote:
thanks.. it has become more clear except how do iterators differ from pointers?


In a lot of ways they can be used very similarly to pointers.

Geminias wrote:
and why do they use the underscore convention?


Why not? Smile

C++ forces no particular naming conventions, so it's pretty much a matter of taste. Unfortunately, lots of different libraries do things differently, so you can't really justify the use of any one convention as a means of maintaining consistency with the names from libraries.

I guess the programmers at SGI decided they liked underscores. Perhaps they also know that lowercase letters are typically easier to read than capitals, and without camelCasing, you need underscores as separators.

Geminias wrote:
So "istream_iterator <string>" is an overloaded function and when you pass it no parameter it defaults to pointing to the end of a istream?


istream_iterator is a class, actually. When you pass it no argument, you get the equivalent of a NULL pointer. Then when the STL function using the iterator is looping it basically starts with the first iterator can advances it until it equals the second iterator.

At the end of the input stream, it gets that NULL equivalent, which equals the second iterator, and that tells it to stop iterating (looping).
Geminias




PostPosted: Tue Jan 03, 2006 2:48 am   Post subject: (No subject)

the reason i asked about the underscore was because i thought iterator was a member function of istream. i didnt realize istream_iterator was a class. I pretty much understand now. TY Wink
md




PostPosted: Tue Jan 03, 2006 10:21 pm   Post subject: (No subject)

MysticVegeta wrote:
Heap sort seems to beat merge and quicksort..

Erm... wha? Methinks you're mistaken... Quick sort and Merge sort are the two fastest sorts given most sets of data. Heap sort is fast too, and is the same O(n log n), but in practice it ends up being slower then quick sort or merge sort.
DIIST




PostPosted: Tue Jan 03, 2006 10:55 pm   Post subject: (No subject)

Merge, & Quick sort may be fast. But they are hard to understand. Well for me any way. Bubble Sort and the minor sorts seem to make sence. Rolling Eyes
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jan 03, 2006 11:37 pm   Post subject: (No subject)

Geminias wrote:
the reason i asked about the underscore was because i thought iterator was a member function of istream. i didnt realize istream_iterator was a class. I pretty much understand now. TY Wink


http://www.sgi.com/tech/stl/
md




PostPosted: Wed Jan 04, 2006 12:17 am   Post subject: (No subject)

thuvs wrote:
Merge, & Quick sort may be fast. But they are hard to understand. Well for me any way. Bubble Sort and the minor sorts seem to make sence. Rolling Eyes

Bubble sort is a great thing to start off with precisely because it _is_ easy to understand. But once you get the hang of it learning how quick sort and merge sort work is a good idea.
DIIST




PostPosted: Wed Jan 04, 2006 11:29 am   Post subject: (No subject)

Cornflake wrote:

Bubble sort is a great thing to start off with precisely because it _is_ easy to understand. But once you get the hang of it learning how quick sort and merge sort work is a good idea.


I wonder if there is like a tutorial that explains the different sorting algorithms. I personally learned bubble sort and the minor sorts form google sites. Its only shell sort and up, that i dont get. Confused

Shell sort uses 2d arrays though so i guess you have to go 3d, if you are dealing with sorting strings. Question
Geminias




PostPosted: Wed Jan 04, 2006 6:36 pm   Post subject: (No subject)

you wouldn't "have" to go 3d arrays. its absolutely no different except in terms of organization. so if you thought it would be easier you could do a 1d array Laughing
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 27 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: