C++

link: strstream と stringstream

C++

http://www5d.biglobe.ne.jp/~y0ka/2005-08-17-3.html ふむふむ。

mapを回す

C++

「p」を参照にもポインタにもできない…なぜー? #include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; void handle(pair<string, string> p) { cout << p.first << ":" << p.second << endl; } int main() { map<string, string> m; m.insert(make_pair("foo", "FOO")); m.insert(m</string,></string,></algorithm></map></string></iostream>…

echoサーバを書いてみた

C++

例外使わないと、どうしてもCっぽくなるなー。 #include <string> #include <iostream> #include <winsock2.h> namespace wsa { int cleanup() { return WSACleanup(); } void startup(WORD version) { using namespace std; WSADATA wsa_data; int n; if ((n = WSAStartup(version, &wsa_d</winsock2.h></iostream></string>…

vectorを使ってみる

C++

#include <iostream> #include <vector> using namespace std; class Foo { public: Foo() { cout << "new:" << this << endl; } Foo(const Foo &foo) { cout << "copy:" << this << endl; } Foo &operator=(Foo foo) { cout << "=:" << this << endl; return *this; } ~Foo() </vector></iostream>…

vectorを使ってみる: ポインタにしてみる

C++

#include <iostream> #include <vector> using namespace std; class Foo { public: Foo() { cout << "new:" << this << endl; } Foo(const Foo &foo) { cout << "copy:" << this << endl; } Foo &operator=(Foo foo) { cout << "=:" << this << endl; return *this; } ~Foo() </vector></iostream>…

vectorを配列にしてみる

C++

#include <iostream> #include <vector> using namespace std; void puts_array(int *p, int len) { for (int i = 0; i < len; i++) { cout << p[i] << endl; } } int main() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } puts_array(&v[0], v.size()); retu</int></vector></iostream>…

汎用クラス

C++

とりあえず使ってみる。 「return 0;」が気になるな… #include <iostream> using namespace std; template <class T> class Stack { private: T *stack; int size; int tos; public: Stack(int size); ~Stack(); bool push(T x); T pop(); bool empty(); }; template <class T> Stack<T>::St</t></class></class></iostream>…

例外の制限

C++

VCだとサポートされず。(コンパイルエラーにはならず、無視される) #include <iostream> using namespace std; void func() throw(int) { throw 0L; } int main() { try { func(); } catch(...) { } } $ g++ -g foo.cpp -o foo && ./foo Hangup</iostream>

terminateとunexpected

C++

#include <iostream> using namespace std; void func() throw(int) { throw 0L; } void uhandler() { cout << "uhandler()" << endl; } void thandler() { cout << "thandler()" << endl; exit(1); } int main() { set_unexpected(uhandler); set_terminate(thandler)</iostream>…

例外を上に投げる

C++

「...」で受け取っても上に投げれる、と。 #include <iostream> using namespace std; void foo() { throw "error"; } void bar() { try { foo(); } catch (...) { cout << "error at foo()" << endl; throw; } } int main() { try { bar(); } catch (char *e) { cout <</iostream>…

new(nothrow)

C++

VCでもビルドできた。 bad_allocはどっかでハンドルすべきなのかなぁ… #include <iostream> using namespace std; int main() { int *i = new(nothrow) int; }</iostream>

template、template

C++

#include <iostream> using namespace std; template <class T> class Foo { T x; public: Foo(T x); void show(); template <class TT> void func(T x, TT y); }; template <class T> Foo<T>::Foo(T x) : x(x) {} template <class T> void Foo<T>::show() { cout << x << endl; } template <class T> template <class TT> …</class></class></t></class></t></class></class></class></iostream>

typeidを使ってみる

C++

#include <iostream> #include <typeinfo> using namespace std; template <class T> class Foo { public: void show() { cout << typeid(Foo<T>).name() << endl; } }; int main() { Foo<int> foo; Foo<Foo<int>> bar; cout << typeid(foo).name() << endl; cout << typeid(bar).name() << endl; return 0;</foo<int></int></t></class></typeinfo></iostream>…

dynamic_castとvirtual

C++

http://rararahp.cool.ne.jp/cgi-bin/lng/vc/vclng.cgi?print+200404/04040017.txt うーん、わかるようなわからないような。 確かに、virtualなメンバ関数を持っていないような基本クラスを派生クラスにキャストする状況は少ない気がする。 どっかに、仕様な…

link: Deep C++ 演算:static_cast

C++

http://msdn.microsoft.com/ja-jp/library/cc440191(VS.71).aspx 酔った頭だとぜんぜん理解できない…

仮想関数

C++

