C++中所有容器及算法的详细列子(包括其函数的使用)

这篇博客详细列举了C++中的各种容器及其算法应用,包括相关函数的使用示例。每个示例都有详尽的注释,方便读者理解和直接运行。主要内容涉及模板类的应用。
摘要由CSDN通过智能技术生成

在学C++的时候学到了容器和算法。看了资料,将所有的demo罗列出来,其中代码中全部注释,没一个带有=============的为一个main函数,用到的头文件都写在最前面,将注释去掉就可以直接运行。

//============================================================================
// Name        : ACC.cpp
// Author      : zhubin
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
#include <string>
#include <numeric>
#include <list>
#include <set>
#include <stack>
#include <valarray>
#include <math.h>
#include <map>
#include <deque>
using namespace std;
///**
// *
// * //========================= list 容器数据===================================
// * */
/*//创建一个 list 容器的实例 LISTINT
typedef list<int> LISTINT;
//创建一个 list 容器的实例 LISTCHAR
typedef list<int> LISTCHAR;
void main(void) {
//--------------------------
	//用 list 容器处理整型数据
//--------------------------
	//用 LISTINT 创建一个名为 listOne 的 list 对象
	LISTINT listOne;
//声明 i 为迭代器
	LISTINT::iterator i;
//从前面向 listOne 容器中添加数据
	listOne.push_front(2);
	listOne.push_front(1);
//从后面向 listOne 容器中添加数据
	listOne.push_back(3);
	listOne.push_back(4);
//从前向后显示 listOne 中的数据
	cout << "listOne.begin()--- listOne.end():" << endl;
	for (i = listOne.begin(); i != listOne.end(); ++i)
		cout << *i << " ";
	cout << endl;
//从后向后显示 listOne 中的数据
	LISTINT::reverse_iterator ir;
	cout << "listOne.rbegin()---listOne.rend():" << endl;
	for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) {
		cout << *ir << " ";
	}
	cout << endl;
//使用 STL 的 accumulate(累加)算法
	int result = accumulate(listOne.begin(), listOne.end(), 0);
	cout << "Sum=" << result << endl;
	cout << "------------------" << endl;
//--------------------------//用 list 容器处理字符型数据
//--------------------------//用 LISTCHAR 创建一个名为 listOne 的 list 对象
	LISTCHAR listTwo;
//声明 i 为迭代器
	LISTCHAR::iterator j;
//从前面向 listTwo 容器中添加数据
	listTwo.push_front('A');
	listTwo.push_front('B');
//从后面向 listTwo 容器中添加数据
	listTwo.push_back('x');
	listTwo.push_back('y');
//从前向后显示 listTwo 中的数据
	cout << "listTwo.begin()---listTwo.end():" << endl;
	for (j = listTwo.begin(); j != listTwo.end(); ++j)
		cout << char(*j) << " ";
	cout << endl;
//使用 STL 的 max_element 算法求 listTwo 中的最大元素并显示
	j = max_element(listTwo.begin(), listTwo.end());
	cout << "The  maximum  element  in  listTwo  is: " << char(*j) << endl;
}*/
///**
// *
// * //========================= vector 容器数据===================================
// * */
/*
typedef vector<int> INTVECTOR;
//测试 vector 容器的功能
void main(void)
{
//vec1 对象初始为空
INTVECTOR vec1;
//vec2 对象最初有 10 个值为 6 的元素
INTVECTOR vec2(10,6);
//vec3 对象最初有 3 个值为 6 的元素
INTVECTOR vec3(vec2.begin(),vec2.begin()+3);
//声明一个名为 i 的双向迭代器
INTVECTOR::iterator i;
//从前向后显示 vec1 中的数据
cout<<"vec1.begin()--vec1.end():"<<endl;
for (i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i << " ";
cout << endl;
//从前向后显示 vec2 中的数据
cout<<"vec2.begin()--vec2.end():"<<endl;
for (i =vec2.begin(); i !=vec2.end(); ++i)
cout << *i << " ";
cout << endl;
//从前向后显示 vec3 中的数据
cout<<"vec3.begin()--vec3.end():"<<endl;
for (i =vec3.begin(); i !=vec3.end(); ++i)
cout << *i << " ";
cout << endl;
//测试添加和插入成员函数
vec1.push_back(2);
vec1.push_back(4);
vec1.insert(vec1.begin()+1,5);
vec1.insert(vec1.begin()+1,vec3.begin(),vec3.end());
cout<<"push() and insert():" <<endl;
for (i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i << " ";
cout << endl;
//测试赋值成员函数
vec2.assign(8,1);
cout<<"vec2.assign(8,1):" <<endl;
for (i =vec2.begin(); i !=vec2.end(); ++i)
cout << *i << " ";
cout << endl;
//测试引用类函数
cout<<"vec1.front()="<<vec1.front()<<endl;
cout<<"vec1.back()="<<vec1.back()<<endl;
cout<<"vec1.at(4)="<<vec1.at(4)<<endl;
cout<<"vec1[4]="<<vec1[4]<<endl;
//测试移出和删除
vec1.pop_back();
vec1.erase(vec1.begin()+1,vec1.end()-2);
cout<<"vec1.pop_back() and vec1.erase():" <<endl;
for (i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i << " ";
cout << endl;
//显示序列的状态信息
cout<<"vec1.capacity(): "<<vec1.capacity()<<endl;
cout<<"vec1.max_size(): "<<vec1.max_size()<<endl;
cout<<"vec1.size(): "<<vec1.size()<<endl;
cout<<"vec1.empty(): "<<vec1.empty()<<endl;
//vector 序列容器的运算
cout<<"vec1==vec3: "<<(vec1==vec3)<<endl;
cout<<"vec1<=vec3: "<<(vec1<=vec3)<<endl;
}
*/

