#include < iostream > #include < list > using namespace std; int main() { listlst; list revlst; int i; for(i=0; i< 10; i++) lst.push_back('A'+i); cout << "lst size = " << lst.size() << endl; cout << "size: "; list< char >::iterator p; while(!lst.empty()){ p = lst.begin(); cout << *p; lst.pop_front(); revlst.push_front(*p); } cout << endl << endl; cout << "revlst size = "; cout << revlst.size() << endl; cout << "content: "; p = revlst.begin(); while(p != revlst.end()){ cout << *p; p++; } return 0; }
Month: May 2016
リスト
#include < iostream > #include < list > using namespace std; int main() { listlst; int i; for(i=0; i<10; i++) lst.push_back('A'+i); cout << "size = " << lst.size() << endl; list ::iterator p; cout << "content: "; while(!lst.empty()){ p = lst.begin(); cout << *p; lst.pop_front(); } return 0; }
オーバーロードバージョン
#include < iostream > #include < vector > using namespace std; class Demo { double d; public: Demo() { d = 0.0; } Demo(double x){ d = x; } Demo &operator=(double x){ d = x; return *this; } double getd() { return d; } }; bool operator<(Demo a, Demo b) { return a.getd() < b.getd(); } bool operator==(Demo a, Demo b) { return a.getd() == b.getd(); } int main() { vectorv; int i; for(i=0; i<10; i++) v.push_back(Demo(i/3.0)); for(i=0; i
反復子を利用してベクトルにアクセス
#include < iostream > #includeusing namespace std; int main() { vector v; int i; for(i=0; i<10; i++) v.push_back(i); for(i=0; i<10; i++) cout << v[i] << " "; cout << endl; vector ::iterator p = v.begin(); while(p != v.end()){ cout << *p << " "; p++; } return 0; }
コンストラクタ
#include < iostream > using namespace std; class myclass { int a; public: myclass(int x){ a= x; } int geta() { return a; } }; int main() { myclass ob(4); cout << ob.geta(); return 0; }
new(nothrow)オプション
割り当てエラーを強制的に発生させています。
#include#include < new > using namespace std; int main() { double *p; do { p = new(nothrow) double[100000]; if(p) cout << "succeed memory set\n"; else cout << "memory set error\n"; } while(p); return 0; }
try/catchブロック
#include < iostream > #includeusing namespace std; int main() { int *p; try { p = new int; } catch (bad_alloc xa){ cout << "error memory set.\n"; return 1; } for(*p = 0; *p < 10; (*p)++) cout << *p << " "; delete p; return 0; }
catchの使用方法
#include < iostream > using namespace std; void Xhandler(int test) { try{ if(test==0) throw test; if(test==1) throw 'a'; if(test==2) throw 123.23; } catch(...){ cout << "get!\n"; } } int main() { cout << "start\n"; Xhandler(0); Xhandler(1); Xhandler(2); cout << "finish"; return 0; }
catchの例外処理
#include < iostream > using namespace std; int main() { cout << "start\n"; try { cout << "try block internal\n"; throw 10; cout << "this is not action"; } catch (int i){ cout << "get! number is : "; cout << i << "\n"; } cout << "finish"; return 0; }
複数の汎用データ型の定義
#include < iostream > using namespace std; templateclass myclass { Type1 i; Type2 j; public: myclass(Type1 a, Type2 b) { i = a; j = b; } void show() { cout << i << ' ' << j << '\n'; } }; int main() { myclass ob1(10, 0.23); myclass ob2('X', "This is a test"); ob1.show(); ob2.show(); return 0; }