第二十三模板 18标准模板库 简单

//第二十三模板 18标准模板库
//1 容器 容器是包含其他对像的对像,标准C++ 库中提供了一系列的容器类
//可以分为两种类型,顺序和关联类型,顺序容器可提供对自身元素的顺序或者随机访问,关联容器则过滤掉了一些元素,只按关键值访问有关元素

//2 顺序容器
//标准C++库提供了三种顺序容器,分别为vector list 和deque

//2.1 向量容器
/*#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int num=2;
int main()
{
    vector<double>price(num);
	vector<string>book(num);
	cout<<"开始录入"<<num<<"本书的数据"<<endl;
	for(int i=0; i<num; i++)
	{
	    cout<<"请输入第"<<i+1<<"本书的书名:";
		getline(cin,book[i]);
		cout<<"请输入价格:";
		cin>>price[i];
		cin.ignore();
	}
	for(int i=0; i<num; i++)
	{
	     cout<<"第"<<i+1<<"本书的书名:";
		 cout<<book[i]<<"\t"<<"价格:"<<price[i]<<endl;
	}
	cout<<"max_size:"<<book.max_size()<<endl;
	return 0;
}*/

/*
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class people
{
public:
	people();
	people(const string&name, const int age);
	people(const people&r);
	~people();
	void SetName(const string&name);
	string GetName()const;
	void SetAge(const int age);
	int GetAge()const;
	people&operator=(const people&r);
private:
	string theName;
	int theAge;
};

people::people():theName("默认创建的新人"),theAge(52){}

people::people(const string&name, const int age):theName(name),theAge(age){}

people::people(const people&r):theName(r.GetName()),theAge(r.GetAge())
{
   cout<<"复制构造函数"<<endl;
}
people::~people(){cout<<"析构函数执行"<<endl;}

void people::SetName(const string&r)
{
    theName = r;
}

string people::GetName()const
{
	return theName;
}

void people::SetAge(const int age)
{
    theAge = age;
}

int people::GetAge()const
{
    return theAge;
}

people&people::operator=(const people&r)
{
    cout<<"operator=函数执行";
	theName = r.GetName();
	theAge = r.GetAge();
	return *this;
}

ostream &operator<<(ostream&out, const people&r)
{
	out<<r.GetName()<<"的年龄是:"<<r.GetAge()<<endl;
	return out;
}

template<class T>
void show(const vector<T>&v); //定认一个容器参数为一个模板值
typedef vector<people>man;    //man的在这里定义一个容器,里面放的是people
int main()
{
     people Jack;
	 people Mary("Mark",24);
	 people Tom("Tom",18);
	 people Jesson("Jesson",29);
	 man non;
	 cout<<"non:"<<endl;
	 show(non);

	 man manyMan(3);
	 cout<<"manyMan(3):"<<endl;
	 show(manyMan);
	 manyMan[0] = Mary;
	 manyMan[1] = Tom;
	 manyMan[2] = Jesson;
	 cout<<"为容器manyMan(3)分配个人后:"<<endl;
	 show(manyMan);

	 people Ketty("Ketty",58);
	 manyMan.push_back(Ketty);
	 manyMan.push_back(Jack);
	 cout<<"manyMan()增加二个人后:"<<endl;
	 show(manyMan);
	 manyMan[0].SetName("Rose");
	 manyMan[0].SetAge(16);
	 cout<<"设置manyMan[0]后:"<<endl;
	 system("pause");
	 return 0;
}

template<class T>
void show(const vector<T> &v)
{
	cout<<"max_size()="<<v.max_size();
	cout<<"\tsize()="<<v.size();
	cout<<"\t capacity()="<<v.capacity();
	cout<<"\t"<<(v.empty()?"空":"不为空");
	cout<<endl;
	for(int i=0; i<v.size(); ++i)
	{
	    cout<<v[i]<<endl;
	}
	cout<<endl;	
}*/
/*
Constructors() 构造函数
Operators()    对vector进行赋值或比较
assign()       对vector中的元素赋值
at()           返回指定位置的元素
back()         返回最末一个元素
begin()        返回第一个元素的迭代器
capacity()     返回vector所能容纳的元素数量(在不重新分配内存的情况下)
clear()        清空所有元素
empty()        判断Vector是否为空(返回true时为空)
end()          返因最末元素的迭代器(实指向最末元素的下一个位置)
erase()        删除指定元素
front()        返回第一个元素
get_allocator()   返回vector的内存分配器
insert()        插入元素的vctor中
pop_back()      移除最后一元素
push_back()     在vector最后添加一个元素
rbegin()        返回vector尾部的逆迭代器
rend()          返回vector起始的逆迭代器
reserve()       设置vector最小的元素容纳数量
resize()        改变Vector元素的数量大小
size()          返回vector元素数量的大小
swap()          交换两个Vector
*/

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2711695.html