///**
// *
// * //========================= deque 容器数据===================================
// * */
/*typedef deque<int> INTDEQUE;
//从前向后显示 deque 队列的全部元素
void put_deque(INTDEQUE deque, char *name) {
	INTDEQUE::iterator pdeque;
	cout << "The contents of " << name << " : ";
	for (pdeque = deque.begin(); pdeque != deque.end(); pdeque++)
		cout << *pdeque << " ";
	cout << endl;
}
//测试 deqtor 容器的功能
void main(void) {
//deq1 对象初始为空
	INTDEQUE deq1;
//deq2 对象最初有 10 个值为 6 的元素
	INTDEQUE deq2(10, 6);
//deq3 对象最初有 3 个值为 6 的元素
	INTDEQUE deq3(deq2.begin(), deq2.begin() + 3);
//声明一个名为 i 的双向迭代器变量
	INTDEQUE::iterator i;
//从前向后显示 deq1 中的数据
	put_deque(deq1, "deq1");
//从前向后显示 deq2 中的数据
	put_deque(deq2, "deq2");
//从前向后显示 deq3 中的数据
	put_deque(deq3, "deq3");
//从 deq1 序列后面添加两个元素
	deq1.push_back(2);
	deq1.push_back(4);
	cout << "deq1.push_back(2) and deq1.push_back(4):" << endl;
	put_deque(deq1, "deq1");
//从 deq1 序列前面添加两个元素
	deq1.push_front(5);
	deq1.push_front(7);
	cout << "deq1.push_front(5) and deq1.push_front(7):" << endl;
	put_deque(deq1, "deq1");
//在 deq1 序列中间插入数据
	deq1.insert(deq1.begin() + 1, 3, 9);
	cout << "deq1.insert(deq1.begin()+1,3,9):" << endl;
	put_deque(deq1, "deq1");
//测试引用类函数
	cout << "deq1.front()=" << deq1.front() << endl;
	cout << "deq1.back()=" << deq1.back() << endl;
	cout << "deq1.at(4)=" << deq1.at(4) << endl;
	cout << "deq1[4]=" << deq1[4] << endl;
	deq1.at(1) = 10;
	deq1[2] = 12;
	cout << "deq1.at(1)=10 and deq1[2]=12 :" << endl;
	put_deque(deq1, "deq1");
//从 deq1 序列的前后各移去一个元素
	deq1.pop_front();
	deq1.pop_back();
	cout << "deq1.pop_front() and deq1.pop_back():" << endl;
	put_deque(deq1, "deq1");
//清除 deq1 中的第 2 个元素
	deq1.erase(deq1.begin() + 1);
	cout << "deq1.erase(deq1.begin()+1):" << endl;
	put_deque(deq1, "deq1");
//对 deq2 赋值并显示
	deq2.assign(8, 1);
	cout << "deq2.assign(8,1):" << endl;
	put_deque(deq2, "deq2");
//显示序列的状态信息
	cout << "deq1.max_size(): " << deq1.max_size() << endl;
	cout << "deq1.size(): " << deq1.size() << endl;
	cout << "deq1.empty(): " << deq1.empty() << endl;
//deqtor 序列容器的运算
	cout << "deq1==deq3: " << (deq1 == deq3) << endl;
	cout << "deq1<=deq3: " << (deq1 <= deq3) << endl;
}*/
///**
// *
// * //========================= list 容器数据===================================
// * */
/*typedef list<int> INTLIST;
//从前向后显示 list 队列的全部元素
void put_list(INTLIST list, char *name) {
	INTLIST::iterator plist;
	cout << "The contents of " << name << " : ";
	for (plist = list.begin(); plist != list.end(); plist++)
		cout << *plist << " ";
	cout << endl;
}
//测试 list 容器的功能
void main(void) {
//list1 对象初始为空
	INTLIST list1;
//list2 对象最初有 10 个值为 6 的元素
	INTLIST list2(10, 6);
//list3 对象最初有 3 个值为 6 的元素
	INTLIST list3(list2.begin(), --list2.end());
//声明一个名为 i 的双向迭代器
	INTLIST::iterator i;
//从前向后显示各 list 对象的元素
	put_list(list1, "list1");
	put_list(list2, "list2");
	put_list(list3, "list3");
//从 list1 序列后面添加两个元素
	list1.push_back(2);
	list1.push_back(4);
	cout << "list1.push_back(2) and list1.push_back(4):" << endl;
	put_list(list1, "list1");
//从 list1 序列前面添加两个元素
	list1.push_front(5);
	list1.push_front(7);
	cout << "list1.push_front(5) and list1.push_front(7):" << endl;
	put_list(list1, "list1");
//在 list1 序列中间插入数据
	list1.insert(++list1.begin(), 3, 9);
	cout << "list1.insert(list1.begin()+1,3,9):" << endl;
	put_list(list1, "list1");
//测试引用类函数
	cout << "list1.front()=" << list1.front() << endl;
	cout << "list1.back()=" << list1.back() << endl;
//从 list1 序列的前后各移去一个元素
	list1.pop_front();
	list1.pop_back();
	cout << "list1.pop_front() and list1.pop_back():" << endl;
	put_list(list1, "list1");
//清除 list1 中的第 2 个元素
	list1.erase(++list1.begin());
	cout << "list1.erase(++list1.begin()):" << endl;
	put_list(list1, "list1");
//对 list2 赋值并显示
	list2.assign(8, 1);
	cout << "list2.assign(8,1):" << endl;
	put_list(list2, "list2");
//显示序列的状态信息
	cout << "list1.max_size(): " << list1.max_size() << endl;
	cout << "list1.size(): " << list1.size() << endl;
	cout << "list1.empty(): " << list1.empty() << endl;
//list 序列容器的运算
	put_list(list1, "list1");
	put_list(list3, "list3");
	cout << "list1>list3: " << (list1 > list3) << endl;
	cout << "list1<list3: " << (list1 < list3) << endl;
//对 list1 容器排序
	list1.sort();
	put_list(list1, "list1");
//结合处理
	list1.splice(++list1.begin(), list3);
	put_list(list1, "list1");
	put_list(list3, "list3");
}*/

