C++

【C++】NMAKEによるバッチビルド

C++

NMAKE コマンドを使ってビルドしたいとき、 毎回 Visual Studioのコマンドプロンプト を起動するのが面倒ですよね(-_-)ワンバッチでできましたのでメモです gist.github.comCMakeと組み合わせて使うとめちゃくちゃ便利なのよ(*'▽')参考: stackoverflow.com

【C++】Enum::ParseとEnum::ToString

C++

C#には Enum 文字列の変換ができます 羨ましいですね~C++でも似てるものが欲しい // こんな感じ #include <iostream> enum class Foo { X,Y,Z }; int main() { Foo foo = Enum::Parse<Foo>("Z"); std::string s = Enum::ToString(Foo::Z); } 書いてみた // enum.h #pragma </foo></iostream>…

【C++】マジックインクリメント

C++

PHPには文字列のインクリメントがあるらしく、C++で実装してみた マジックインクリメントとは (以下はネットで調べて理解したことです。間違ったら指摘していただければ幸いです)英数字の文字列をインクリメントする ++"a" = "b" ++"0" = "1" 桁上げも可能…

【C++】UTF8

C++

UTF8文字列処理のメモです。いまだにUTF8を使ってサーバーとやり取りしてます。 たまに、表示文字数をカウントしたり、文字数が多すぎる場合途中から"..."を表示したり 結構困ります。。。C++11からstd::u32stringを追加されて意外と便利です。 std::codecvt…

【C++】2進数文字列変換

C++

再帰でやってみた #include <string> // std::string #include <functional> // std::function std::string binary(int dec) { std::function<std::string(int)> base2 = [&](int val) { if(val == 0) return std::string{}; return base2(val >> 1) + std::to_string(val%2); }; if(dec == 0) retu</std::string(int)></functional></string>…

cmath にある remquo という 数学関数

C++

浮動小数点剰余を計算する関数 fmod は使ってるが、 剰余と商の両方もほしい場合、何を使うべきかを探してみた。fmodと同じヘッダにある remquo はよさそうにみえて以下コードを書いてみた #include <cmath> #include <iostream> int main() { int quo; auto rem = remquo(59,</iostream></cmath>…

【C++】長いfor文とおさらばだ

C++

C++11の範囲for文を使ったらSTLのコンテナを簡単に回せる std::vector<int> v; // C++03 for(int i = 0 ; i < v.size() ; i++) { v[i]; } // algorithm std::for_each(v.begin(),v.end(),func); // C++11 for(auto it : v) { it; } 見やすくて書きやすいだけど、</int>…

【C++】 クラスの呼び方

C++

クラスの継承関係ではいろんな呼び方があります 1. class 子クラス : 親クラス {}; 2. class 派生クラス : 基底クラス {}; 3. class サブクラス : スーパークラス {}; D&E本では2番目を使用されます 自分も 派生クラス/基底クラス に 統一しようと思い…

【C#】名前付き引数を覚えた

