Loading lang_cpp_tr_03...
std::array<int, 10> ai={1, 3, 4, 7, 3, 9, 4, 1, 8, 5}; std::vector<double> vd={0.1, 1.3, 2.4, 3.7, 4.3, 5.9, 6.4, 7.1, 8.8, 9.5};
<algorithm>.
<cmath>donnera cette partie fractionnaire.
[&](const auto &lhs, const auto &rhs) { return ...property...of...lhs... < ...property...of...rhs...; }
//----------------------------------------------------------------------------
#ifndef MUSIC_SONG_HPP #define MUSIC_SONG_HPP
#include <string>
namespace music {
struct Song { std::string artist; std::string title; int year; bool play; };
inline std::string to_string(const Song &s) { return (s.play ? "♫ « " : "• « ")+ s.title+" » by "+ s.artist+" in "+ std::to_string(s.year); }
} // namespace music
#endif // MUSIC_SONG_HPP
//----------------------------------------------------------------------------
std::vector<music::Song> songs= {{"Led Zeppelin", "Good Times Bad Times", 1969, false}, {"The Doors", "Riders on the Storm", 1971, false}, {"Led Zeppelin", "Immigrant Song", 1970, false}, {"Georges Moustaki", "Le métèque", 1969, false}, {"Georges Brassens", "Mourir pour des idées", 1972, false}, {"The Doors", "Peace Frog", 1970, false}, {"Georges Moustaki", "La philosophie", 1975, false}, {"Georges Brassens", "L'ancêtre", 1969, false}};
return std::to_string(the_parameter);
using std::to_string; // fallback if no ADL-specific overload return to_string(the_parameter); // try ADL first, then fallback
std::vector<int> v={1, 3, 4, 7, 3, 9, 4, 1, 8, 5};
//----------------------------------------------------------------------------
#ifndef GENERIC_SEQ_UTILS_HPP #define GENERIC_SEQ_UTILS_HPP
#include <string> #include <algorithm>
namespace generic {
template<typename Iter, typename ToStringFnct> inline std::string to_string(Iter begin_it, Iter end_it, ToStringFnct to_string_fnct) { std::string result; std::for_each(begin_it, end_it, [&](const auto &elem) { if(!empty(result)) { result+=", "; } result+=to_string_fnct(elem); }); return '{'+result+'}'; }
template<typename Iter> inline std::string to_string(Iter begin_it, Iter end_it) { return to_string(begin_it, end_it, [&](const auto &elem) { using std::to_string; // fallback if no ADL-specific overload return to_string(elem); // try ADL first, then fallback }); }
template<typename Sequence> inline std::string to_string(const Sequence &sequence) { return to_string(cbegin(sequence), cend(sequence)); }
template<typename Sequence, typename ToStringFnct> inline std::string to_string(const Sequence &sequence, ToStringFnct to_string_fnct) { return to_string(cbegin(sequence), cend(sequence), to_string_fnct); }
template<typename Iter, typename VisitFnct, typename CompareFnct> inline void for_each_unique(Iter begin_it, Iter end_it, VisitFnct visit_fnct, CompareFnct compare_fnct) { std::for_each(begin_it, end_it, [&](auto &elem) { const auto pos=std::find_if(begin_it, end_it, [&](const auto &other) { return compare_fnct(elem, other); }); if(pos!=end_it && &*pos==&elem) { visit_fnct(elem); } }); }
template<typename Iter, typename VisitFnct> inline void for_each_unique(Iter begin_it, Iter end_it, VisitFnct visit_fnct) { for_each_unique(begin_it, end_it, visit_fnct, [&](const auto &lhs, const auto &rhs) { return lhs==rhs; }); }
template<typename Sequence, typename VisitFnct, typename CompareFnct> inline void for_each_unique(const Sequence &sequence, VisitFnct visit_fnct, CompareFnct compare_fnct) { for_each_unique(cbegin(sequence), cend(sequence), visit_fnct, compare_fnct); }
template<typename Sequence, typename VisitFnct> inline void for_each_unique(const Sequence &sequence, VisitFnct visit_fnct) { for_each_unique(cbegin(sequence), cend(sequence), visit_fnct); }
template<typename Sequence, typename VisitFnct, typename CompareFnct> inline void for_each_unique(Sequence &sequence, VisitFnct visit_fnct, CompareFnct compare_fnct) { for_each_unique(begin(sequence), end(sequence), visit_fnct, compare_fnct); }
template<typename Sequence, typename VisitFnct> inline void for_each_unique(Sequence &sequence, VisitFnct visit_fnct) { for_each_unique(begin(sequence), end(sequence), visit_fnct); }
} // namespace generic
#endif // GENERIC_SEQ_UTILS_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "seq_utils.hpp" #include "song.hpp"
#include <iostream> #include <array> #include <vector> #include <cmath> #include <algorithm>
void test_sort() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::array<int, 10> ai={1, 3, 4, 7, 3, 9, 4, 1, 8, 5}; std::cout << "initial ai: " << generic::to_string(ai) << '\n'; std::sort(begin(ai), end(ai)); std::cout << "sorted ai: " << generic::to_string(ai) << '\n'; for(auto mod=2; mod<=5; ++mod) { std::sort(begin(ai), end(ai), [&](const auto &lhs, const auto &rhs) { return lhs%mod < rhs%mod; }); std::cout << "sorted ai (modulo " << mod << "): " << generic::to_string(ai) << '\n'; } std::vector<double> vd={0.1, 1.3, 2.4, 3.7, 4.3, 5.9, 6.4, 7.1, 8.8, 9.5}; std::cout << "initial vd: " << generic::to_string(vd) << '\n'; std::sort(begin(vd), end(vd)); std::cout << "sorted vd: " << generic::to_string(vd) << '\n'; std::sort(begin(vd), end(vd), [&](const auto &lhs, const auto &rhs) { return std::fmod(lhs, 1.0) < std::fmod(rhs, 1.0); }); std::cout << "sorted vd (fractional): " << generic::to_string(vd) << '\n'; std::vector<music::Song> songs= {{"Led Zeppelin", "Good Times Bad Times", 1969, false}, {"The Doors", "Riders on the Storm", 1971, false}, {"Led Zeppelin", "Immigrant Song", 1970, false}, {"Georges Moustaki", "Le métèque", 1969, false}, {"Georges Brassens", "Mourir pour des idées", 1972, false}, {"The Doors", "Peace Frog", 1970, false}, {"Georges Moustaki", "La philosophie", 1975, false}, {"Georges Brassens", "L'ancêtre", 1969, false}}; std::cout << "initial songs: " << generic::to_string(songs) << '\n'; std::sort(begin(songs), end(songs), [&](const auto &lhs, const auto &rhs) { return lhs.year<rhs.year; }); std::cout << "sorted songs (year): " << generic::to_string(songs, [&](const auto &s) { return "\n "+to_string(s); }) << '\n'; }
void test_for_each_unique() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::vector<int> v={1, 3, 4, 7, 3, 9, 4, 1, 8, 5}; std::cout << "unique v:"; generic::for_each_unique(v, [&](const auto &elem) { std::cout << ' ' << elem; }); std::cout << '\n'; for(auto mod=2; mod<=5; ++mod) { std::cout << "unique v (modulo " << mod << "):"; generic::for_each_unique(v, [&](const auto &elem) { std::cout << ' ' << elem; }, [&](const auto &lhs, const auto &rhs) { return lhs%mod == rhs%mod; }); std::cout << '\n'; } std::vector<music::Song> songs= {{"Led Zeppelin", "Good Times Bad Times", 1969, false}, {"The Doors", "Riders on the Storm", 1971, false}, {"Led Zeppelin", "Immigrant Song", 1970, false}, {"Georges Moustaki", "Le métèque", 1969, false}, {"Georges Brassens", "Mourir pour des idées", 1972, false}, {"The Doors", "Peace Frog", 1970, false}, {"Georges Moustaki", "La philosophie", 1975, false}, {"Georges Brassens", "L'ancêtre", 1969, false}}; std::cout << "initial songs: " << generic::to_string(songs, [&](const auto &s) { return "\n "+to_string(s); }) << '\n'; std::cout << "unique artist:\n"; generic::for_each_unique(songs, [&](const auto &song) { std::cout << " " << to_string(song) << '\n'; }, [&](const auto &lhs, const auto &rhs) { return lhs.artist==rhs.artist; }); std::cout << "unique artist (reversed):\n"; generic::for_each_unique(crbegin(songs), crend(songs), [&](const auto &song) { std::cout << " " << to_string(song) << '\n'; }, [&](const auto &lhs, const auto &rhs) { return lhs.artist==rhs.artist; }); generic::for_each_unique(songs, [&](auto &song) { song.play=true; }, [&](const auto &lhs, const auto &rhs) { return lhs.year==rhs.year; }); std::cout << "unique year: " << generic::to_string(songs, [&](const auto &s) { return "\n "+to_string(s); }) << '\n'; }
int main() { test_sort(); test_for_each_unique(); return 0; }
//----------------------------------------------------------------------------