cxx

N-gram Template Library

http://karlmicha.googlepages.com/lg 作ってたのとほとんど同じものがあった。

均等分割

cxx

最初を適当に決めて、残りを再帰で均等に。 頭の悪いアルゴリズムなので、計算量とかスタックとかがいろいろ大変なことになっている。 #include <iostream> #include <list> #include <iterator> #include <locale> #include <cassert> using namespace std; pair<list<wstring>,size_t> divid_len(size_t num, const w</list<wstring></cassert></locale></iterator></list></iostream>…

Casting does not work as expected when optimization is turned on.

http://gcc.gnu.org/bugs.html#nonbugs_c

Reference is not alias

cxx

#include <iostream> class cmplx { public: double x; double y; double& real; double& imaginary; public: cmplx(double _x, double _y) : x(_x), y(_y), real(_x), imaginary(_y) {} }; int main(int argc, char** argv) { cmplx c(0.1, 0.1); std::cout << c.real </iostream>…

コンストラクタのインターフェイスを増やす

cxx

Javaでいうところの public class Foo { public Foo(String s) { ... } public Foo(SomeClass x) { this(x.toString()); } がやりたいのですが、 class Foo { Foo(string s) { ... } Foo(int x) { Foo(x + ""); } } だと→ 共通部分を関数にしてください。 [1…

vector で 2次元配列

cxx

#include <vector> #include <iostream> using namespace std; int main() { vector<vector<double> > table(2, vector<double>(2)); table[0][0] = 0.1; table[0][1] = 0.2; table[1][0] = 0.3; table[1][1] = 0.4; for ( vector<vector<double> >::iterator i = table.begin(), e = table.end(); i != e; ++i ) { fo</vector<double></double></vector<double></iostream></vector>…

バベル案内

http://www.aoky.net/articles/steve_yegge/tour_de_babel.htm Javaに切り替えることは、2人のプログラマになることだ。 1人はあなたがもはや気にかけなくて良くなったことの面倒を見、もう1人が問題領域にフォーカスする。

nesugi.net - swigの使い方のメモ書き

http://www.nesugi.net/hiki/?swig%a4%ce%bb%c8%a4%a4%ca%fd%a4%ce%a5%e1%a5%e2%bd%f1%a4%ad 1. moduleにしたいプログラムのsource(*.cppとか)を持ってくる 2. cppファイル毎に*.iを書く ← これがswig語 3. swigコマンドで*.iを各スクリプト言語用のwrapper(…

Java for C++ programmer

関数ポインタはない。 → interface 経由で”関数”を持つクラスのシングルトンインスタンスを渡す。リンクはしなくていい。 ただし、ライブラリを使うプログラムを実行するときに、 ライブラリがある場所にパスを通す必要がある。 java -cp lib:. Main → 少な…

Linkers and Loaders

http://www.iecc.com/linker/ リンカの本。

Floating point arithmetic in C++ templates.

http://mi.eng.cam.ac.uk/~er258/code/fp_template.html

高階 template

cxx

ふつうにできるのを知ってショックでした。 以前、それらしく書いてみたらコンパイルエラーだったので、 できないと思い込んでいた。 // パラメータとして型を渡す template class template< typename T > class T0 {}; // パラメータとして1階テンプレート…

g++ -fstrict-aliasing

http://gcc.gnu.org/onlinedocs/gcc-4.0.3/gcc/optimize-options.html#index-fstrict_002daliasing-511 int i = 10; float f = *( (int*)&i );は移植性が低いだけでなく、GCC4の最適化 O2 O1 -fstrict-aliasing -fschedule-insns によって意味が変わる。訂正…

Qt 4.2 -- Qt Tutorial

http://doc.trolltech.com/4.2/tutorial.html 一見 Java awt, swing と似ている。 SIGNAL & SLOT のイベント処理のところは違うけれど。 qmake -project qmake make

bitwise operators

http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#an208 x = ~x; x = compl x; x = x ^ 0; x = x xor 0;

Chapter10.Boost.StaticAssert

http://boost.org/doc/html/boost_staticassert.html コンパイル時に、型変数の値チェックとかできる。 かなり便利かも。 #include <iterator> #include <boost/static_assert.hpp> #include <boost/type_traits.hpp> template <class RandomAccessIterator > RandomAccessIterator foo(RandomAccessIterator from, RandomAccessIterator to) { // this</class></boost/type_traits.hpp></boost/static_assert.hpp></iterator>…

C++ で hashCode

cxx

任意の型のハッシュ値を計算する枠組みは?Java は Object.hashCode() があるので、 ユーザー定義のクラスでもそれにのっとればいい。C++ では boost::hash が凖・標準。 http://www.boost.org/doc/html/hash.htmlvia http://d.hatena.ne.jp/y-hamigaki/2006…

Log4J徹底解説〜目次

http://www.nurs.or.jp/~sug/soft/log4j/index.htm log4j と annotation の連携は面白いかも。

ジェネリック -- Java vs C#

http://homepage2.nifty.com/magicant/programmingmemo/genericsjavavscs.html C# は型引数の数だけクラスを展開し、 そのバイトコードを生成するので、実行時に型引数の情報が残る。 Java は型引数の一致をコンパイル時に判定するが、 バイトコートは型引数…

C++ for Java Programmers - Google Search

http://www.google.com/search?q=c%2b%2b+for+java+programmers これらのドキュメントはだいたい、 Java プログラマが C++ で書けるようになることを目標にしている。 書くのは、大体、逐語訳で書ける。 C++の方が多くの構文を持っているので。# interface …

private/protected/public inheritance -- is-a にならない継承

cxx

Java の継承は C++ の public 継承。つねに is-a 関係が成立する。 #include <iostream> class Root { private: static const int priv = 100; protected: static const int prot = 20; public: static const int pub = 3; public: int get() { return priv + prot + p</iostream>…

Intel C++ Compiler for Linux* - Intel Software Network

http://www.intel.com/cd/software/products/asmo-na/eng/compilers/clin/219856.htmIntel C++ Compiler for Linux*Free Non-commercial Unsupported Compilericc は C++ のソースのみ、 C++ のライブラリとのリンクする。 icpc は いつも(Cのソースでも) C++…

Traits

http://sumim.no-ip.com:8080/wiki/818 via d.y.d.

Copy constructor and implicit type conversion

cxx

#include <iostream> class Complex { private: const double re; const double im; public: Complex(double r = 0.0, double i = 0.0) : re(r), im(i) { std::cout << "constructor: " << *this << std::endl; } Complex(const Complex& c) : re(c.re), im(c.im) { st</iostream>…

Let's Boost

http://www.kmonos.net/alang/boost/ STL の次は Boost。STLもまだまだ勉強中だけど... STLのページ Standard Template Library プログラミング on the Web SGI - STL Programmer's Guide boost::property_mapに書かれている concept というのは、Java の in…

C++ FAQ LITE

http://www.parashift.com/c++-faq-lite/index.html

可変長引数 K&R と ANSI

cxx

Solaris の CC /usr/ucb/cc は K&R らしい。K&R int sendf(va_alist) va_dcl { static char buf[10240]; /* xxx, enough? */ SOCKET s; const char *fmt; va_list args; va_start( args ); s = va_arg( args, SOCKET ); fmt = va_arg( args, char * ); ANSI …

C one-liner

cxx

#! /bin/bash echo $1 > /tmp/col-$$.c gcc /tmp/col-$$.c -o /tmp/col-$$.o \ && /tmp/col-$$.o rm /tmp/col-$$.o /tmp/col-$$.c include が使えないけど。 col 'main(){puts("test");'}

const_cast<>

cxx

const をなくすキャスト。 (実行時には書き込むとSegmentation Fault) #include <cstdio> char * f(char * s) { puts(s); s[0] = '\0'; return s; } int main(int argc, char ** argv) { const char * s = "cc"; f(const_cast<char *>(s)); return 0; }</char></cstdio>

C/C++ Reference

http://www.cppreference.com/ Minimalist なリファレンス。