Created
October 16, 2013 14:41
-
-
Save FrancisGX/7008824 to your computer and use it in GitHub Desktop.
Contrasts: C++ & Ruby
from http://devblog.avdi.org/2013/10/16/sorting-lines-in-5-languages/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a C++ program that sorts lines read from the first arg and writes them to the second arg. | |
#include<string> | |
#include<vector> | |
#include<algorithm> | |
#include<fstream> | |
using namespace std; | |
int main(int argc, char **argv) { | |
ifstream ifile(argv[1]); | |
ofstream ofile(argv[2]); | |
string line; | |
vector<string> data; | |
while(getline(ifile, line)) { | |
data.push_back(line); | |
} | |
sort(data.begin(), data.end()); | |
for(const auto &i : data) { | |
ofile << i << std::endl; | |
} | |
return 0; | |
} | |
This is the same program written in Ruby | |
File.write(ARGV.pop, ARGF.each_line.sort.join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow