Created
October 11, 2012 11:37
-
-
Save gurgeh/3871781 to your computer and use it in GitHub Desktop.
Comparison of C++ string writing
This file contains 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
#include <vector> | |
#include <string> | |
#include <map> | |
#include <iostream> | |
using namespace std; | |
/* | |
d = {"primes" : [2, 3, 5, 7], | |
"fib": [1, 1, 2, 3]} | |
print d | |
*/ | |
void print1(){ | |
map<string, vector<int>> msv; | |
vector<int> v; | |
v.push_back(2); | |
v.push_back(3); | |
v.push_back(5); | |
v.push_back(7); | |
msv["primes"] = v; | |
v.clear(); | |
v.push_back(1); | |
v.push_back(1); | |
v.push_back(2); | |
v.push_back(3); | |
msv["fib"] = v; | |
map<string, vector<int>>::iterator endi = msv.end(); | |
for(map<string, vector<int>>::iterator i = msv.begin(); i != endi; i++){ | |
cout << i->first << ": "; | |
vector<int>::iterator endvi = i->second.end(); | |
bool first = true; | |
for(vector<int>::iterator vi = i->second.begin(); vi != endvi; vi++){ | |
if(!first){ | |
cout << ", "; | |
} else { | |
first = false; | |
} | |
cout << *vi; | |
} | |
cout << endl; | |
} | |
} | |
void print2(){ | |
map<string, vector<int>> msv = {{"primes", {2, 3, 5, 7}}, | |
{"fib", {1, 1, 2, 3}}}; | |
for(auto i: msv){ | |
cout << i.first << ": "; | |
bool first = true; | |
for(auto vi: i.second){ | |
if(!first){ | |
cout << ", "; | |
} else { | |
first = false; | |
} | |
cout << vi; | |
} | |
cout << endl; | |
} | |
} | |
template<List> | |
string show_list(const List &l){ | |
return join(l | transform([](x){return (format("%1%") % x).str();}), ", "); | |
} | |
void print3(){ | |
map<string, vector<int>> msv = {{"primes", {2, 3, 5, 7}}, | |
{"fib", {1, 1, 2, 3}}}; | |
join(msv | transform([](x){return (format("%1%: [%2%]") % x.first % show_list(x.second)).str();}), "\n") | |
} | |
int main(void){ | |
print1(); | |
print2(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment