C++

メモだけ

C++

#include <iostream> using namespace std; template <int N, int _3 = N % 3, int _5 = N % 5> struct FizzBuzz { FizzBuzz() { FizzBuzz<N - 1>(); cout << N << " "; } }; template <int N, int _5> struct FizzBuzz<N, 0, _5> { FizzBuzz() { FizzBuzz<N - 1>(); cout << "Fizz "; } }; template <int N, int _3> struc…</int></n></n,></int></n></int></iostream>

gflags

C++

http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/gflags/?root=svn 一応、VCでもビルドできた。 #include <iostream> #include "gflags/gflags.h" #include "gflags/gflags_completions.h" DEFINE_bool(big_menu, false, "Include 'advanced' options in the menu l</iostream>…

cpp圧勝

C++

http://q.hatena.ne.jp/1243223376 VCが多いせいかも。その他が気になる。

vectorの二次元配列

C++

便利なのか微妙なところだ…。 #include <iostream> #include <vector> using namespace std; void f(vector< vector<int> >& vv_) { vector< vector<int> > vv = vv_; for (vector< vector<int> >::const_iterator i = vv.begin(); i != vv.end(); i++) { vector<int> v = *i; for (vector<int>::const_i</int></int></int></int></int></vector></iostream>…

link:typedefテンプレート

C++

http://msdn.microsoft.com/ja-jp/library/cc440199(VS.71).aspx ふむふむ。

typedefテンプレートもどき

C++

typedefにテンプレートは使えないので、継承で何とかしてみる。 #include <iostream> #include <vector> using namespace std; template<typename T> class vecvec : public vector< vector<T> > {}; template <class T> void func(vecvec<T> vv) { for (vecvec<T>::const_iterator i = vv.begin() ; i != vv.</t></t></class></t></typename></vector></iostream>…

typedefテンプレートもどき その2

C++

structを使ってみる。 #include <iostream> #include <vector> using namespace std; template<class T> struct vecvec { typedef vector< vector<T> > type; }; void func(vecvec<int>::type& vv) { for (vecvec<int>::type::const_iterator i = vv.begin() ; i != vv.end() ; i++) { for (vector<int>::c</int></int></int></t></class></vector></iostream>…

Sparse Hash: Linuxでの測定

http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/ruby-sparsehash/ext/sparsehash.cpp?root=svn&view=markup Linux上だと実用的な速度に見えなくもない。 [root@andLinux ruby-sparsehash]# ruby test.rb SparseHashMap 1000000 Sparsehash::SparseHashMa…

sparse_hash_mapを使う

C++

http://google-sparsehash.googlecode.com/ APIは普通のMapと変わらない感じ。 #include <iostream> #include <google/sparse_hash_map> using namespace std; using namespace google; int main() { typedef sparse_hash_map<const char *, const char *> cmap; cmap m; m["foo"] = "hoge"; m["bar"] = "piyo"; m["zoo"] = </const></google/sparse_hash_map></iostream>…

ポリシークラスを使ってみる

C++

Modern C++ Design難しいなぁ…。 #include <iostream> using namespace std; template <class T> class Foo { public: void func() { cout << sizeof(T) << endl; } }; template <class T> class Bar { public: void func() { cout << typeid(T).name() << endl; } }; class Zoo { }; tem</class></class></iostream>…

簡単なキャッシュ(gcc/andlinux)

C++

typenameの罠に見事に嵌った。VCって意外と賢かったのか… #include <iostream> #include <string> #include <sstream> #include <map> #include <boost/asio.hpp> #include <boost/timer.hpp> using namespace std; template <class I, class O> class Cache { map<I, O> m_; O (*f_)(I x); public: Cache(O (*f)(I x)): f_(f) {} O operator ()(…</i,></class></boost/timer.hpp></boost/asio.hpp></map></sstream></string></iostream>

簡単なキャッシュ

C++

#include <iostream> #include <string> #include <sstream> #include <map> #ifdef _WIN32 #define _WIN32_WINNT 0x0501 #endif #include <boost/asio.hpp> #include <boost/timer.hpp> using namespace std; template <class I, class O> class Cache { map<I, O> m_; O (*f_)(I x); public: Cache(O (*f)(I x)): f_(f) {…</i,></class></boost/timer.hpp></boost/asio.hpp></map></sstream></string></iostream>

30分経ってもboostのビルドが終わらない…

C++

もう寝るか…

関数の返値をパラメータとするテンプレート関数

C++

gcc 3.4だと出来ないと思ってたら出来た。 #include <iostream> #include <string> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; template <class I, class O> class Foo { O (*f_)(I x); public: Foo(O (*f)(I x)): f_(f) {} O operator ()(I x) { return f_(x); } }; int xstr2int(s</class></boost/lexical_cast.hpp></string></iostream>…

libarchiveを使う

C C++

http://people.freebsd.org/~kientzle/libarchive/ ふむふむ。 #include <iostream> #include <string> #include <archive.h> #include <archive_entry.h> using namespace std; namespace { void list(string filename) { archive *a = archive_read_new(); archive_read_support_compression_all(a); arch</archive_entry.h></archive.h></string></iostream>…

bindを実装してみる

C++

意外とわかりやすい…と思うんだけど。 #include <iostream> using namespace std; template <class R, class A> class Function { R (*f_)(A); A x_; public: Function(R (*f)(A), A x) : f_(f), x_(x) {} R operator() () { return f_(x_); } }; template <class R, class A> Function<R, A> bind(R (*f)(A), A x</r,></class></class></iostream>…

bind1st、bind2nd

C++

第一引数は「引数を2つとる関数オブジェクト」。 汎用的でないのはplus、minusとかのヘルパ関数だからかな。 #include <iostream> #include <functional> using namespace std; int count(int m, int n) { return m * 2 + n; } int main() { cout << bind1st(ptr_fun(count), 3)(5</functional></iostream>…

tr1:bindを使う

C++

VC9のtr1の実装具合ってどんなもんだろ。 #include <iostream> #include <functional> using namespace std; int count1(int n) { return ++n; } int count2(int &n) { return ++n; } int count3(int *n) { return ++(*n); } int main() { int n1 = 0, n2 = 0, n3 = 0; tr1::functi</functional></iostream>…

link: Q&A on our TR1 implementation

C++

http://d.hatena.ne.jp/ntnek/20080116/p1 めもめも。

戻り値・ポインタ

C++

関数の戻り値のstringをコピーで返すのは、C++だとふつーのこと? どうも、ポインタで渡すパターンは少ないように見える。スタンダードなん?

LNK1000

C++

Visual C++ プロジェクトを Microsoft Visual Studio 2008 で /INCREMENTAL ビルド オプションを使用して、リンクすると、次のエラー メッセージが表示される可能性があります。

boost::asio: echoサーバ

C++

う、動いてる… #include <iostream> #include <string> #define _WIN32_WINNT 0x0501 #include <boost/bind.hpp> #include <boost/asio.hpp> using namespace std; using boost::bind; using boost::asio::io_service; using boost::asio::ip::tcp; using boost::system::error_code; void echo(tcp::acceptor *</boost/asio.hpp></boost/bind.hpp></string></iostream>…

boost::bind萌え〜

C++

思わず顔がにやけてしまった。 #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/function.hpp> using namespace std; int add(int a, int b) { return a + b; } int main() { boost::function<int (int)> f = boost::bind(add, 5, _1); cout << f(1) << endl; cout << f(2) << endl; return </int></boost/function.hpp></boost/bind.hpp></string></iostream>…

boost:クロージャを作る

C++

萌え。 #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp> using namespace std; int count(boost::shared_ptr<int> n) { (*n)++; return *n; } int main() { boost::shared_ptr<int> n(new int(0)); boost::function<int (void)> f = boost::…</int></int></int></boost/shared_ptr.hpp></boost/function.hpp></boost/bind.hpp></string></iostream>

てきとーにスマートポインタを実装する

C++

shared_ptrの仕組みが気になったので、てきとーに実装してみる。 #include <iostream> using namespace std; template <typename T> class Ptr { T *p; int *cnt; public: Ptr(T *p) : p(p) { cnt = new int(1); cout << "count=" << *cnt << endl; } Ptr(const Ptr<T> &self) { p = s</t></typename></iostream>…

boost::asio

C++

boost::asioすごいなー。 #include <iostream> #include <string> #define _WIN32_WINNT 0x0501 #include <boost/asio.hpp> using namespace std; using namespace boost::asio; int main(int argc, char* argv[]) { ip::tcp::iostream s("www.boost.org", "http"); s << "GET / HTTP/1.1\r\n";</boost/asio.hpp></string></iostream>…

Boost C++ Librariesプログラミング

C++

Boost C++ Librariesプログラミング作者: 稲葉一浩出版社/メーカー: 秀和システム発売日: 2007/07メディア: 単行本購入: 4人 クリック: 235回この商品を含むブログ (34件) を見る日曜日に購入。小さな機能からちょこちょこ使ってみる…つもり。 しかし、便利…

テンプレートの特殊化

C++

http://www.geocities.jp/ky_webid/cpp/language/037.html Modern C++ Designを読み始めた。 でも、難しい…Modern C++ Design―ジェネリック・プログラミングおよびデザイン・パターンを利用するための究極のテンプレート活用術 (C++ In‐Depth Series)作者: …

Modern C++ Design

あとで買う。Modern C++ Design―ジェネリック・プログラミングおよびデザイン・パターンを利用するための究極のテンプレート活用術 (C++ In‐Depth Series)作者: アンドレイアレキサンドレスク,Andrei Alexandrescu,村上雅章出版社/メーカー: ピアソンエデュ…

echoサーバを書いてみた: 例外を使う

C++

さすがに「これはひどい」だったので、例外を使うように書き直す。 テンプレート、便利だなー。 #include <string> #include <iostream> #include <sstream> #include <winsock2.h> namespace wsa { int cleanup() { return WSACleanup(); } void startup(WORD version) { WSADATA wsa_data; int n; </winsock2.h></sstream></iostream></string>…