#include < iostream >
#include < list >
using namespace std;
int main()
{
list< char > lst1, lst2;
int i;
for(i=0; i< 10; i+=2) lst1.push_back('A'+i);
for(i=1; i< 11; i+=2) lst2.push_back('A'+i);
cout << "lst1 content: ";
list< char >::iterator p = lst1.begin();
while(p != lst1.end()){
cout << *p;
p++;
}
cout << endl << endl;
cout << "lst2 content: ";
p = lst2.begin();
while(p != lst2.end()){
cout << *p;
p++;
}
cout << endl << endl;
lst1.merge(lst2);
if(lst2.empty())
cout << "lst2 is now empty\n";
cout << "merged lst1 content\n";
p = lst1.begin();
while(p != lst1.end()){
cout << *p;
p++;
}
return 0;
}
双方向リスト
#include < iostream >
#include < list >
using namespace std;
int main()
{
list lst;
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;
}
リスト
#include < iostream >
#include < list >
using namespace std;
int main()
{
list lst;
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()
{
vector v;
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;
}