///**
// *
// * //========================= set 容器数据===================================
// * */
/*//创建 set 模板的实例
typedef set<int> SET_INT;
//put_HTset 函数,从头向尾显示 set 容器的所有元素
void put_HTset(SET_INT set1, char *name) {
	SET_INT::iterator it;
	cout << name << ": ";
	cout << "Head to Tail=";
	for (it = set1.begin(); it != set1.end(); ++it)
		cout << (*it) << " ";
	cout << endl;
}
//put_THset 函数,从尾向头显示 set 容器的所有元素
void put_THset(SET_INT s1, char *name) {
	SET_INT::reverse_iterator i;
	cout << name << ": ";
	cout << "Tail to Head=";
	for (i = s1.rbegin(); i != s1.rend(); i++)
		cout << (*i) << " ";
	cout << endl;
}
//测试 set 模板
void main(void) {
	int i;
//声明 set 的对象和迭代器
	SET_INT s1;       //容器初始尾空
	SET_INT::iterator it;
//向 s1 对象中插入值
	for (i = 1; i < 20; i = i + 2) {
		s1.insert(i);
	}
//正向显示 s1 中的数据
	put_HTset(s1, "s1");
//反向显示 s1 中的数据
	put_THset(s1, "s1");
//构造含有元素的序列并显示
	SET_INT s2(s1);
	put_HTset(s2, "s2");
//删除 s2 的第 2 个元素并显示
	s2.erase(++s2.begin());
	put_HTset(s2, "s2");
//向 s2 插入 8 和 9 并显示
	s2.insert(8);
	s2.insert(9);
	put_HTset(s2, "s2");
//清空 s2 的序列
	s2.clear();
	put_HTset(s2, "s2");
//按关键给定的区间显示序列中的元素
	cout << "[s1.lower_bound(5),s1.upper_bound(15)] :";
	for (it = s1.lower_bound(4); it != s1.upper_bound(16); it++)
		cout << (*it) << " ";
	cout << endl;
//显示 s1 的状态信息
	cout << "s1.size():" << s1.size() << endl;
	cout << "s1.max_size():" << s1.max_size() << endl;
	cout << "s1.count(15):" << s1.count(15) << endl;
//交换两个 set 容器的元素并显示
	s1.swap(s2);
	put_HTset(s1, "s1");
	put_HTset(s2, "s2");
//关系运算
	s1.insert(5);
	cout << "s1>s2 = " << (s1 > s2) << endl;
}*/
///**
// *
// * //========================= multiset 容器数据===================================
// * */
/*//创建 multiset 模板的实例
typedef multiset<int> MULTISET_INT;
//put_HTset 函数,从头向尾显示 multiset 容器的所有元素
void put_HTset(MULTISET_INT set1, char *name) {
	MULTISET_INT::iterator it;
	cout << name << ": ";
	cout << "Head to Tail=";
	for (it = set1.begin(); it != set1.end(); ++it)
		cout << (*it) << " ";
	cout << endl;
}
//put_THset 函数,从尾向头显示 multiset 容器的所有元素
void put_THset(MULTISET_INT s1, char *name) {
	MULTISET_INT::reverse_iterator i;
	cout << name << ": ";
	cout << "Tail to Head=";
	for (i = s1.rbegin(); i != s1.rend(); i++)
		cout << (*i) << " ";
	cout << endl;
}
//测试 multiset 模板
void main(void) {
	int i;
//声明 multiset 的对象和迭代器
	MULTISET_INT s1;       //容器初始尾空
	MULTISET_INT::iterator it;
//向 s1 对象中插入值
	for (i = 1; i < 20; i = i + 2) {
		s1.insert(i);
	}
//正向显示 s1 中的数据
	put_HTset(s1, "s1");
//反向显示 s1 中的数据
	put_THset(s1, "s1");
//构造含有元素的序列并显示
	MULTISET_INT s2(s1);
	put_HTset(s2, "s2");
//删除 s2 的第 2 个元素并显示
	s2.erase(++s2.begin());
	put_HTset(s2, "s2");
//向 s2 插入 8 和 9 并显示
	s2.insert(8);
	s2.insert(9);
	put_HTset(s2, "s2");
//清空 s2 的序列
	s2.clear();
	put_HTset(s2, "s2");
//按键给定的区间显示序列中的元素
	cout << "[s1.lower_bound(5),s1.upper_bound(15)] :";
	for (it = s1.lower_bound(4); it != s1.upper_bound(16); it++)
		cout << (*it) << " ";
	cout << endl;
//显示 s1 的状态信息
	cout << "s1.size():" << s1.size() << endl;
	cout << "s1.max_size():" << s1.max_size() << endl;
	cout << "s1.count(15):" << s1.count(15) << endl;
//交换两个 multiset 容器的元素并显示
	s1.swap(s2);
	put_HTset(s1, "s1");
	put_HTset(s2, "s2");
//关系运算
	s1.insert(2);
	put_HTset(s1, "s1");
	put_HTset(s2, "s2");
	cout << "s1>s2 = " << (s1 > s2) << endl;
}*/
///**
// *
// * //========================= map 容器数据===================================
// * */
/*//创建 map 的实例,整数(int)映射字符串(string)
typedef map<int, string> INT2STRING;
//测试 map 容器
void main() {
//创建 map 对象 theMap
	INT2STRING theMap;
	INT2STRING::iterator theIterator, it;
//向 theMap 容器中添入数据,数字和字符串配对
//每个元素是一个映射对
	theMap.insert(INT2STRING::value_type(0, "Zero"));
	theMap.insert(INT2STRING::value_type(2, "Two"));
	theMap.insert(INT2STRING::value_type(4, "Four"));
	theMap.insert(INT2STRING::value_type(6, "Six"));
	theMap.insert(INT2STRING::value_type(8, "Eight"));
//显示 map 容器的所有对象
	cout << "theMap.begin()--theMap.end():" << endl;
	for (theIterator = theMap.begin(); theIterator != theMap.end();
			++theIterator) {
		cout << (*theIterator).first;
		cout << "," << (*theIterator).second << " ";
	}
	cout << endl;
//测试 map 容器 key 的惟一性
	theMap.insert(INT2STRING::value_type(0, "Zero"));
	theMap.insert(INT2STRING::value_type(1, "One"));
	theMap.insert(INT2STRING::value_type(2, "Two"));
	theMap.insert(INT2STRING::value_type(3, "Three"));
	theMap.insert(INT2STRING::value_type(4, "Four"));
	theMap.insert(INT2STRING::value_type(5, "Five"));
	theMap.insert(INT2STRING::value_type(6, "Six"));
	theMap.insert(INT2STRING::value_type(7, "Seven"));
	theMap.insert(INT2STRING::value_type(8, "Eight"));
	theMap.insert(INT2STRING::value_type(9, "Nine"));
//下列语句将不能插入到 map 容器中
	theMap.insert(INT2STRING::value_type(5, "AAA"));
//显示 map 容器的所有对象
	cout << "theMap.begin()--theMap.end():" << endl;
	for (theIterator = theMap.begin(); theIterator != theMap.end();
			++theIterator) {
		cout << (*theIterator).first;
		cout << "," << (*theIterator).second << " ";
	}
	cout << endl;
//按键给定的区间显示序列中的元素
	cout << "[theMap.lower_bound(3),theMap.upper_bound(8)]  :" << endl;
	for (it = theMap.lower_bound(3); it != theMap.upper_bound(8); it++) {
		cout << (*it).first;
		cout << "," << (*it).second << " ";
	}
	cout << endl;
//显示 theMap 的状态信息
	cout << "theMap.size():" << theMap.size() << endl;
	cout << "theMap.max_size():" << theMap.max_size() << endl;
	cout << "theMap.count(15):" << theMap.count(15) << endl;
//  从键盘上输入数字,显示对应的字符串
	string theString = "";
	int index;
	for (;;) {
		cout << "Enter \"q\" to quit, or enter a Number: ";
		cin >> theString;
		if (theString == "q")
			break;
		for (index = 0; index < theString.length(); index++) {
			theIterator = theMap.find(theString[index] - '0');
			if (theIterator != theMap.end())
				cout << (*theIterator).second << " ";
			else
				cout << "[err] ";
		}
		cout << endl;
	}
}*/
///**
// *
// * //========================= multimap 容器数据===================================
// * */
/*//创建 multimap 的实例,整数(int)映射字符串(string)
typedef multimap<int, string> INT2STRING;
//测试 multimap 容器
void main() {
//创建 multimap 对象 theMap
	INT2STRING theMap;
	INT2STRING::iterator theIterator, it;
//向 theMap 容器中添入数据,数字和字符串配对
//每个元素是一个映射对
	theMap.insert(INT2STRING::value_type(90, "张卫"));
	theMap.insert(INT2STRING::value_type(85, "李华"));
	theMap.insert(INT2STRING::value_type(73, "赵明"));
	theMap.insert(INT2STRING::value_type(96, "郝名"));
//显示 multimap 容器的所有对象
	cout << "theMap.begin()--theMap.end():" << endl;
	for (theIterator = theMap.begin(); theIterator != theMap.end();
			++theIterator) {
		cout << (*theIterator).second;
		cout << "\t" << (*theIterator).first << endl;
	}
//测试 multimap 容器 key 的非惟一性
	theMap.insert(INT2STRING::value_type(90, "李朋"));
	theMap.insert(INT2STRING::value_type(85, "钱德"));
	theMap.insert(INT2STRING::value_type(93, "赵刚"));
//按成绩高低输出 multimap 容器的所有对象
	INT2STRING::reverse_iterator i;
	cout << "theMap.rbegin()--theMap.rend():" << endl;
	for (i = theMap.rbegin(); i != theMap.rend(); ++i) {
		cout << (*i).second;
		cout << "\t" << (*i).first << endl;
	}
//按关键给定的区间显示序列中的元素
	cout << "[theMap.lower_bound(80),theMap.upper_bound(90)]:" << endl;
	for (it = theMap.lower_bound(80); it != theMap.upper_bound(90); it++) {
		cout << (*it).second;
		cout << "\t" << (*it).first << endl;
	}
//显示 theMap 的状态信息
	cout << "theMap.size():" << theMap.size() << endl;
	cout << "theMap.max_size():" << theMap.max_size() << endl;
	cout << "theMap.count(90):" << theMap.count(90) << endl;
//清除 90 分以下的数据,并显示结果
	theMap.erase(theMap.lower_bound(60), theMap.upper_bound(89));
	cout << "theMap.rbegin()--theMap.rend():" << endl;
	for (i = theMap.rbegin(); i != theMap.rend(); ++i) {
		cout << (*i).second;
		cout << "\t" << (*i).first << endl;
	}
}*/
///**
// *
// * //=========================valarray 容器数据===================================
// * */
/*#define ARRAY_SIZE   3                //array size
//测试 valarray 容器
void main() {
//创建具有 3 个元素的数组 val_array
	valarray<double> val_array(ARRAY_SIZE);
//设置数组的值为 1, 4, 9
	for (int i = 0; i < ARRAY_SIZE; i++)
		val_array[i] = (i + 1) * (i + 1);
//显示 val_array 数组的大小
	cout << "Size of val_array = " << val_array.size() << endl;
//  显示 val_array 数组的值
	cout << "The  values  in  val_array  before  calling  sqrt()  and pow():"
			<< endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << val_array[i] << "     ";
	cout << endl;
//声明一个 rev_valarray 数组,其保存对数组 val_array 的取反
	valarray<double> rev_valarray(ARRAY_SIZE);
	for (int i = 0; i < ARRAY_SIZE; i++)
		rev_valarray[i] = val_array[ARRAY_SIZE - i - 1];
//显示 rev_valarray 数组的大小和元素
	cout << "Size  of  rev_valarray  =  " << rev_valarray.size() << endl;
	cout << "The values in rev_valarray:" << endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << rev_valarray[i] << "     ";
	cout << endl;
//  声明 rvalue_array 数组,其存放调用 sqrt()和 pow()函数的返回值
	valarray<double> rvalue_array;
//调用 sqrt()函数并显示结果
	rvalue_array = sqrt(val_array);
	cout << "The  result  of  rvalue_array  after  calling  sqrt():" << endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << rvalue_array[i] << "      ";
	cout << endl;
//对 val_array 数组元素计算幂函数并显示
	rvalue_array = pow(val_array, rev_valarray);
	cout << "The  result  after  calling  pow(val_array, rev_valarray):"
			<< endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << rvalue_array[i] << "      ";
	cout << endl;
//对 val_array 数组元素计算幂函数,指数均为 2.0,并显示
	rvalue_array = pow(val_array, 2.0);
	cout << "The  result  after  calling  pow(val_array,  2.0):" << endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << rvalue_array[i] << "      ";
	cout << endl;
//对 2.0 进行幂函数运算,指数均为数组 val_array 的各元素值
	rvalue_array = pow(2.0, val_array);
	cout << "The  result  after  calling  pow(2.0,  val_array):" << endl;
	for (int i = 0; i < ARRAY_SIZE; i++)
		cout << rvalue_array[i] << "      ";
	cout << endl;
//对 val_array 和 rvalue_array 求和
	cout << "val_array.sum()=" << val_array.sum() << endl;
	cout << "rvalue_array.sum()=" << rvalue_array.sum() << endl;
//求最大值并显示
	cout << "val_array.max()=" << val_array.max() << endl;
	cout << "rvalue_array.max()=" << rvalue_array.max() << endl;
}*/
///**
// *
// * //=========================STACK添加数据===================================
// * */
/*typedef stack<int> STACK_INT;
void main() {
	STACK_INT stack1;
	int i;
//判断栈是否空
	cout << "stack1.empty() returned " << (stack1.empty() ? "true" : "false")
			<< endl;
//0,2,4,6...入栈
	for (i = 0; i < 10; i = i + 2)
		stack1.push(i);
//top()函数
	if (!stack1.empty())
		cout << "stack1.top() returned " << stack1.top() << endl;
//计算栈的长度
	cout << "stack1.size(): " << stack1.size() << endl;
//改变栈顶的值  20.
	if (!stack1.empty()) {
		cout << "stack1.top()=20;" << endl;
		stack1.top() = 20;
	}
//弹出栈中所有的数据并显示
	cout << "stack1: ";
	while (!stack1.empty()) {
		cout << stack1.top() << " ";
		stack1.pop();
	}
	cout << endl;
}*/
///**
// *
// * //=========================list添加数据===================================
// * */
/*//创建一个 list 容器的实例 LISTINT,其存放 int 型数据
typedef list<int> LISTINT;
void main(void) {
//用 LISTINT 创建一个名为 listOne 的 list 对象
	LISTINT listOne;
//指定 i 为迭代器变量
	LISTINT::iterator i;
	LISTINT::reverse_iterator ir;
//从前面向 listOne 容器中添加数据
	listOne.push_front(2);
	listOne.push_front(1);
//从后面向 listOne 容器中添加数据
	listOne.push_back(3);
	listOne.push_back(4);
//从前向后显示 listOne 中的数据
	for (i = listOne.begin(); i != listOne.end(); ++i)
		cout << *i << " ";
	cout << endl;
//从后向后显示 listOne 中的数据
	for (ir = listOne.rbegin(); ir != listOne.rend(); ++ir)
		cout << *ir << " ";
	cout << endl;
//从键盘上输入数据
	for (i = listOne.begin(); i != listOne.end(); ++i) {
		cout << "listOne   :";
		cin >> (*i);
	}
//从前向后显示 listOne 中的数据
	for (i = listOne.begin(); i != listOne.end(); ++i)
		cout << *i << " ";
	cout << endl;
//bidirectional 迭代器不允许加减运算
// i=listOne.begin()+1;
}*/
///**
// *
// * //========================= accumulate 算法对普通数组及vector的处理===================
// * */
/*//利用类模板生成类实例
typedef vector<int> IntArray;
typedef list<int> LISTINT;
typedef set<int> SET_INT;
int add(int a, int b) {
	return a + b;
}
//在 main()函数中测试 accumulate 算法
void main() {
//--------------------------------------------
	//  accumulate 算法对于普通数组的计算
//---------------------------------------------
	int x[] = { 1, 3, 5, 7, 9 };
	cout << "x[]:";
	for (int i = 0; i < 5; i++)
		cout << x[i] << " ";
	cout << endl;
	cout << "accumulate(x,x+5,0)=";
	cout << accumulate(x, x + 5, 0) << endl;
	int val = 100;
	cout << "val=" << val << endl;
	cout << "accumulate(x,x+5,val)=";
	cout << accumulate(x, x + 5, val) << endl;
//--------------------------------------------
	//  accumulate 算法对于 vector 容器的计算
//---------------------------------------------
	//声明 intvector 容器和迭代器 ii
	IntArray intvector;
	IntArray::iterator ii;
//向 intvector 容器中插入元素
	for (int i = 1; i <= 5; i++) {
		intvector.push_back(i);
	};
//显示 intvector 容器中的元素值和累加结果
	cout << "intvector: " << endl;
	for (ii = intvector.begin(); ii != intvector.end(); ++ii)
		cout << (*ii) << " ";
	cout << endl;
	cout << "accumulate(intvector.begin(),intvector.end(),0)=";
	cout << accumulate(intvector.begin(), intvector.end(), 0) << endl;
//--------------------------------------------
	//  accumulate 算法对于 list 容器的计算
//---------------------------------------------
	//声明 list 容器对象和迭代器
	LISTINT::iterator iL;
	LISTINT list1;
//向 list1 容器对象中插入元素并显示
	list1.push_front(1);
	list1.push_front(3);
	list1.push_front(5);
	list1.push_back(2);
	list1.push_back(6);
//显示 list1 容器的元素值和累加结果
	cout << "list1: " << endl;
	for (iL = list1.begin(); iL != list1.end(); ++iL)
		cout << (*iL) << " ";
	cout << endl;
	cout << "accumulate(list1.begin(),list1.end(),0)=";
	cout << accumulate(list1.begin(), list1.end(), 0) << endl;
//--------------------------------------------
	//  accumulate 算法对于 set 容器的计算
//---------------------------------------------
	//声明 set 容器对象和迭代器
	SET_INT set1;
	SET_INT::iterator si;
//向 set1 容器中插入元素
	set1.insert(5);
	set1.insert(20);
	set1.insert(10);
	set1.insert(15);
	set1.insert(25);
//显示 set1 容器的元素值和累加结果
	cout << "set1: " << endl;
	for (si = set1.begin(); si != set1.end(); ++si)
		cout << (*si) << " ";
	cout << endl;
	cout << "accumulate(set1.begin(),set1.end(),0)=";
	cout << accumulate(set1.begin(), set1.end(), 0) << endl;
	cout << "accumulate(set1.begin(),set1.end(),100)=";
	cout << accumulate(set1.begin(), set1.end(), 100) << endl;
}*/
/**
 *
 * //========================= count 算法对普通数组及vector的处理========================
 * */
