Author |
Message |
Jeevan25
|
Posted: Wed Dec 21, 2005 5:09 pm Post subject: string sort |
|
|
i am in need of help. i am a high school student taking c++. i am using dev c++ as my ide. i need to write a program that reades from a file. the file contains 1000 words in random order. one word per line. the program reads the file and sort the words in alphabetic order. then it outputs the sorted words to a new file. for example.
unsorted words.dat code: | pile
game
they
apple |
now the program will read this file and sort the words in alphabetic order and save to a new file.
sorted words.dat code: | apple
game
pile
they | how do i do this? pls help. thanks. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Dec 21, 2005 5:13 pm Post subject: (No subject) |
|
|
Well, first you need to read the lines from the file into something like a:
c++: | std::vector<std::string> |
Now, here's where you ask me what that is. Don't worry, specific questions are how you learn. |
|
|
|
|
|
Jeevan25
|
Posted: Wed Dec 21, 2005 7:42 pm Post subject: (No subject) |
|
|
yeap. i didnt go that far. my teacher told me three ways. select sort, insertion sort and bubble sort. i didn't learn what you typed. my teacher ain't that great either. she tells me to come after school but she is never there. |
|
|
|
|
|
wtd
|
Posted: Wed Dec 21, 2005 7:44 pm Post subject: (No subject) |
|
|
Well, as I said, the first step is to get the lines into some kindof collection, then you can worry about the sort. |
|
|
|
|
|
Martin
|
|
|
|
|
Jeevan25
|
Posted: Wed Dec 21, 2005 10:03 pm Post subject: (No subject) |
|
|
i need to know the loops and the if statements to sort the words. i know how to read and write to files. |
|
|
|
|
|
bugzpodder
|
Posted: Wed Dec 21, 2005 10:37 pm Post subject: (No subject) |
|
|
if you find the examples martin give are not enough, then I suggest you go speak to your teacher. He will be more accessible and provide more relevant help |
|
|
|
|
|
Jeevan25
|
Posted: Wed Dec 21, 2005 10:46 pm Post subject: (No subject) |
|
|
yeah right. i hate her. i ask for help with my code. somthing is wrong. she ask like 3 people who are getting moer than 80 to help me because she couldn' find problem. then she called the best student. he is the one who solved it. not the teacher. if there was a worst teacher of millenium she would be that one. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Dec 21, 2005 10:51 pm Post subject: (No subject) |
|
|
Shame the STL can't be fully utilized.
code: | #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
using namspace std;
int main()
{
vector<string> words;
ifstream input_file("file_name");
copy(istream_iterator<string>(input_file),
istream_iterator<int>(),
back_inserter(words));
sort(words);
copy(words.begin(),
words.end(),
ostream_iterator<string>(cout, "\n"));
return 0;
} |
|
|
|
|
|
|
wtd
|
Posted: Wed Dec 21, 2005 11:22 pm Post subject: (No subject) |
|
|
Hmmm... actually...
code: | #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
using namspace std;
int main()
{
ifstream input_file("file_name");
vector<string> words(istream_iterator<string>(input_file),
istream_iterator<string>());
sort(words);
copy(words.begin(),
words.end(),
ostream_iterator<string>(cout, "\n"));
return 0;
} |
|
|
|
|
|
|
MysticVegeta
|
Posted: Sun Dec 25, 2005 12:59 pm Post subject: (No subject) |
|
|
haha I love this sort commands which are not there in Turing by the ways, But I have a question, Didn't your teacher say about "shell sort"? I think thats the fastest way to sort...
http://linux.wku.edu/~lamonml/algor/sort/sort.html |
|
|
|
|
|
Hikaru79
|
Posted: Sun Dec 25, 2005 2:59 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: Didn't your teacher say about "shell sort"? I think thats the fastest way to sort...
Haha, no, it's the fastest of the O(n^2) algorithms -- that's far, far away from being faster than quicksort or merge sort (for anything other than trivially-sized lists, that is). |
|
|
|
|
|
wtd
|
|
|
|
|
MysticVegeta
|
Posted: Mon Jan 02, 2006 12:06 am Post subject: (No subject) |
|
|
Heap sort seems to beat merge and quicksort.. |
|
|
|
|
|
Geminias
|
Posted: Mon Jan 02, 2006 10:02 am Post subject: (No subject) |
|
|
Quote: #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
using namspace std; //typo should be "namespace"
int main()
{
ifstream input_file("file_name");
vector<string> words(istream_iterator<string>(input_file),
istream_iterator<string>());
sort(words);
copy(words.begin(),
words.end(),
ostream_iterator<string>(cout, "\n"));
return 0;
}
if namespace were spelled right would this code compile for you wtd? |
|
|
|
|
|
|