void func(int width,int height) { // do something } void bar() { func(height:10,width:20); } 1.引数の順番を気にしなくかける 2.呼び出すコードを見ただけで引数の意味が分かる void bar() { func(/*width*/20,/*height*/10); } C++では名前付き引数を…

【C++】std::bitset のoperator[]

C++

使ってみて少し迷いました #include <bitset> #include <vector> #include <iostream> int main() { std::bitset<4> bit(0b1000); std::vector<int> v = {1,0,0,0}; if(v[0] == bit[0]) std::cout << "true" << std::endl; else std::cout << "false" << std::endl; } false 違和感があった</int></iostream></vector></bitset>…

std::fill のメモ書き

C++

std::fill でポインタ配列初期化時 #include <vector> int main(void) { Foo* foo[10]; std::fill(foo,foo+10,NULL); } error C2440: '=' : 'const int' から 'xxx' に変換できません。 とエラーが表示されます。NULLの定義は 0 だったので std::fill(foo,foo+10,(Fo</vector>…

Clang - (draft) C++14

C++

Clang is (draft) C++14 feature-complete!言語拡張と標準ライブラリ両方を含め Clang は (draft) C++14 の対応を完了しました

std::rotate を調べた

C++

コンテナの要素を回転するアルゴリズムです #include <iostream> #include <vector> #include <algorithm> // std::rotate int main(void) { std::vector<int> v{1,2,3,4,5,6,7}; std::cout << "before: "; for(auto i:v) std::cout << i << " "; std::cout << std::endl; std::rotate(v.begin(</int></algorithm></vector></iostream>…

std::unique_copy で余分なスペースを削除

C++

余分のスペースを削除します*1 #include <iostream> #include <string> #include <algorithm> int main(void) { std::string s = "This is a unique_copy sample!!"; std::cout << "before:" << s << std::endl; auto result = std::unique_copy(s.begin(),s.end(),s.begin(), [](char c1,</algorithm></string></iostream>…

std::tuple と std::tie と std::ignore を調べた

C++

複数の型の値を保存するクラス : std::tuple #include <tuple> #include <string> #include <iostream> int main(void) { std::tuple<int,float,std::string> t(1,2.5,"Hello"); std::cout << "0:" << std::get<0>(t) << std::endl; std::cout << "1:" << std::get<1>(t) << std::endl; std::cout << "2:" << </int,float,std::string></iostream></string></tuple>…

std::transform を調べた

C++

std::transform は 範囲内の要素に関数を適用してくれる機能です4引数バージョン // 小文字 -> 大文字 #include <string> #include <algorithm> #include <iostream> int main() { std::string s("hello"); std::transform(s.begin(), s.end(),s.begin(),toupper); std::cout << s; } HEL</iostream></algorithm></string>…

std::mem_fn を調べた

C++

std::mem_fn はメンバー関数への呼び出し用ラッパーを作成してくれる機能です使い方: #include <functional> class Foo { public: void set(int val){val_=val;} private: int val_; }; int main(void) { Foo foo; auto setter = std::mem_fn(&Foo::set); setter(foo,20)</functional>…

std::ref と std::cref を調べた

C++

std::ref は 変数への参照tを保持するreference_wrapperオブジェクトを生成する std::cref は const版です #include <functional> #include <iostream> void f(int& n1, int& n2, const int& n3) { std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << std::endl; ++</iostream></functional>…

std::generateを覚えた

C++

引数をとらない関数オブジェクトの結果を要素に書き込む #include <vector> #include <iostream> #include <algorithm> int main() { std::vector<int> foo(10); int i = 0; std::generate(foo.begin(),foo.end(),[&]() -> int { return i++; }); std::for_each(foo.begin(),foo.end(),[](int v</int></algorithm></iostream></vector>…

User-defined literalsを調べてみた

C++

C++11以前 // 単位があいまい height = 3.4; // メートルか?センチメートルか? // 異なる単位の計算(3.4cm * 2.1mmの場合) ratio = (3.4 * 10) / 2.1; より明確定義できるように C++11以降、リテラルにカスタムサフィックスを追加する機能を提供されていま…

for_each_with_indexを調べてみた

C++

std::for_each に ループカウンターを付ける方法を調べて 何種類の実装方法があったが、自分にとって難しかったので、 自分なりにやってみました #include <iostream> #include <string> #include <vector> // ループカウンター付きfor_each template<class InputIterator, class Function> Function for_each_with_index(Inp</class></vector></string></iostream>…

std:bindにメンバー関数を扱う

C++

メモメモ #include <iostream> #include <functional> class Foo { public: Foo():m_val(0){} ~Foo(){} void setVal(int val){m_val=val;} int getVal(void){return m_val;} private: int m_val; }; void main() { Foo foo; auto func = std::bind(&Foo::setVal,&foo,10); // setVa</functional></iostream>…