/*#define size 10
//产生指定范围的整数随机数
int getrand(int min, int max) {
	int m;
	m = (max - min);
	m = min + double(rand()) / RAND_MAX * m;
	return m;
}
//利用类模板生成实例
typedef vector<int> IntArray;
typedef list<int> LISTINT;
typedef set<int> SET_INT;
//在 main()函数中测试 accumulate 算法
void main() {
//--------------------------------------------
	//  count 算法对于普通数组的计算
//---------------------------------------------
	int x[size];
	cout << "x[]:";
	for (int i = 0; i < size; i++) {
		x[i] = getrand(1, 3);
		cout << x[i] << " ";
	}
	cout << endl;
	cout << "count(x,x+size,2)=";
	cout << count(x, x + size, 2) << endl;
	cout << "count(x+2,x+8,2)=";
	cout << count(x + 2, x + 8, 2) << endl;
//--------------------------------------------
//  count 算法对于 vector 容器的计算
//---------------------------------------------
//声明 intvector 容器和迭代器 ii
	IntArray intvector;
	IntArray::iterator ii;
//向 intvector 容器中插入元素
	for (int i = 1; i < size; i++) {
		intvector.push_back(getrand(2, 6));
	};
//显示 intvector 容器中的元素值和统计结果
	cout << "intvector: ";
	for (ii = intvector.begin(); ii != intvector.end(); ++ii)
		cout << (*ii) << " ";
	cout << endl;
	cout << "count(intvector.begin(),intvector.end(),4)=";
	cout << count(intvector.begin(), intvector.end(), 4) << endl;
//--------------------------------------------
//  count 算法对于 list 容器的计算
//---------------------------------------------
//声明 list 容器对象和迭代器
	LISTINT::iterator iL;
	LISTINT list1;
//向 list1 容器对象中插入元素并显示
	for (int i = 1; i < size; i++) {
		list1.push_front(getrand(3, 5));
	};
//显示 list1 容器的元素值和统计结果
	cout << "list1: ";
	for (iL = list1.begin(); iL != list1.end(); ++iL)
		cout << (*iL) << " ";
	cout << endl;
	cout << "count(list1.begin(),list1.end(),3)=";
	cout << count(list1.begin(), list1.end(), 3) << endl;
//--------------------------------------------
//  count 算法对于 set 容器的计算
//---------------------------------------------
//声明 set 容器对象和迭代器
	SET_INT set1;
	SET_INT::iterator si;
//向 set1 容器中插入元素
	for (int i = 1; i < size; i++) {
		set1.insert(getrand(1, 10));
	};
//显示 set1 容器的元素值和统计结果
	cout << "set1: ";
	for (si = set1.begin(); si != set1.end(); ++si)
		cout << (*si) << " ";
	cout << endl;
	cout << "count(set1.begin(),set1.end(),5)=";
	cout << count(set1.begin(), set1.end(), 5) << endl;
}*/
///**
// *
// * //========================= count_if 算法对普通数组及vector的处理======================
// * */
/*//如果字符串以'S'开头,则返回 true
int MatchFirstChar(const string& str) {
	string s("S");
	return s == str.substr(0, 1);
}
//测试 count_if 算法
int main() {
	const int VECTOR_SIZE = 8;
//生成成员类型为 strings 的 vector 容器类
	typedef vector<string> StringVector;
//定义迭代器类型
	typedef StringVector::iterator StringVectorIt;
//声明 vector 容器的对象
	StringVector NamesVect(VECTOR_SIZE);
//声明迭代器
	StringVectorIt start, end, it;
	int result = 0;    //  存放统计数据
//初始化 vector 容器 NamesVect
	NamesVect[0] = "She";
	NamesVect[1] = "Sells";
	NamesVect[2] = "Sea";
	NamesVect[3] = "Shells";
	NamesVect[4] = "by";
	NamesVect[5] = "the";
	NamesVect[6] = "Sea";
	NamesVect[7] = "Shore";
//设置容器的起始位置和终止位置
	start = NamesVect.begin();
	end = NamesVect.end();
//显示 NamesVect 容器的元素
	cout << "NamesVect: ";
	for (it = start; it != end; it++)
		cout << *it << " ";
	cout << endl;
//统计并显示 NamesVect 容器的所有元素中以'S'字符开头的字符串
	result = count_if(start, end, MatchFirstChar);
	cout << "Number of elements that start with letter \"S\" = " << result
			<< endl;
//显示 NamesVect 容器[1,6]之间的元素
	cout << "NamesVect[1]--NamesVect[6]: ";
	for (it = &NamesVect[1]; it != &NamesVect[7]; it++)
		cout << *it << " ";
	cout << endl;
//统计并显示 NamesVect 容器的所有元素中以'S'字符开头的字符串
	result = count_if(&NamesVect[1], &NamesVect[7], MatchFirstChar);
	cout << "Number of elements that start with letter \"S\" = " << result
			<< endl;
	return 1;
}*/

