2011-04-30から1日間の記事一覧

ビット演算、2進法での取り扱い, bool

C++では、2進法を取り扱うのに文法は特にない。1000100なら、2みたいに個数をカウントする。 while(n) { n = n & n-1; //n = 10010 &10001 //0になるときは、たとえば、n=10000のとき。 n = 10000 & 01111; } 結構定番みたい。 n = n >> 1; //10010->01001…

日付関数

閏年か判定 bool isleap(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } 01:01:01とかになってたら、変換 sscanf(time, "%d:%d:%d", &hh, &mm, &ss); //string str = "01:01:01"とかなら、 for(int i = 0; i iss(str); iss >…

STL #include vector のまとめ。

元々の定義はこんな感じ. vector(); vector( const vector& c ); explicit vector( size_type num, const T& val = T() ); vector( input_iterator start, input_iterator end ); vector test(sz); //sz:要素数 vector test(sz, 0) //五個の要素を0で初期化…

STL #include stringのまとめ。

以外と曖昧なコンストラクタの実験から. string ( ); string ( const string& str ); string ( const string& str, size_t pos, size_t n = npos ); string ( const char * s, size_t n ); //Content is initialized to a copy of the string formed by the…

オペレータ

class LessInt { public: bool operator()(const int& riLeft, const int& riRight) const { return riLeft ()); //と、あたかも、コンストラクタを呼び出しているがごとく見える. sort(a.begin(), a.end(), cmp) // このとき、クラスは入れ子にできるよ. …

発想の転換

計算機は小数点とか扱うの苦手なので、あまり÷(/)を使わない. できるだけ×をつかうと、int型のみで済む.doubleに変換しなくても良くなる. とくに、座標計算とか、角度もとめるとかはもろにそうで、 int x, y; ならば、 arg = atan2(y, x) とかとやるより…