Need help with C++ sorting file data

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jhaglund2
Posts: 65
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

elpapa wrote:

Code: Select all

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main&#40;)
&#123;
	std&#58;&#58;ifstream data&#40;"data.txt");
	std&#58;&#58;string line;
	std&#58;&#58;vector<int> v;
	
	while &#40;std&#58;&#58;getline&#40;data, line&#41;) &#123;

		v.clear&#40;);

		std&#58;&#58;istringstream parse&#40;line&#41;;
		int i;
		
		while &#40;parse >> i&#41;
			v.push_back&#40;i&#41;;
		
		std&#58;&#58;sort&#40;v.begin&#40;), v.end&#40;));

		for &#40;int o &#58; v&#41;
			std&#58;&#58;cout << o << " ";

		std&#58;&#58;cout << std&#58;&#58;endl;

	&#125;

	std&#58;&#58;cout << "Press Enter to exit";
	std&#58;&#58;cin.get&#40;);

	return 0;
&#125;

Excellent Patrik! This works right out of the box when I add, "-std=c++11" to the compiler options ([Error] range-based-for loops not allow in C98 mode). Thanks you :)
jhaglund2
Posts: 65
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

AlvaroBegue wrote:I wrote this last night, before seeing Patrik's code. Apparently it's a very natural way to do it.

Code: Select all

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

int main&#40;) &#123;
  std&#58;&#58;vector<int> v;
  std&#58;&#58;string line;
  while &#40;std&#58;&#58;getline&#40;std&#58;&#58;cin, line&#41;) &#123;
    std&#58;&#58;istringstream iss&#40;line&#41;;
    v.clear&#40;);
    int i;
    while &#40;iss >> i&#41;
      v.push_back&#40;i&#41;;
    std&#58;&#58;sort&#40;v.begin&#40;), v.end&#40;));
    for &#40;auto x &#58; v&#41;
      std&#58;&#58;cout << x << ' ';
    std&#58;&#58;cout << '\n';
  &#125;
&#125;

EDIT: Note that even most of the variables have the same names!!! The biggest difference between the two programs seems to be that I work in a Unix style --where it's natural to read from standard input and write to standard output-- and he probably works on Windows.
Thanks Alvaro,
I had something close to this but abandoned the whole idea when I encountered a compiler error about range-based for loops. I got your's to work also with a little bit of changes. :)
elpapa
Posts: 211
Joined: Sun Jan 18, 2009 11:27 pm
Location: Sweden
Full name: Patrik Karlsson

Re: Need help with C++ sorting file data

Post by elpapa »

jhaglund2 wrote:Excellent Patrik! This works right out of the box when I add, "-std=c++11" to the compiler options ([Error] range-based-for loops not allow in C98 mode). Thanks you :)
Great, I'm glad I could help!
User avatar
Rebel
Posts: 6994
Joined: Thu Aug 18, 2011 12:04 pm

Re: Need help with C++ sorting file data

Post by Rebel »

jhaglund2 wrote:I'm looking for some help with sorting information taken from a file.

Code: Select all

Example&#58; data.txt

10 3 41 12 1
23 12 34 22 3
32 3 30 2 12

output&#58;

1 3 10 12 41
3 12 22 23 34
2 3 12 30 32
system ("sort file1.txt /o file2.txt");

Will do the job.

For options type help sort from the command line.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Need help with C++ sorting file data

Post by Rein Halbersma »

I like to use a little helper class called "line", so that I can use iterators to read /write from std::cin/std::cout and also to read/write from the intermediate stringstream:

Code: Select all

#include <algorithm>    // copy, sort, transform
#include <iostream>     // cin, cout, istream
#include <iterator>     // istream_iterator, ostream_iterator
#include <sstream>      // stringstream
#include <string>       // getline, string
#include <utility>      // move
#include <vector>       // vector

class line &#123;
    std&#58;&#58;string data;
public&#58;
    friend std&#58;&#58;istream &operator>>&#40;std&#58;&#58;istream &is, line &l&#41; &#123;
        std&#58;&#58;getline&#40;is, l.data&#41;;
        return is;
    &#125;
    /* implicit */ operator std&#58;&#58;string&#40;) const &#123; return data; &#125;    
&#125;;

int main&#40;)
&#123;
    std&#58;&#58;transform&#40;
        std&#58;&#58;istream_iterator<line>&#123;std&#58;&#58;cin&#125;,
        std&#58;&#58;istream_iterator<line>&#123;&#125;,
        std&#58;&#58;ostream_iterator<std&#58;&#58;string>&#123;std&#58;&#58;cout, "\n"&#125;,
        &#91;&#93;&#40;std&#58;&#58;string current_line&#41; &#123;
            std&#58;&#58;stringstream sstr&#123;std&#58;&#58;move&#40;current_line&#41;&#125;;    
            std&#58;&#58;vector<int> v&#40;
                std&#58;&#58;istream_iterator<int>&#123;sstr&#125;,
                std&#58;&#58;istream_iterator<int>&#123;&#125;
            );
            sstr.clear&#40;);
            std&#58;&#58;sort&#40;v.begin&#40;), v.end&#40;));
            std&#58;&#58;copy&#40;
                v.begin&#40;), v.end&#40;), 
                std&#58;&#58;ostream_iterator<int>&#123;sstr, " "&#125;
            );
            return sstr.str&#40;);
        &#125;
    );        
&#125;
So I read in line-by-line, and the C++11 lambda function in the std::transform reads in the integers per line, sorts them and writes them back.
jhaglund2
Posts: 65
Joined: Mon Jan 16, 2017 6:28 pm

Re: Need help with C++ sorting file data

Post by jhaglund2 »

Thank you for the help. I appreciate it!