2014-12-29から1日間の記事一覧

string int

c++

intとstringの変換。 C++11だけしか動かない #include <iostream> #include <string> int n = 100; string str_n; str_n = to_string(n); // "100" n = n + stoi(str_n); // 200</string></iostream>

std::distance

c++

イテレータはわかるんだけど添字がほしいなってとき。 #include <iostream> #include <vector> #include <algorithm> vector<int> v; vector<int>::iterator it; // VALUEと同じかそれ以上の値のイテレータを求める it = lower_bound(v.begin(),v.end(), VALUE); // その値の添字を求める int pos = </int></int></algorithm></vector></iostream>…

std::lower_bound

c++

同じかそれ以上の値が出現する場所のイテレータを返す。 #include <iostream> #include <vector> #include <algorithm> vector<int> v; vector<int>::iterator pos; for(int i = 0; i < 100; i++){ v.push_back(i); } pos = lower_bound(v.begin(), v.end(), 10); int num = *pos; cout << num << en</int></int></algorithm></vector></iostream>…

(構造体の中で関数) & (タプルを使って比較演算定義)

c++

struct S{ int w,h,l; S(int w, int h):w(w), h(h){ l = w*w + h*h; } }; bool operator<(const S& a, const S& b) { // l、hの順番で優先順位を付けて比較 return tie(a.l, a.h) < tie(b.l, b.h); } 関数の定義の書き方がいまいちよくわからない。こういう…

VimのsyntasticをC++11で使う

vim QuickRunとsyntasticで clang++のC++11を使う方法ここを見よう