// allocatorのmax_size()関数の使用例
#include < iostream>
#include < vector>
using namespace std;
int main()
{
vector< short int>::allocator_type si_a;
vector< int>::allocator_type i_a;
vector< long int>::allocator_type li_a;
vector< float>::allocator_type f_a;
vector< double>::allocator_type d_a;
cout << "Here are the number of object that can be allocated.\n";
cout << "short integers: ";
cout << si_a.max_size() << endl;
cout << "integers: ";
cout << i_a.max_size() << endl;
cout << "long integers: ";
cout << li_a.max_size() << endl;
cout << "floats: ";
cout << f_a.max_size() << endl;
cout << "doubles: ";
cout << d_a.max_size() << endl;
return 0;
}
// raw_storage_iteratorの使用例
#include < iostream>
#include < deque>
#include < memory>
#include < algorithm>
using namespace std;
class X {
int a, b;
int sum;
public:
X() { a = b = 0; sum = 0; }
X(int x, int y) { a = x; b = y; }
// コピーコンストラクタ
X(const X &o) {
a = o.a; b = o.b;
sum = o.sum; // sumに値を代入する
}
// 代入のオーバーロード
X operator=(const X &o) {
a = o.a; b = o.b;
// sumに値を代入しない
return *this;
}
void setsum() { sum = a+b; }
void show() {
cout << a << "," << b;
cout << " Sum is: " << sum << endl;
}
};
int main()
{
unsigned char raw1[100], raw2[100];
X *p;
deque< X> q(5);
int i;
for(i=0; i<5; i++) {
q[i] = X(i, i);
q[i].setsum();
}
// 初期化されていないメモリに両端キューを格納する(誤った方法)
copy(q.begin(), q.end(), (X *)raw1);
cout << "Contents of raw memory (incorrect):\n";
p = (X *) raw1;
for(i=0; i<5; i++)
p[i].show();
// 初期化されていないメモリに両端キューを格納する(正しい方法)
copy(q.begin(), q.end(),
raw_storage_iterator((X *)raw2));
cout << "Contents of raw memory (correct):\n";
p = (X *) raw2;
for(i=0; i<5; i++)
p[i].show();
return 0;
}
// 配列をコンテナとして使用する
#include < iostream>
#include < list>
#include < algorithm>
using namespace std;
int main()
{
list< int> lst(10);
list< int>::iterator p;
int *ip, *ip_end;
int nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
cout << "Initial contents of nums: ";
for(i=0; i<10; i++)
cout << nums[i] << " ";
cout << endl;
// nums配列をリストにコピーする
copy(nums, &nums[9], lst.begin());
cout << "Contents of lst after copy: ";
for(p=lst.begin(); p!=lst.end(); p++)
cout << *p << " ";
cout << endl;
// 5よりも小さい要素を削除する
ip_end = remove_copy_if(lst.begin(), lst.end(),
nums, bind2nd(less< int>(), 5));
cout << "Contents of nums after remove_copy_if(): ";
for(ip=nums; ip!=ip_end; ip++)
cout << *ip << " ";
return 0;
}
// bitsetの使用例
#include < iostream>
#include < bitset>
using namespace std;
int main()
{
bitset<16> b(32);
bitset<16> b2(0);
cout << "Original bits: ";
cout << b;
cout << endl;
// ビットを設定する
b[0] = 1;
b[2] = 1;
b[10] = 1;
b[12] = 1;
cout << "Bits after assignment: ";
cout << b;
cout << endl;
// ビットを回転する
b <<= 2;
cout << "Bits after left rotate: ";
cout << b;
cout << endl;
// ビットを反転する
cout << "After flipping bits: ";
b.flip();
cout << b;
cout << endl;
// 設定されているビットをチェックする
if(b.any()) cout << "b has at least 1 bit set.\n";
// 設定されているビットの数をカウントする
cout << "b has " << b.count();
cout << " bits set.\n";
// ビットを3種類の方法でテストする
if(b[0] == 1)
cout << "bit 0 is on\n";
if(b.test(1))
cout << "bit 1 is on\n";
if(b.at(2) == 1)
cout << "bit 2 is on\n";
// ビットを整数に加算する
cout << "Add 11 to bit 0: " << b[0] + 11 << endl;
return 0;
}
// NP_Arrayの基本的な操作の例
#include < iostream>
#include < algorithm>
#include < functional>
#include "np_a.h"
using namespace std;
// 整数を表示する
void display(int v)
{
cout << v << " ";
}
int main()
{
NP_Array< int> ob(5, 5);
NP_Array< int>::iterator p;
int i, sum;
cout << "Size of ob is: " << ob.size() << endl;
cout << "Initial contents of ob:\n";
for(i=-5; i<5; i++) cout << ob[i] << " ";
cout << endl;
// obに値を与える
for(i=-5; i<5; i++) ob[i] = i;
cout << "New values for ob: \n";
p = ob.begin();
do {
cout << *p++ << " ";
} while (p!=ob.end());
cout << endl;
// 負のインデックス位置にある要素の合計を表示する
sum = 0;
for(i = -ob.get_neg_ext(); i<0; i++)
sum += ob[i];
cout << "Sum of values with negative subscripts is: ";
cout << sum << "\n\n";
// copy()を使用して、あるオブジェクトをほかのオブジェクトにコピーする
cout << "Copy ob to ob2 using copy() algorithm.\n";
NP_Array< int> ob2(ob.get_pos_ext(), ob.get_neg_ext());
copy(ob.begin(), ob.end(), ob2.begin());
// for_each()アルゴリズムを使用して、ob2を表示する
cout << "Contents of ob2: \n";
for_each(ob2.begin(), ob2.end(), display);
cout << endl;
// remove_copy_if()を使用して、0よりも小さい値を削除する
cout << "Remove values less than zero and";
cout << " put result into ob3.\n";
NP_Array< int> ob3(ob.get_pos_ext(), ob.get_neg_ext());
replace_copy_if(ob.begin(), ob.end(), ob3.begin(),
bind2nd(less< int>(), 0), 0);
cout << "Contents of ob3: \n";
for_each(ob3.begin(), ob3.end(), display);
cout << "\n\n";
cout << "Swap ob and ob3.\n";
ob.swap(ob3); // obとob3を交換する
cout << "Here is ob3:\n";
for_each(ob3.begin(), ob3.end(), display);
cout << endl;
cout << "Swap again to restore.\n";
ob.swap(ob3); // 元に戻す
cout << "Here is ob3 after second swap:\n";
for_each(ob3.begin(), ob3.end(), display);
cout << "\n\n";
// insert()メンバ関数を使用する
cout << "Element at ob[0] is " << ob[0] << endl;
cout << "Insert values into ob.\n";
ob.insert(ob.end(), -9999);
ob.insert(&ob[1], 99);
ob.insert(&ob[-3], -99);
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Element at ob[0] is " << ob[0] << endl;
cout << endl;
cout << "Insert -7 three times to front of ob.\n";
ob.insert(ob.begin(), 3, -7);
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Element at ob[0] is " << ob[0] << endl;
cout << endl;
// push_back()とpop_back()を使用する
cout << "Push back the value 40 onto ob.\n";
ob.push_back(40);
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Pop back two values from ob.\n";
ob.pop_back(); ob.pop_back();
for_each(ob.begin(), ob.end(), display);
cout << "\n\n";
// erase()を使用する
cout << "Erase element at 0.\n";
p = ob.erase(&ob[0]);
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Element at ob[0] is " << ob[0] << endl;
cout << endl;
cout << "Erase many elements in ob.\n";
p = ob.erase(&ob[-2], &ob[3]);
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Element at ob[0] is " << ob[0] << endl;
cout << endl;
cout << "Insert ob4 into ob.\n";
NP_Array< int> ob4(3, 0);
for(i=0; i<3; i++) ob4[i] = i+100;
ob.insert(&ob[0], ob4.begin(), ob4.end());
for_each(ob.begin(), ob.end(), display);
cout << endl;
cout << "Element at ob[0] is " << ob[0] << endl;
cout << endl;
cout << "Here is ob shown with its indices:\n";
for(i=-ob.get_neg_ext(); i< ob.get_pos_ext(); i++)
cout >< "[" << i << "]: " << ob[i] << endl;
cout << endl;
cout << "Make a copy of ob2.\n";
NP_Array< int> ob5(ob2);
for_each(ob5.begin(), ob5.end(), display);
cout << "\n\n";
cout << "Clear ob.\n";
ob.clear();
for_each(ob.begin(), ob.end(), display);
cout << "Size of ob after clear: " << ob.size();
cout << "\n\n";
cout << "Construct object from a range.\n";
NP_Array< int> ob6(&ob2[-2], ob2.end());
cout << "Size of ob6: " << ob6.size() << endl;
for_each(ob6.begin(), ob6.end(), display);
cout << endl;
return 0;
}
// 関連する演算子の使用例
#include < iostream>
using namespace std;
#include "np_a.h"
// 整数を表示する
void display(int v)
{
cout << v << " ";
}
int main()
{
NP_Array< int> ob1(2, 3), ob2(2, 3), ob3(4, 4);
int i;
// ob1とob2に値を与える
for(i=-3; i<2; i++) {
ob1[i] = i;
ob2[i] = i;
}
cout << "Contents of ob1 and ob2:\n";
for(i=-3; i<2; i++)
cout << ob1[i] << " ";
cout << endl;
for(i=-3; i<2; i++)
cout << ob2[i] << " ";
cout << "\n\n";
if(ob1 == ob2) cout << "ob1 == ob2\n";
if(ob1 != ob2) cout << "error\n";
cout << endl;
cout << "Assign ob1[-1] the value 99\n";
ob1[-1] = 99;
cout << "Contents of ob1 is now:\n";
for(i=-3; i<2; i++)
cout << ob1[i] << " ";
cout << endl;
if(ob1 == ob2) cout << "error\n";
if(ob1 != ob2) cout << "ob1 != ob2\n";
cout << endl;
if(ob1 < ob2) cout << "ob1 < ob2\n";
if(ob1 <= ob2) cout << "ob1 <= ob2\n";
if(ob1 > ob2) cout << "ob1 > ob2\n";
if(ob1 >= ob2) cout << "ob1 >= ob2\n";
if(ob2 < ob1) cout << "ob2 < ob1\n";
if(ob2 <= ob1) cout << "ob2 <= ob1\n";
if(ob2 > ob1) cout << "ob2 > ob1\n";
if(ob2 >= ob1) cout << "ob2 >= ob1\n";
cout << endl;
// オブジェクトのサイズを比較する
if(ob3 != ob1) cout << "ob3 != ob1";
if(ob3 == ob1) cout << "ob3 == ob1";
return 0;
}
// クラスオブジェクトをNP_Arrayに格納する
#include < iostream>
#include "np_a.h"
using namespace std;
class test {
public:
int a;
test() { cout << "constructing\n"; a=0; }
test(const test &o) {
cout << "copy constructor\n";
a = o.a;
}
~test() { cout << "destructing\n"; }
};
int main()
{
NP_Array< test> t(2, 3);
int i;
for(i=-3; i<2; i++) t[i].a = i;
for(i=-3; i<2; i++) cout << t[i].a << " ";
cout << endl;
// 新しいコンテナにコピーする
NP_Array< test> t2(4, 7);
copy(t.begin(), t.end(), &t2[-2]);
cout << "Contents of t2:\n";
for(i=-7; i<4; i++) cout << t2[i].a << " ";
cout << endl;
NP_Array< test> t3(t.begin()+1, t.end()-1);
cout << "Contents of t3:\n";
for(i=t3.get_neg_ext(); i< t3.get_pos_ext(); i++) cout << t3[i].a << " ";
cout << endl;
t.clear();
cout << "Size after clear(): " << t.size() << endl;
// コンテナオブジェクトを代入する
t = t3;
cout << "Contents of t:\n";
for(i=t.get_neg_ext(); i< t.get_pos_ext(); i++) cout << t[i].a << " ";
cout << endl;
return 0;
}