C++ routine to place all short into a set -
it should possible, since number of short int
s less max_size()
of std::set<short int>
:
#include <iostream> #include <set> #include <limits.h> #include <math.h> int main() { std::set<short int> myset; std::cout << "max size of myset " << myset.max_size() << std::endl; std::cout << "number of short ints " << pow(2.0, double(sizeof(short int) * char_bit)); return 0; }
outputs
max size of myset 4294967295 number of short ints 65536
now, i'd place short int
s set, meaning need routine iterate on short int
s. if possible, general routine iterating on elements of given type. have insight?
just fun:
template <typename t> void fill_all_values( std::set<t> &s ) { t t = t(); s.insert( t++ ); while ( t != t() ); }
one more fun solution:
template <typename t> void fill_all_values_2( std::set<t> &s ) { typedef std::set<t>::size_type size_type; size_type t_max = (size_type( 1 ) << (char_bit*sizeof(t))) - 1; ( size_type = 0; < t_max; ++i ) s.insert( *reinterpret_cast<t*>(&i) ); s.insert( *reinterpret_cast<t*>(&t_max) ); }
Comments
Post a Comment