virtualをつけないとオーバライドされない、と。 #include <iostream> using namespace std; class Foo { public: void func() { cout << "Foo::func()" << endl; } virtual void vfunc() { cout << "Foo::vfunc()" << endl; } }; class Bar : public Foo { public: vo</iostream>…

仮想関数: キャストしてみる

C++

#include <iostream> using namespace std; class Foo { public: void func() { cout << "Foo::func()" << endl; } virtual void vfunc() { cout << "Foo::vfunc()" << endl; } }; class Bar : public Foo { public: void func() { cout << "Bar::func()" << endl; } v</iostream>…

純粋仮想関数

C++

インスタンス化はできない、と。 #include <iostream> using namespace std; class Foo { public: void func() { cout << "Foo::func()" << endl; } virtual void vfunc() = 0; }; class Bar : public Foo { public: void func() { cout << "Bar::func()" << endl; } v</iostream>…

純粋仮想関数: 0以外にしてみる

C++

コンパイルできず。当たり前か。 でも、変わったシンタックスだなー。 #include <iostream> using namespace std; class Foo { public: void func() { cout << "Foo::func()" << endl; } virtual void vfunc() = 1; }; int main() { return 0; } $ g++ -g foo.cpp -o f</iostream>…

純粋仮想関数: 下に丸投げ

C++

#include <iostream> using namespace std; class Foo { public: virtual void vfunc() = 0; }; class Bar : public Foo { public: virtual void vfunc() = 0; }; class Zoo : public Bar { public: void vfunc() { cout << "Zoo::vfunc()" << endl; } }; int main() {</iostream>…

純粋仮想デストラクタ

C++

http://archive.mag2.com/0000236703/index.html これって定石なのかなぁ? #include <iostream> using namespace std; class Foo { public: virtual ~Foo() = 0; }; Foo::~Foo() {} class Bar : public Foo {}; int main() { Bar bar; return 0; }</iostream>

純粋仮想関数: 参照で操作

C++

できた。 #include <iostream> using namespace std; class Foo { public: virtual void func() = 0; }; class Bar : public Foo { public: void func() { cout << "Bar::func()" << endl; } }; int main() { Bar bar; Foo &foo = bar; foo.func(); return 0; } Bar::f</iostream>…

純粋仮想関数: 使ってみる

C++

使ってみる。 #include <iostream> using namespace std; class Bike { public: virtual void shift_gear() = 0; }; class Suzuki : public Bike { public: void shift_gear() { cout << "スコンスコン" << endl; } }; class Kawasaki : public Bike { public: void sh</iostream>…

汎用関数

C++

テンプレートの展開(?)はコンパイル時、「<>」で明示的に指定できる、と。 #include <iostream> #include <string> using namespace std; template <class X> void func(X &x) { cout << x << endl; } int main() { int i = 100; string s = "foo"; func(i); func<int>(i); func(s); func<string>(s);</string></int></class></string></iostream>…

link: テンプレートあれこれ (4) -- typename の役割

C++

http://www.fides.dti.ne.jp/~oka-t/cpplab-template-4.html staticはCの問題じゃないかなぁ。

汎用関数のオーバーロード

C++

テンプレートの前に関数が定義されていても、隠蔽される、と。 少なくともVCではそうなった。 #include <iostream> #include <string> using namespace std; void func(int &x) { cout << "overload" << endl; } template <class X> void func(X &x) { cout << x << endl; } int main() </class></string></iostream>…

汎用関数について思ったこと

C++

引数の参照渡しのことを考えると、組み込み型とユーザ定義型をまたぐようなテンプレートを作るべきじゃないのかなぁ、など思った。 「func(100)」とかできないのはいやだし、一時オブジェクトのビミョーな動きを考えると、ユーザ定義型はなるべく参照で渡し…

関数オブジェクトを使ってみる

C++

面白いなぁ。 #include <iostream> using namespace std; class Fib { private: int a; int b; public: Fib() : a(1), b(1) {} int operator() (){ int tmp = a; a = b; b = tmp + b; return tmp; } }; int main() { Fib fib; for (int i = 0; i < 10; i++) { cout << </iostream>…

stringへの入出力

C++

「#include 」が必要。 クラスを使うだけなら「#include 」だけでいいのはなんでだろう? #include <string> #include <iostream> using namespace std; int main() { string s; cin >> s; cout << s; return 0; }</iostream></string>

cin.unsetf(ios::skipws)

C++

読み込めない…なんでー? #include <iostream> using namespace std; int main() { char cs[256]; cin.unsetf(ios::skipws); cin >> cs; cout << cs << endl; return 0; } 100続行するには何かキーを押してください . . .</iostream>