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?
ksm092
-4
Light Poster
Recommended Answers
Jump to Post#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")); }
Jump to PostYes, it's very easy.
ofstream ofs("other_data.txt"); copy(vec.begin(), vec.end(), ostream_iterator<int>(ofs, " "));
Jump to PostYes, 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 …
All 11 Replies
jinhao
14
Newbie Poster
ksm092
-4
Light Poster
jinhao
14
Newbie Poster
ksm092
-4
Light Poster
jinhao
14
Newbie Poster
ksm092
-4
Light Poster
ksm092
-4
Light Poster
jinhao
14
Newbie Poster
ksm092
-4
Light Poster
ksm092
-4
Light Poster
jinhao
14
Newbie Poster
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.