///**
// *
// * //========================= fill 和 fill_n算法对普通数组及vector的处理====================
// * */
/*//利用类模板生成实例
typedef vector<int> IntArray;
//显示数组
void put_array(int x[], int size) {
	for (int i = 0; i < size; i++)
		cout << x[i] << " ";
	cout << endl;
}
//显示 vector 容器中的元素
void put_vector(IntArray v) {
	IntArray::iterator theIterator;
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
	cout << endl;
}
//在 main()函数中测试 fill 和 fill_n 算法
int main() {
//--------------------------------------------
	//  fill 和 fill_n 算法对普通数组的计算
//---------------------------------------------
	int x[] = { 1, 3, 5, 7, 9 };
	cout << "x[]: ";
	put_array(x, 5);
//填数处理
	fill(x + 1, x + 3, 2);
	cout << "fill(x+1,x+3,2): " << endl;
	put_array(x, 5);
	fill_n(x, 3, 8);
	cout << "fill_n(x,3,8): " << endl;
	put_array(x, 5);
//--------------------------------------------
//  fill 和 fill_n 算法对于 vector 容器的计算
//---------------------------------------------
//声明 intvector 容器和迭代器 ii
	IntArray intvector;
//向 intvector 容器中插入元素
	for (int i = 1; i <= 10; i++) {
		intvector.push_back(i);
	};
//显示 intvector 容器中的元素值和统计结果
	cout << "intvector: " << endl;
	put_vector(intvector);
//填数处理
	fill(intvector.begin(), intvector.begin() + 3, 2);
	cout << "fill(intvector.begin(),intvector.begin()+3,2): " << endl;
	put_vector(intvector);
	fill_n(&intvector[5], 3, 8);
	cout << "fill_n(&intvector[5],3,8): " << endl;
	put_vector(intvector);
	return 0;
}*/
/**
 *
 * //========================= find()算法对普通数组及vector的处理=================
 * */
