cpp

RE (Runtime Error) どこで落ちたのか分かるコンパイルオプション -fsanitize=undefined

環境 C++ Mac 普段の動き コードを書いた後 g++ answer.cpp && oj t としてテストケースもチェックしている REになると、どこで発生したかわからない どうする?printf? gdb? こうすればいい g++ -g -fsanitize=undefined answer.cpp && oj t オプションを付…

emplace_back

cpp

使いたいときは、コンパイルオプションが必要 g++ -std=c++11 answer_abc004_4.cpp

数字をフラグのvectorに変換するヘルパー (例: 3 => [1, 1, 0, 0])

#include<bitset> // char to int int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } const int FLAG_NUM = 10; // 1023 => 00111 11111 vector<int> num_to_flags(int x){ stringstream ss; ss << static_cast<std::bitset<FLAG_NUM> >(x); string s = ss.str(); </std::bitset<flag_num></int></bitset>…

XORと ==比較の優先度

cpp

answer_arc098_b.cpp:30:16: warning: ^ has lower precedence than ==; == will be evaluated first [-Wparentheses] if(a+b == a^b){ コンパイラ賢い

【C++】pairのsecondを基準にソート(abc103_d)

提出 beta.atcoder.jp 抜粋 pairの比較関数を定義すればいい bool compare_by_b(pair<int, int> a, pair<int, int> b) { if(a.second != b.second){ return a.second < b.second; }else{ return a.first < b.first; } } int main(){ ... vector<pair<int, int> > pairs(M); // 入力 FOR(i, 0, M)</pair<int,></int,></int,>…

【C++】vectorの扱い。初期化時にサイズ指定とアクセスにv.at(i)を使おう

cpp

サイズ指定 vector<ll> x, y; x.reserve(N); y.reserve(N); これは、こう書くべき。 vector<ll> x(N); vector<ll> y(N); アクセス vectorを宣言してpush_backをしていない状態で配列演算子で値を入れるというミスをした 以下のように FOR(i, 0, N){ ll v; cin >> v; x[i]</ll></ll></ll>…

【C++】再帰が深くてもSegmentation Faultは起こる

cpp

再現コード #include<stdio.h> #include<iostream> #include<iomanip> #include<string> #include<vector> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #include<string> #include<math.h> #include<numeric> #include<complex> #include<ctype.h> using namespace std; typedef long lon…</ctype.h></complex></numeric></math.h></string></algorithm></set></map></stack></queue></vector></string></iomanip></iostream></stdio.h>

【C++】フラグの総当たり・全検索をbitsetで行う方法

cpp

FOR(i, 0, 10){ bitset<D_MAX> flags(i); p(flags); } bitset<D_MAX> aaa(3); p(aaa[0]); p(aaa[1]); p(aaa[2]); output: 0000000000 0000000001 0000000010 0000000011 0000000100 0000000101 0000000110 0000000111 0000001000 0000001001 1 1 0 forの内側で毎回bitsetを</d_max></d_max>…

【C++】大文字 小文字 判定

cpp

#include <ctype.h> islower(c) isupper(c)</ctype.h>

【C++】2次元ベクトルの代わりに複素数を使う

cpp

問題 beta.atcoder.jp 解答 beta.atcoder.jp 補足 ベクトル的に解きたいのでcomplexを使用 90度の回転行列でベクトルを曲げる(机上計算) 和・差・負が楽だった

【C++】nCr 組み合わせ数の実装 漸化式

cpp

参考 http://www.nct9.ne.jp/m_hiroi/linux/cpp08.html#ans06 順列で書くと桁あふれする nCr, nCr-1の関係式から再帰で求められる 追記:競プロではこれを使うことはほぼなく、逆元を使う long long combination(int n, int r) { if (n == r || r == 0) retu…

【C++】文字列の最初と最後の文字を返す s.front(), s.back()

cpp

beta.atcoder.jp こういうしりとりでしか使わないかもしれないけれど。 #include<valarray> #include<iostream> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define pn(s) cout << (#s) << " " << (s) << endl int main(){ string s = "abcde"; cout << </iostream></valarray>…

【C++】いもす法を覚えた!abc017_3

cpp

問題 beta.atcoder.jp この問題ではナイーブに解くとO(N * M) いもす法を使うとO(N + M) いもす法の覚えかた いもす法で検索して本人の解説を読む 喫茶店の例までで十分 今回の問題の解答を読む サンプル1のテストデータで図を書き、いもす法のシミュレート…

【C++】数値計算用だけどあまり知られていないvalarrayを使ってみた。内積、循環シフトなど

cpp

#include<valarray> #include<iostream> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define pn(s) cout << (#s) << " " << (s) << endl void printValArray(valarray<int> A){ for(int a : A){ cout << a << " "; } cout << endl; } int main(){ valarray<int> A </int></int></iostream></valarray>…

C++ foreach的な書き方で書き換えるときは参照渡しを

cpp

#include<stdio.h> #include<iostream> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) void printArray(int *A, int n){ FOR(i, 0, n){ cout << A[i] << " "; } cout << endl; } int main(){ int A[] = {1,2,3}; printArray(A, 3); for(int v : A){ v = 0;</iostream></stdio.h>…

【C++】string s = "abcde"として、s[5]を読んでいいの!?いいんです

cpp

for(int i = 0; i < s.length(); i++) { if(s[i] == 'c' && s[i+1] == 'h'); ... というコードを見かけた 配列でやったら範囲エラー 文字列は終端 '\0' なのでOK 読むだけならね。書き換えはダメ stackoverflow.com s[-1]も読んでいいんです! beta.atcoder.…

【C++】vectorの和。accumulate

cpp

#include <numeric> #define ALL(v) (v).begin(), (v).end() ... ll childSweetSum = accumulate(ALL(a), 0LL); // long long or int childSweetSum = accumulate(ALL(a), 0); include numericが必要 begin, endは定型なのでALL long longは初期値に 0LL と入れる</numeric>

【C++】warning: sizeof on array function parameter will return size of

cpp

c++でArrayを引数にするPrintArray(int a[])を作り、中でマクロARRAY_LENGTH(A)を使った LENGTH分だけforする予定だったが、長さがちゃんと取れない 引数がaというポインタで、ARRAY_LENGTHではsizeof(a)しているが・・・ これは配列の大きさではなくポイン…

【C++】cin高速化

たまに見かけるこのコード cin.tie(0); ios::sync_with_stdio(false); cin高速化と呼ばれている scanfの方が速いようだが、便利なのでcin使いたい人向け 使って計測してみた M行読み込む問題 https://beta.atcoder.jp/contests/abc113/tasks/abc113_c 302ms …

【C++】Class定義内に比較関数を書いてソート

cpp

Class外に書くより中に書いた方がいいでしょう class Data{ public: int i; int p; int y; int order; static bool cmp_by_i(const Data &a, const Data &b) { return a.i < b.i; } static bool cmp_by_y(const Data &a, const Data &b) { return a.y < b.y;…