Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
map sort
#include <boost/utility/string_ref.hpp> #include <boost/uuid/random_generator.hpp> #include <boost/uuid/nil_generator.hpp> #include <boost/operators.hpp> #include <boost/unordered_map.hpp> #include <boost/functional/hash.hpp> #include <boost/uuid/uuid_io.hpp> #include <boost/io/ios_state.hpp> #include <boost/ref.hpp> #include <boost/lexical_cast.hpp> #include <vector> #include <algorithm> #include <iomanip> namespace boost { template<> struct hash<boost::string_ref> { std::size_t operator()(boost::string_ref const& sr) const { return boost::hash_range(sr.begin(), sr.end()); } }; } int main(int argc, char** argv) { typedef boost::uuids::uuid value_type; typedef boost::string_ref string_type; typedef boost::uuids::random_generator generator_type; typedef boost::unordered_map< string_type, value_type > map_type; // create map const string_type k1 = {"key.z1"}; const string_type k2 = {"key.a2"}; const string_type k3 = {"key.3c"}; map_type map_; map_.emplace(k1, generator_type()()); map_.emplace(k2, generator_type()()); map_.emplace(k3, generator_type()()); // dump sorted entries typedef boost::reference_wrapper<map_type::value_type> reference_type; std::vector<reference_type> sorted_ids; sorted_ids.reserve(map_.size()); std::size_t key_width = 0; std::for_each( map_.begin(), map_.end(), [&](map_type::value_type& data) { key_width = std::max(data.first.length(), key_width); //LOG_DBG << "insert " << data.first; sorted_ids.push_back(reference_type(data)); } ); std::sort(sorted_ids.begin(), sorted_ids.end(), [](reference_type const& lhs, reference_type const& rhs) { return lhs.get().first.compare(rhs.get().first); } ); // print map contents std::size_t no = 1; const std::size_t no_end = sorted_ids.size(); for (auto const& data: sorted_ids) { std::cout << "| " << no << "| " << data.get().first << std::setw(key_width + 3 - data.get().first.length()) << "| " << data.get().second // id has always constant width << " |"; if(no++ != no_end) { std::cout << std::endl; } } return 0; }
run
|
edit
|
history
|
help
0
QuadRootPoint
c++ mouse
Find the row with max ones
Backtracking_simple
Perfect Square Solution
Sortowanie przez scalanie
Bad code
numberOftweets
ADVENTURE CODE CSCI40
aaa666