/*#define ARRAY_SIZE 10
//利用类模板生成实例
typedef vector<int> IntArray;
//显示数组
void put_array(int x[], int size) {
	for (int i = 0; i < size; i++)
		cout << x[i] << " ";
	cout << endl;
}
//显示 vector 容器中的元素
void put_vector(IntArray v) {
	IntArray::iterator theIterator;
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
	cout << endl;
}
//在 main()函数中测试 find()算法
int main() {
	int i, value, *p;
	//--------------------------------------------
	//  find()算法对于普通数组的处理
	//---------------------------------------------
	int x[ARRAY_SIZE] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
	cout << "x[]: ";
	put_array(x, ARRAY_SIZE);
	//find()算法查找,并显示查找结果
	for (i = 0; i <= 2; i++) {
		cout << "value=";
		cin >> value;
		p = find(x, x + ARRAY_SIZE, value);
		if (p != x + ARRAY_SIZE) {   //查到
			cout << "First element that matches " << value;
			cout << " is at location " << p - x << endl;
		} else {            //未查到
			cout << "The sequence does not contain any elements";
			cout << " with value " << value << endl;
		}
	}
	//--------------------------------------------
	//  find()算法对于 vector 容器的处理
	//---------------------------------------------
	//声明 intvector 容器对象
	IntArray intvector;
	//向 intvector 容器中插入元素
	for (i = 1; i <= 10; i++) {
		intvector.push_back(i);
	};
	//显示 intvector 容器中的元素值
	cout << "intvector: ";
	put_vector(intvector);
	//find()算法查找,并显示查找结果
	IntArray::iterator pos;
	for (i = 0; i <= 2; i++) {
		cout << "value=";
		cin >> value;
		pos = find(intvector.begin(), intvector.end(), value);
		if (pos != intvector.end()) {   //查到
			cout << "First element that matches " << value;
			cout << " is at location " << pos - intvector.begin() << endl;
		} else {            //未查到
			cout << "The  sequence  does  not  contain  any elements";
			cout << " with value " << value << endl;
		}
	}
	return 1;
}*/
/**
 *
 * //========================= find_end()算法对普通数组的处理====================
 * */
/*#define ARRAY_SIZE 10
//利用类模板生成实例
typedef vector<int> IntArray;
//显示数组
void put_array(int x[], int size) {
	for (int i = 0; i < size; i++)
		cout << x[i] << " ";
}
//显示 vector 容器中的元素
void put_vector(IntArray v) {
	IntArray::iterator theIterator;
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
}
//在 main()函数中测试 find()_end()算法
int main() {
//--------------------------------------------
	//  find_end()算法对普通数组的处理
//---------------------------------------------
	int x[ARRAY_SIZE] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
	cout << "x[]: ";
	put_array(x, ARRAY_SIZE);
	cout << endl;
	int y[] = { 5, 7, 9 };
	cout << "y[]: ";
	put_array(y, 3);
	cout << endl;
// find_end()算法查找,并显示查找结果
	int *p = find_end(x, x + ARRAY_SIZE, &y[0], &y[2]);
	if (p != x + ARRAY_SIZE) {   //查到
		cout << "The first element that matches :";
		put_array(y, 3);
		cout << " is at location in x" << p - x << endl;
	} else {            //未查到
		cout << "The sequence does not contain any elements";
		cout << " with value ";
		put_array(&x[3], 3);
	}
//--------------------------------------------
	//  find_end()算法对 vector 容器的处理
//---------------------------------------------
	//声明 intvector 容器对象
	IntArray intvector;
//向 intvector 容器中插入元素
	for (int i = 1; i <= 10; i++) {
		intvector.push_back(i);
	};
	//显示 intvector 容器中的元素值
	cout << "intvector: ";
	put_vector(intvector);
	cout << endl;
	IntArray temp;
	temp.push_back(5);
	temp.push_back(6);
	temp.push_back(7);
	cout << "temp: ";
	put_vector(temp);
	cout << endl;
	// find_end()算法查找,并显示查找结果
	IntArray::iterator pos;
	pos = find_end(intvector.begin(), intvector.end(), temp.begin(),
			temp.end());
	if (pos != intvector.end()) {   //查到
		cout << "The first element that matches ";
		put_vector(temp);
		cout << "  is  at  location  in  intvector  " << pos - intvector.begin()
				<< endl;
	} else {            //未查到
		cout << "The sequence does not contain any elements";
		cout << " with value ";
		put_vector(temp);
		cout << endl;
	}
	return 1;
}*/
/**
 *
 * //===============利用 generate_n 算法用 Fibonacci  数填充 vector 容器==================
 * */
/*//返回一个 Fibonacci 数,其由 generate_n()算法调用
int Fibonacci1(void) {
	static int r;
	static int f1 = 0;
	static int f2 = 1;
	r = f1 + f2;
	f1 = f2;
	f2 = r;
	return f1;
}
//返回一个 Fibonacci 数,其由 generate()算法调用
int Fibonacci2(void) {
	static int r;
	static int f1 = 0;
	static int f2 = 1;
	r = f1 + f2;
	f1 = f2;
	f2 = r;
	return f1;
}
//定义整型数的 vector 容器类
typedef vector<int> IntVector;
//显示 vector 容器中的元素
void put_vector(IntVector v, char *name) {
	IntVector::iterator theIterator;
	cout << name << ":" << endl;
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
	cout << endl;
}
//测试 generate()和 generate_n()算法
int main() {
	const int VECTOR_SIZE = 15;
//定义迭代器类
	typedef IntVector::iterator IntVectorIt;
//声明 vector 容器对象
	IntVector Numbers1(VECTOR_SIZE), Numbers2(VECTOR_SIZE);
	int i;
//初始化 vector 容器对象
	for (i = 0; i < VECTOR_SIZE; i++)
		Numbers1[i] = i;
//显示 vector 容器对象的元素
	cout << "Before calling generate_n:" << endl;
	put_vector(Numbers1, "Numbers1");
//利用 generate_n 算法用 Fibonacci  数填充 vector 容器
	generate_n(Numbers1.begin(), VECTOR_SIZE, Fibonacci1);
//显示 vector 容器对象的元素
	cout << "After calling generate_n:" << endl;
	put_vector(Numbers1, "Numbers1");
//利用 generate 算法用 Fibonacci  数填充 vector 容器
	generate(Numbers2.begin(), Numbers2.end(), Fibonacci2);
//显示 vector 容器对象的元素
	cout << "After calling generate:" << endl;
	put_vector(Numbers2, "Numbers2");
	return 1;
}*/
/**
 *
 * //================reverse()和 reverse_copy()算法对 vector 容器的处理======================
 * */
