Need help with C++ sorting file data

Discussion of chess software programming and technical issues.

Moderator: Ras

jhaglund2
Posts: 66
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()
{
	std::ifstream data("data.txt");
	std::string line;
	std::vector<int> v;
	
	while (std::getline(data, line)) {

		v.clear();

		std::istringstream parse(line);
		int i;
		
		while (parse >> i)
			v.push_back(i);
		
		std::sort(v.begin(), v.end());

		for (int o : v)
			std::cout << o << " ";

		std::cout << std::endl;

	}

	std::cout << "Press Enter to exit";
	std::cin.get();

	return 0;
}

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: 66
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() {
  std::vector<int> v;
  std::string line;
  while (std::getline(std::cin, line)) {
    std::istringstream iss(line);
    v.clear();
    int i;
    while (iss >> i)
      v.push_back(i);
    std::sort(v.begin(), v.end());
    for (auto x : v)
      std::cout << x << ' ';
    std::cout << '\n';
  }
}

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: 7306
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

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: data.txt

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

output:

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: 749
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 {
    std::string data;
public:
    friend std::istream &operator>>(std::istream &is, line &l) {
        std::getline(is, l.data);
        return is;
    }
    /* implicit */ operator std::string() const { return data; }    
};

int main()
{
    std::transform(
        std::istream_iterator<line>{std::cin},
        std::istream_iterator<line>{},
        std::ostream_iterator<std::string>{std::cout, "\n"},
        [](std::string current_line) {
            std::stringstream sstr{std::move(current_line)};    
            std::vector<int> v(
                std::istream_iterator<int>{sstr},
                std::istream_iterator<int>{}
            );
            sstr.clear();
            std::sort(v.begin(), v.end());
            std::copy(
                v.begin(), v.end(), 
                std::ostream_iterator<int>{sstr, " "}
            );
            return sstr.str();
        }
    );        
}
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: 66
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!