Sign in to follow this  

linked list - ordening strings (is it possible?)

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello i wonder if it is possible to read a file with records like "name = value" and put these records in alphabetically ordered using a linked list i have already achieved it with numbers... but strings i tried everything i could with no success... For example, i want to take these three records from a file size = 20 witdh = 30 height = 10 and compare each other to put it alphabetically ordered... i don't have any idea on how to compare strings to achieve this... my list will hold the hole line on each node (eg. "size = 20") i tried even a way to compare only the first character Then, after all have been ordered, i have to put it in another file... can someone help me?

Share this post


Link to post
Share on other sites
#include <algorithm>
#include <fstream>
#include <iterator>
#include <list>
#include <string>

int main()
{
std::ifstream reader("infile.txt");
std::list<std::string> records;
std::string record;
while (std::getline(reader, record))
{
records.push_back(record);
}
records.sort();
std::ofstream writer("outfile.txt");
std::copy(records.begin(), records.end(), std::ostream_iterator<std::string>(writer, "\n"));
}




Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Silent Angel
sorry, forgot to say.. i'm using C++ and the list i'm using one i got from http://www.gametutorials.com/ a long time ago...


You really should use the C++ standard container classes, you know.


#include <list>
#include <string>

using namespace std;

list<string> mylist;

mylist.push_back("size = 20");
mylist.push_back("witdh = 30");
mylist.push_back("height = 10");

mylist.sort();



Though, since you say you want to read from and write to a file:


#include <list>
#include <string>
#include <fstream>

using namespace std;

int main()
{
list<string> mylist;
ifstream input("foo.txt");
string line;

while(!input.eof())
{
getline(input, line);
mylist.push_back(line);
}

mylist.sort();

ofstream output("bar.txt");
list<string>::iterator itor;
for(itor = mylist.begin(); itor != mylist.end(); ++itor)
ouput << *itor << endl;
}



Though of course, there are more compact versions using STL algorithms.
Furthermore, at some point or other, you might find beneficial to actually parse the file into an associative array:


#include <map>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
map<string,int> mymap;
ifstream input("foo.txt");

string line;
while(!input.eof())
{
getline(input, line);
istringstream iss(line);

string key;
int value;


iss >> key;
iss >> ws; // eat whitespace
iss.ignore(); // the '=' sign
iss >> value;

mymap[key] = value;
}

// can do lookups by key as necessary.
int size = mymap["size"]; // will create a new entry if 'size' not in map.
// use mymap.find() to avoid that.

// and the keys are in order:

ofstream output("bar.txt");
map<string,int>::iterator itor;
for(itor = mymap.begin(); itor != mymap.end(); ++itor)
output << itor->first << " = " << itor->second << endl;
}


Again, proper code would be cleaner and incorporate some decent amount of error-checking. Check your favourite C++ library reference manual.

[disclaimer: I've typed this in a browser without testing the code.]

Share this post


Link to post
Share on other sites

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this