/*//利用类模板生成实例
typedef vector<int> IntArray;
//显示数组
void put_array(int x[], int size) {
	for (int i = 0; i < size; i++)
		cout << x[i] << " ";
	cout << endl;
}
//显示 vector 容器中的元素
void put_vector(IntArray v) {
	IntArray::iterator theIterator;
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
	cout << endl;
}
//在 main()函数中测试 reverse()和 reverse_copy()算法
int main() {
//--------------------------------------------
//  reverse()和 reverse_copy()算法对普通数组处理
//---------------------------------------------
	int x[] = { 1, 3, 5, 7, 9 };
	cout << "x[]:";
	put_array(x, 5);
//reverse()反转 x 数组并显示
	reverse(x, x + 5);
	cout << "x[]:";
	put_array(x, 5);
	int y[] = { 2, 4, 6, 8, 10 };
	cout << "y[]:";
	put_array(y, 5);
//reverse_copy()反转 y 数组的部分元素并拷贝到 x 数组第 2个元素位置
	reverse_copy(y + 1, y + 3, x + 1);
	cout << "x[]:";
	put_array(x, 5);
	cout << "y[]:";
	put_array(y, 5);
//--------------------------------------------
	//  reverse()和 reverse_copy()算法对 vector 容器的处理
//---------------------------------------------
	//声明 intvector 容器和迭代器 ii
	IntArray intvector;
//向 intvector 容器中插入元素
	for (int i = 1; i <= 10; i++) {
		intvector.push_back(i);
	};
//显示 intvector 容器中的元素值
	cout << "intvector: " << endl;
	put_vector(intvector);
//reverse()对于 vector 容器的处理
	reverse(intvector.begin(), intvector.end());
	cout << "intvector: " << endl;
	put_vector(intvector);
// reverse_copy 对于 vector 容器的处理
	IntArray temp(5);
	reverse_copy(intvector.begin() + 2, intvector.begin() + 7, temp.begin());
	cout << "temp: " << endl;
	put_vector(temp);
	return 1;
}*/
/**
 *
 * //=================sort()和 partial_sort()算法对普通数组处理=====================
 *
 * */
/*#define ARRAY_SIZE 15
//定义整型数的 vector 容器类
typedef vector<int> IntVector;
//显示数组
void put_array(int x[], int size) {
	for (int i = 0; i < size; i++)
		cout << x[i] << " ";
	cout << endl;
}
//显示 vector 容器中的元素
void put_vector(IntVector v, char *name) {
	IntVector::iterator theIterator;
	cout << name << ": ";
	for (theIterator = v.begin(); theIterator != v.end(); ++theIterator) {
		cout << (*theIterator) << " ";
	}
	cout << endl;
}
//产生指定范围的整数随机数
int getrand(int min, int max) {
	int m;
	m = (max - min);
	m = int(min + double(rand()) / RAND_MAX * m);
	return m;
}
//在 main()函数中测试 sort()和 partial_sort()算法
int main() {
	int i;
//--------------------------------------------
  sort()和 partial_sort()算法对普通数组处理
//---------------------------------------------
sort()算法处理数组,并显示
	int x[ARRAY_SIZE];
	for (i = 0; i < ARRAY_SIZE; i++) {
		x[i] = getrand(1, 20);
	}
	cout << "x[]:";
	put_array(x, ARRAY_SIZE);
	sort(x, x + ARRAY_SIZE);
	cout << "sort(x,x+ARRAY_SIZE):" << endl;
	put_array(x, ARRAY_SIZE);
//partial_sort()算法对于数组进行处理
	int y[ARRAY_SIZE];
	for (i = 0; i < ARRAY_SIZE; i++) {
		y[i] = getrand(1, 30);
	}
	cout << "y[]:";
	put_array(y, ARRAY_SIZE);
	partial_sort(y + 2, y + 7, y + ARRAY_SIZE);
	cout << "partial_sort(y+2,y+7,y+ARRAY_SIZE):" << endl;
	put_array(y, ARRAY_SIZE);
//--------------------------------------------
//  sort()和 partial_sort()算法对 vector 容器的处理
//---------------------------------------------
	IntVector Numbers1, Numbers2;
	for (i = 0; i < 15; i++) {
		Numbers1.push_back(getrand(1, 30));
		Numbers2.push_back(getrand(1, 30));
	}
	put_vector(Numbers1, "Numbers1");
	put_vector(Numbers2, "Numbers2");
//sort()算法处理并显示
	sort(Numbers1.begin(), Numbers1.end());
	cout << "After call sort():" << endl;
	put_vector(Numbers1, "Numbers1");
//partial_sort()算法处理并显示
	partial_sort(Numbers2.begin() + 2, Numbers2.begin() + 7, Numbers2.end());
	cout << "After call partial_sort():" << endl;
	put_vector(Numbers2, "Numbers2");
	return 1;
}*/
//=================end=end=end=end=end===========================================================

C++模板类的使用:

//============================================================================
// Name        : ACCD.cpp
// Author      : zhubin
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<string.h>
using namespace std;
/**
 *
 * =================C++ 模板类的使用==========================
 * */
/*//声明函数模板的原型语句
template <class T> void swap(T *x, T *y);
//定义一个结构类型
struct student {
int n;
char name[20];
float grade;
};
//在 main()函数中测试 swap()函数模板
void main(void)
{
//交换两个 int 型变量中的数据
int m=3,n=5;
cout<<"m="<<m<<"   n="<<n<<endl;
swap(&m,&n);
cout<<"m="<<m<<"   n="<<n<<endl;
cout<<"-------------------"<<endl;
//交换两个 double 型变量中的数据
double x=3.5,y=5.7;
cout<<"x="<<x<<"   y="<<y<<endl;
swap(&x,&y);
cout<<"x="<<x<<"   y="<<y<<endl;
cout<<"-------------------"<<endl;
//交换两个 char 型变量中的数据
char c1='A',c2='a';
cout<<"c1="<<c1<<"   c2="<<c2<<endl;
swap(&c1,&c2);
cout<<"c1="<<c1<<"   c2="<<c2<<endl;
cout<<"-------------------"<<endl;
//交换两个结构变量中的数据
student s1={1001,"ZhangHua",90};
student s2={1011,"LiWei",95.5};
cout<<"s1:   ";
cout<<s1.n<<"   "<<s1.name<<"   "<<s1.grade<<endl;
cout<<"s2:   ";
cout<<s2.n<<"   "<<s2.name<<"   "<<s2.grade<<endl;
swap(&s1,&s2);
cout<<"swap(s1,s2):"<<endl;
cout<<"s1:   ";
cout<<s1.n<<"   "<<s1.name<<"   "<<s1.grade<<endl;
cout<<"s2:   ";
cout<<s2.n<<"   "<<s2.name<<"   "<<s2.grade<<endl;
}*/
/**
 *
 * =================C++ 模板类的使用==========================
 * */
