バイナリファイルを作成する
//wb: 書き込みモードで新規に作成 file = open("hoge.txt", "wb:UTF-8") file = File.open("hoge.txt", "wb:UTF-8") //w+b: 書き込み読み込み両用モードで新規に作成 file = open("hoge.txt", "w+b:UTF-8") file = File.open("hoge.txt", "w+b:UTF-8")
string int
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
std::distance
イテレータはわかるんだけど添字がほしいなってとき。
#include <iostream> #include <vector> #include <algorithm> vector<int> v; vector<int>::iterator it; // VALUEと同じかそれ以上の値のイテレータを求める it = lower_bound(v.begin(),v.end(), VALUE); // その値の添字を求める int pos = distance(v.begin(), it);
あとから気づいたけど、イテレータがわかっているのだからそれだけでよかった。
参考
std::distance - cplusplus.com
vectorの指定したiteratorの要素番号を取得する
std::lower_bound
同じかそれ以上の値が出現する場所のイテレータを返す。
#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 << endl; // 10 num = *(++pos); cout << num << endl; // 11