- 追加された行はこの色です。
- 削除された行はこの色です。
[[プログラミング]][[C]]
#contents
*Tips [#q514910c]
-include blockの代わりに、#pragma onceを先頭に入れるだけで良い。
-[[コーディングパターン>http://home.wakatabe.com/ryo/blog/wp-admin/post.php?post=396&action=edit]]
-[[めっちゃ多様なアルゴリズム>http://chaste.web.fc2.com/Reference.files/Algo.html]]
--スプライン,splineなど
*bit演算 [#d98fb640]
-[[参考>http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=bitManipulation]]
-there exists m in N n = 2^mの判定。
--n & (n-1)
--long long n;
-count trailing zeros
--unsigned int n;
-__builtin_ctz(n) = k * 2^m, k and 2 are coprimeなるm
--__builtin_ctz(n)
--__builtin_ctz(n) = LSB(n)
-count leading zeros
--__builtin_clz(n)
--unsigned int n;
--sizeof(n) – __builtin_clz(n) = argmin x < 2^i
-n bitの補数表現a<-->int b
--a = (b+1<<(n-1))%(1<<(n-1));
--a = (b+1<<(n-1))&( (1<<(n-1))-1);
--b = (a-1<<(n-1))%(1<<(n-1));
*STL [#g28b631c]
**vector [#q3faaa26]
-[[vectorをforでeraseするならiteratorは自己責任>http://home.wakatabe.com/ryo/blog/?p=665]]
**strstream [#b494e9c7]
-ss.str()は終端文字std::endsを自動挿入しない
int code = 1000;
std::strstream ss;
ss << code << std::ends;
*C++11 [#s431e75f]
-[[量化関数>http://home.wakatabe.com/ryo/blog/?p=694]]
*文法 [#y2b7c204]
**pure virtual [#vd2ccddd]
-純粋仮想にはvirtual = 0としなければならない。
-それを引く仮想にも、virtualとしなければならない。
*ゲッタ・セッタの生成プログラム [#b54be07e]
-これに"int test"などを入力すると,ゲッタとセッタを出力してくれる
awk '{printf("%s get%s(void){return %s;}\nvoid set%s(%s %s){%s = %s;}\n", $1, toupper(substr($2, 1, 1)) substr($2, 2), $2, toupper(substr($2, 1, 1)) substr($2, 2), $1, $2, $2 "_", $2);}'
-本当はvim scriptでやりたいのだが…
*newと実体の動作の違い [#s450bd15]
-完全にコピーコンストラクタがおかしい以外考えられない。
--new delete newで正常動作して、実体定義、実体代入で正常動作しない
*親のコンストラクタを呼ぶ [#xdbd6710]
decisionCross::decisionCross(StockDayStream* stock, int s, int m, int a)
: decision(stock), _s(s), _m(m), _a(a)
{
}
*sstreamとstrstream [#w9312a13]
-str()が,sstreamはstd::string型を出力し,strstreamはchar*を出力する
-strstreamはstd::endsを最後に入れる必要があるが,sstreamは入れてはならない.
*coutに表示させる [#rf02bde0]
#include <iostream>
class HoldingStock {
public:
int m_code;
std::ostream& operator()(std::ostream& ros) {
ros << m_code << " " << m_code << " " << m_sl << " " << m_tp;
return ros;
}
};
*isnanがあいまい [#q4869608]
-std::isnanとすれば解決
*fstreamのパターン [#w2b1e53c]
-これが楽
#include <iostream>
#include <fstream>
using namespace;
int main()
{
ifstream fin(filename);
if (!fin) {
return 1;
}
while (fin) {
fin >> t;
fin >> x >> y >> z;
cout << x << " " << y << " " << z << endl;
}
return 0;
}
ofstream ofs("data.txt");
if(ofs) {
ofs << "OK" << endl; // cout に書き出すのと同じ感じで
}