// 31_31-1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>

using namespace std;

void disp_i(int *d, int n)
{
	for (int i = 0; i < n; i++) {
		cout << *d++ << " ";
	}
	cout << '\n';
}

void disp_v(vector<int> &obj)
{
	vector<int>::iterator p;
	for (p = obj.begin(); p != obj.end(); p++) {
		cout << *p << " ";
	}
	cout << '\n';
}

int _tmain(int argc, _TCHAR* argv[])
{
	int *ip;
	vector<int>::iterator vp;

	int n1[5] = { 30, 20, 50, 10, 40 };
	int n2[5] = { 15, 25, 30, 40, 55 };
	int n3[10];
	vector<int> v1(n1, n1+5);
	vector<int> v2(n2, n2+5);
	vector<int> v3(v1.size() + v2.size());

	cout << "-----n1とv1の内容\n";
	cout << "n1: ";
	disp_i(n1, 5);
	cout << "v1: ";
	disp_v(v1);

	cout << "-----n1とv1をsort\n";
	sort(n1, n1+5);
	sort(v1.begin(), v1.end());
	cout << "n1: ";
	disp_i(n1, 5);
	cout << "v1: ";
	disp_v(v1);

	cout << "-----n1をbinary_search\n";
	if (binary_search(n1, n1+5, 30)) {
		cout << "30は登録あり\n";
	}
	else {
		cout << "30は登録なし\n";
	}

	cout << "-----v1をbinary_search\n";
	if (binary_search(v1.begin(), v1.end(), 33)) {
		cout << "33は登録あり\n";
	}
	else {
		cout << "33は登録なし\n";
	}

	cout << "-----mergeを処理する\n";
	merge(n1, n1+5, n2, n2+5, n3);
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	cout << "n3: ";
	disp_i(n3, 10);
	cout << "v3: ";
	disp_v(v3);

	// 注意:unique処理で不要なデータは末尾側に残される
	cout << "-----unique処理をし、そのままの要素を表示\n";
	ip = unique(n3, n3+10);
	vp = unique(v3.begin(), v3.end());
	cout << "n3: ";
	disp_i(n3, 10);
	cout << "v3: ";
	disp_v(v3);

	cout << "-----n3の先頭側の有効な値だけ表示\n";
	cout << "n3: ";
	disp_i(n3, ip-n3);

	cout << "-----v3の末尾の不要なデータを削除して表示\n";
	v3.erase(vp, v3.end());
	cout << "v3: ";
	disp_v(v3);

	MessageBox(NULL, _T("Check Console Window"), L"MessageBox", MB_OK);

	return 0;
}

