Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <variant>
- #include <vector>
- #include <map>
- #include <algorithm>
- #include <iterator>
- const auto usdEurCourse = 1.19;
- const auto usdBtcCourse = 0.000076;
- typedef std::tuple<std::string, double> AnotherTuple;
- typedef std::variant<
- /*USD*/ double,
- /*EUR*/ double,
- /*BTC*/ double,
- /*Another*/ AnotherTuple
- > currency;
- enum
- {
- USD,
- EUR,
- BTC,
- Another
- };
- auto comodityMarketsValues = std::map<std::string, currency>{
- { "Gold", currency{ std::in_place_index<USD>, 5.75 } },
- { "Platinium DAX", currency{ std::in_place_index<EUR>, 5.23 } },
- { "FSTE Oil", currency{ std::in_place_index<Another>, AnotherTuple{ "GBP", 1023.22 } } },
- };
- double toUSD(currency curr)
- {
- switch (curr.index())
- {
- case USD: return std::get<USD>(curr);
- case EUR: return std::get<EUR>(curr) * usdEurCourse;
- case BTC: return std::get<BTC>(curr) * usdBtcCourse;
- default: throw std::runtime_error("Unsupported currency");
- }
- }
- int main()
- {
- std::map<std::string, currency> comodityMarketsValuesInUSD;
- std::transform(
- comodityMarketsValues.begin(),
- comodityMarketsValues.end(),
- std::inserter(comodityMarketsValuesInUSD, comodityMarketsValuesInUSD.begin()),
- [](std::pair<std::string, currency> nameCurrPair) -> std::pair<std::string, currency>
- {
- return std::pair<std::string, currency>(
- nameCurrPair.first,
- currency{ std::in_place_index<USD>, toUSD(nameCurrPair.second) }
- );
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment