I am trying to sort a vector of strings in order (one line consists of various strings e.g. 12 10 2010) and was wondering if I would be able to extract individual values from one line of a vector? The way I am doing this is by reading lines from a datafile into a vector and then trying to sort them.Or if there is any other way that would be better, e.g. stringstream?

#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>

int main(void)
{
	using namespace std;

	ifstream ifs("data.txt");
	
	vector<int> vec;  
	copy(istream_iterator<int>(ifs), istream_iterator<int>(), back_inserter(vec));
	
	sort(vec.begin(), vec.end());
	
	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"));
}

.

Yes, it's very easy.

ofstream ofs("other_data.txt");
copy(vec.begin(), vec.end(), ostream_iterator<int>(ofs, " "));

I'm sorry if I am asking too much, but I have lines in the datafile in the form of (12 August 2011: stuff) so I was going to change the month (e.g. August) into a number and then add the day month and year into one number to sort the lines and then write the sorted lines but in proper form (12 August 2011: stuff) to another file. I just want to know if this is possible and if I would be able to implement these functions to the code you have given me?

Yes, it's still easy.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>

class log
{
public:
	void read(std::istream& is)
	{
		is>>day_;
		_m_read_month(is);
		is>>year_;
		std::getline(is, stuff_);
	}
	
	void write(std::ostream& os) const
	{
		os<<day_<<' ';
		_m_write_month(os);
		os<<' '<<year_;
		os<<stuff_;
	}
	
	bool operator<(const log& rhs) const
	{
		return (year_ < rhs.year_) || (year_ == rhs.year_ && ((month_ < rhs.month_) || (month_ == rhs.month_ && day_ < rhs.day_)));
	}
private:
	void _m_read_month(std::istream& is)
	{
		std::string str;
		is>>str;
		if("July" == str)
			month_ = 7;
		else if("August" == str)
			month_ = 8;	
	}
	
	void _m_write_month(std::ostream & os) const
	{
		std::string str;
		switch(month_)
		{
		case 7:
			str = "July"; break;
		case 8:
			str = "August"; break;	
		}
		os<<str;
	}
private:
	int year_;
	int month_;
	int day_;
	std::string stuff_;
};

std::istream & operator>>(std::istream& is, log& l)
{
	l.read(is);
	return is;	
}


std::ostream & operator<<(std::ostream& os, const log& l)
{
	l.write(os);
	return os;	
}

int main(void)
{
	using namespace std;
	ifstream ifs("data.txt");
	
	
	vector<log> vec;
	copy(istream_iterator<log>(ifs), istream_iterator<log>(), back_inserter(vec));
	
	sort(vec.begin(), vec.end());
	
	copy(vec.begin(), vec.end(), ostream_iterator<log>(cout, "\n"));
	
	ofstream ofs("other_data.txt");
	copy(vec.begin(), vec.end(), ostream_iterator<log>(ofs, "\n"));
}

the format of data in data.txt should be

10 August 2012: stuff bb
12 August 2011: stuff aa

But this way the 'month' does not get saved in the new file?

Ohhh I see thank you so much
I will work more on the code

The month is saved.
_m_read_month() translates the string into number and stores the number.
_m_write_month() translate the number into string and write it into ostream object.
But the code I gaven only translates July and Auguest, you should complete the code for other months.

Yes,
Thank You so much!

What does line 60 - 71 do and how is it working when it is floating in between the class and the main?

Line 60-71 define two operator in global scope. They are same with functions and define a method for inputting/outputting a object of class log by std iostream.

On line 80: istream_iterator reads a log object from ifs by using istream& operator>>(istream& is, log & l), like ifs>>logobj;
On line 84 and 87; ostream_iterator writes a log object to cout/ofs by using ostream& operator<<(ostream& os, const log& l), like ofs<<logobj and cout<<logobj;

The classes of ofs and cout is derived from std::ostream, that is why cout and ofs can output the log object by using operator defined on line 67.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.