STL之vector
来源:互联网 发布:中国软件评测 编辑:程序博客网 时间:2024/04/30 12:44
/*
#include <iostream>
#include <string>
#include <vector>
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;
}
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<<"复制构造函数执行...\n";
}
people::~people(){cout<<"析构函数执行\n";}
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=函数执行...\n";
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;
int main()
{
people Jack;
people Mary("Mary",24);
people Tom("Tom",18);
people Jesson("Jesson",29);
man non;
cout<<"non:\n";
show(non);
man manyMan(3);
cout<<"manyMan(3):\n";
show(manyMan);
manyMan[0]=Mary;
manyMan[1]=Tom;
manyMan[2]=Jesson;
cout<<"为容器manyMan(3)分配个人后:\n";
show(manyMan);
people Ketty("Ketty",58);manyMan.push_back(Ketty);manyMan.push_back(Jack);
cout<<"manyMan()增加2个人后:\n";
show(manyMan);
manyMan[0].SetName("Rose");
manyMan[0].SetAge(16);
cout<<"设置manyMan[0]后:\n";
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<<"\n";
for (int i=0;i<v.size();++i)
{
cout<<v[i]<<"\n";
}
cout<<endl;
}
- 【STL】STL容器之vector
- STL之vector模板
- STL之vector
- STL之vector
- stl之vector使用
- STL之vector详解
- STL之vector
- STL之vector使用
- STL之vector
- STL容器之vector
- STL容器之 vector
- STL之vector容器
- STL容器之vector
- STL之向量(vector)
- STL之Vector
- C++ STL之vector
- STL之Vector用法
- stl库之vector
- 用GDB调试程序 - Ubuntu中文
- Linux/UNIX 命令行大全完整版
- 【CPU 与编译器版】资源集合
- [ThinkPad] PWMTR32V.Dll的问题
- 毕业设计——人脸检测——001 图片导入MATLAB
- STL之vector
- 自编JQuery插件第十二个:水平滚动图片
- 博客检讨
- const 总 结
- [读书笔记]ASCII字符的兼容性
- UVa Problem 10215 The Largest/Smallest Box... (最大/最小的盒子)
- 2011年下半年11月份系统架构设计师上午试题答案之一
- 生成符合flexigrid的json格式的java代码
- Oracle Pro*c 中sqlca以及oraca的定义和说明