|
|
|
|
|
by jcelerier
2281 days ago
|
|
It's a bit longer in C++ but frankly not by that much : #include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <nlohmann/json.hpp>
#include <range/v3/action/sort.hpp>
int main()
{
using namespace nlohmann;
using namespace ranges;
const json parsed = json::parse(std::ifstream("/tmp/json/test.json"));
std::vector agenda(parsed.begin(), parsed.end());
sort(agenda, {}, [] (const auto& j) { return j["name"]; });
for(const auto& people : agenda) try {
std::tm t{};
std::istringstream(people["hired"].get<std::string>()) >> std::get_time(&t, "%Y-%m-%d %H:%M:%S");
std::cout << people["name"] << " (" << people["age"] << ") - " << std::put_time(&t, "%d/%m/%y") << ": \n";
for(const auto& email : people["emails"])
std::cout << " - " << email << "\n";
} catch (...) { }
}
|
|
We went from:
> many Python projects have way more lines of code than equivalent C++ projects
To:
> It's a bit longer in C++ but frankly not by that much
With the proof being literally __twice__ more characters, with one line being more than a 100 characters long.
I understand that C++ doesn't have a native JSON lib, and sorting is a bit out there, but giving 3rd party lib access to Python feels like cheating:
I mean, I can also create a lib that does the entire script, install it, and just do 'import answernh; answerhn.print_json("agenda.json")'Now again, this is not so say "Python is better than C++". That's not the point.
If you want to write a game engine, C++ makes sense, verbosity doesn't matter, and you don't want the Python GC to kick in. If you want to iterate on your Saas product API quickly, it's probably not the best choice.
Why pretend otherwise? What the point of stating the sky is red?