- 0
- 0
- 约2.67万字
- 约 79页
- 2017-04-23 发布于四川
- 举报
第十一章节标准模板类库STL
;;;11.2.1泛型程序设计;命名空间(Namespace);命名空间(Namespace);容器;容器的接口;适配器;迭代器;算法;函数对象;;;;向量应用举例(一); vector int nMyVector1, //整型向量
nMyVector2;
//该向量类型的迭代子
vector int ::iterator nItBegin,nItEnd;
//该向量类型的输出流迭代子
ostreamiterator int nOutput(cout,″ ″);
//对向量类型对象中的数据进行顺序赋值
for(int i=1;i=10;i++)
{
nMyVector1.pushback(i);
}
//将该向量类型对象中的数据输出到标准输出流,显示出来。
copy(nMyVector1.begin(),nMyVector1.end(),nOutput);
cout endl;
//该向量类型的迭代子被赋值
nItBegin =nMyVector1.begin();
nItEnd =nMyVector1.end(); ; //用insert函数实现两个向量类型对象成员数据的复制
nMyVector2.insert(nMyVector2.begin(),nMyVector1.begin(),nMyVector1.end());
for(i=0;inMyVector2.size();i++)
{
coutnMyVector2[i]″ ″;
}
coutendl;
//用迭代子输出该向量类型对象的数据
while(nItBegin!=nItEnd)
{
cout*nItBegin″ ″;
nItBegin++;
}
cout endl;
//用标准算法得到整型向量对象中所有数据的和
cout ″The sum of nMyVector1[i] is: ″
accumulate(nMyVector1.begin(),nMyVector1.end(),0.0f)
endl;
nItBegin=nMyVector1.begin();
nItEnd =nMyVector1.end(); ; //用一个已经存在的向量对象初始化另一个向量对象
vector int nMyVector3(nItBegin,nItEnd);
vector int nMyVector4(nMyVector3);
//输出两个新的向量对象
copy(nMyVector3.begin(),nMyVector3.end(),nOutput);
cout endl;
copy(nMyVector4.begin(),nMyVector4.end(),nOutput);
cout endl;
};向量应用举例(二); int x;
int y;
public: //声明对外接口
Point(int xx=0,int yy=0) //带默认形参值的构造函数
{
x=xx;y=yy;
}
void display() //显示函数
{
cout″Point″″(″x″,″y″)″″ ″;
}
int getx() const {return x;}
int gety() const {return y;}
bool operator ==(const Point point2) const;//用户自定义类型必须重载″==″和
// ″″运算符,因为STL中的模板类都要支持关系运算如″=″、″=″。
bool operator (const Point point2) const;
};
bool Point::operator ==(const Point point2) const
{
if(
您可能关注的文档
- 第九章节进出口合同的履行.ppt
- 第九章节建设工程质量与安全法规.ppt
- 第九章节电子商务及其应用.ppt
- 第九章节重量分析法.ppt
- 第二十章节人人微博站.ppt
- 第九课西欧、日本经济的发展.ppt
- 第二十八章节Oracle服务器配置和管理.ppt
- 第三课政府的职能.公开课.ppt
- 第二单元第一章节第一节练习使用显微镜.ppt
- 第二十章节国际经济部门的作用.ppt
- 2026年及未来5年内中国液体氩气行业投资前景及策略咨询研究报告.docx
- 2026年及未来5年内中国黄铜合页行业投资前景及策略咨询研究报告.docx
- 2026年及未来5年内中国竹工艺家具行业投资前景及策略咨询研究报告.docx
- 2025年中国微机数显自动分析仪市场调查研究报告.docx
- 2026年及未来5年内中国微尘白色粉笔行业投资前景及策略咨询研究报告.docx
- 2025年中国微电脑型压胶机市场调查研究报告.docx
- 2026年及未来5年内中国数字化等功游泳训练测试系统行业投资前景及策略咨询研究报告.docx
- 2025年中国圆形花瓶市场调查研究报告.docx
- 2026年及未来5年内中国植物纤维静淀过滤器行业投资前景及策略咨询研究报告.docx
- 2025年中国超音波手套机市场调查研究报告.docx
原创力文档

文档评论(0)