/*//定义名为 swap 的函数模板用于交换两个变量中的数据
template <class T> void swap(T *x, T *y)
{
T temp;
temp=*x;
*x=*y;
*y=temp;
}
//定义输入函数模板
template <class T> void input(char *str,T &x) {
cout<<str<<"=";
cin>>x;
}
//定义输出函数模板
template <class T> void output(char *str,T x) {
cout<<str<<"="<<x<<endl;
}
//在 main()函数中测试输入输出函数模板
void main(void)
{
//输入输出 int 型数据
int a,b;
input("a",a);
output("a",a);
b=3*a;
output("3*a",b);
output("a+b",a+b);
cout<<"-------------------"<<endl;
//输入输出 double 型数据
double x,y;
input("x",x);
output("x",x);
y=2*x;
output("y",y);
cout<<"-------------------"<<endl;
//输入输出 char 型数据
char c1;
input("c1",c1);
output("c1+2",char(c1+2));
cout<<"-------------------"<<endl;
//输入输出字符串数据
char string[80];
input("string",string);
output("string",string);
}*/
/**
 *
 * =================C++ 数组模板类的使用==========================
 * */
/*//显示数组的函数模板
template<class T> void arr_put(T arr[], int size) {
	for (int i = 0; i < size; i++)
		cout << arr[i] << " ";
	cout << endl;
}
//选择法对数组排序的函数模板
template<class T> void sort(T arr[], int size) {
	T temp;
	int i, j;
	for (i = 0; i < size - 1; i++)
		for (j = i + 1; j < size; j++)
			if (arr[i] > arr[j]) {
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
}
//二分查找法的函数模板
template<class T> int binary_search(T array[], T value, int size) {
	int found = 0;
	int high = size, low = 0, mid;
	mid = (high + low) / 2;
	cout << "Looking for " << value << endl;
	while ((!found) && (high >= low)) {
		if (value == array[mid])
			found = 1;
		else if (value < array[mid])
			high = mid - 1;
		else
			low = mid + 1;
		mid = (high + low) / 2;
	}
	return ((found) ? mid : -1);
}
//main()函数中使用处理数组的函数模板
void main(void) {
//处理 int 型数组
	int array[10] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
//显示数组初值
	arr_put(array, 10);
//对数组排序并显示
	sort(array, 10);
	arr_put(array, 10);
//查找数组
	cout << "Result  of  search:  " << binary_search(array, 3, 10) << endl;
	cout << "Result  of  search:  " << binary_search(array, 2, 10) << endl;
	cout << "Result  of  search:  " << binary_search(array, 9, 10) << endl;
	cout << "Result  of  search:  " << binary_search(array, 5, 10) << endl;
	cout << "------------------------------" << endl;
//处理字符串型数组
	char ch1, str[] = "happy";
	int size = strlen(str);
//显示数组初值
	arr_put(str, size);
//对数组排序并显示
	sort(str, size);
	arr_put(str, size);
//查找数组
	cout << "Input a char:";
	cin >> ch1;
	cout << "Result  of  search:  " << binary_search(str, ch1, size) << endl;
}*/
/**
 *
 * =================C++ 模板类的使用==========================
 * */
/*//定义名为 ex_class 的类模板
 template<class T> class ex_class {
 T value;
 public:
 ex_class(T v) {
 value = v;
 }
 void set_value(T v) {
 value = v;
 }
 T get_value(void) {
 return value;
 }
 };
 //main()函数中测试 ex_class 类模板
 void main(void) {
 //测试 int 类型数据
 ex_class<int> a(5), b(10);
 cout << "a.value:" << a.get_value() << endl;
 cout << "b.value:" << b.get_value() << endl;
 //测试 char 类型数据
 ex_class<char> ch('A');
 cout << "ch.value:" << ch.get_value() << endl;
 ch.set_value('a');
 cout << "ch.value:" << ch.get_value() << endl;
 //测试 double 类型数据
 ex_class<double> x(5.5);
 cout << "x.value:" << x.get_value() << endl;
 x.set_value(7.5);
 cout << "x.value:" << x.get_value() << endl;
 }*/
/**
 *
 * =================C++ stck模板类的使用==========================
 * */
/*//定义栈的尺寸
 const int SIZE = 100;
 //定义处理栈的类模板接口
 template<class T> class stack {
 T stck[SIZE];
 int tos;
 public:
 stack(void) {
 tos = 0;
 cout << "Stack Initialized." << endl;
 }
 ~stack(void) {
 cout << "Stack Destroyed." << endl;
 }
 void push(T);
 T pop(void);
 };
 //定义栈的成员函数
 template<class T> void stack<T>::push(T i) {
 if (tos == SIZE) {
 cout << "Stack is full." << endl;
 return;
 }
 stck[tos++] = i;
 }
 template<class T> T stack<T>::pop(void) {
 if (tos == 0) {
 cout << "Stack underflow." << endl;
 return 0;
 }
 return stck[--tos];
 }
 //main()函数中测试 stack 类模板
 void main(void) {
 //处理 int 类型数据的栈
 cout << "stack<int> a :" << endl;
 stack<int> a;
 a.push(1);
 a.push(2);
 cout << a.pop() << " ";
 cout << a.pop() << endl;
 //处理 double 类型数据的栈
 cout << "stack<double> b :" << endl;
 stack<double> b;
 b.push(99.3);
 b.push(-12.23);
 cout << b.pop() << " ";
 cout << b.pop() << endl;
 //处理 char 类型数据的栈
 cout << "stack<char> c :" << endl;
 stack<char> c;
 for (int i = 0; i < 10; i++)
 c.push((char) 'A' + i);
 for (int i = 0; i < 10; i++)
 cout << c.pop();
 cout << endl;
 }*/
/**
 *
 * =================C++模板类的使用==========================
 * */
/*

 //定义名为 ex_class 的类模板
 template<class T1, class T2> class ex_class {
 T1 value1;
 T2 value2;
 public:
 ex_class(T1 v1, T2 v2) {
 value1 = v1;
 value2 = v2;
 }
 void set_value(T1 v1, T2 v2) {
 value1 = v1;
 value2 = v2;
 }
 void put_value(void) {
 cout << "valu1=" << value1 << endl;
 cout << "valu2=" << value2 << endl;
 }
 };
 //main()函数中测试 ex_class 类模板
 void main(void) {
 //测试 int 和 double 类型数据
 ex_class<int, double> a(5, 1.5);
 cout << "ex_class <int,double> a:" << endl;
 a.put_value();
 a.set_value(100, 3.14);
 a.put_value();
 //测试 double 和 int 类型数据
 ex_class<double, int> b(0.5, 5);
 cout << "ex_class <double,int> b:" << endl;
 b.put_value();
 b.set_value(1.732, 100);
 b.put_value();
 //测试 char 和 int 类型数据
 ex_class<char, int> c('a', 5);
 cout << "ex_class <char,int> c:" << endl;
 c.put_value();
 c.set_value('B', 100);
 c.put_value();
 //测试 int 和 int 类型数据
 ex_class<int, int> d(5, 10);
 cout << "ex_class <int,int> d:" << endl;
 d.put_value();
 d.set_value(100, 200);
 d.put_value();
 }
 */




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值