C++ 学习笔记8
来源:互联网 发布:刷机恢复数据 编辑:程序博客网 时间:2024/05/17 01:57
Write a comparison object that uses the square of an objects value for comparison. Therefore, a large negative number is greater than a small positive number using this comparison object. Generate in a vector the integers -100 to +100 and use an STL sort with this comparison object. Print out the result.
编写一个比较对象,该对象使用对象的平方值进行比较。因此,使用该比较对象后,一个大的负数比一个小的正数要大。在vector中产生-100~+100的整数,并使用这个排序对象调用STL的排序算法,打印最终结果。
//本程序在VCSP6下编译通过
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class compare
{
public:
bool operator()(const int &x,const int &y)
{
return (x*x)>(y*y);
};
};
int main()
{
vector<int> v;
compare cmp;
for(int i=-100;i<101;i++)
v.push_back(i);
cout<<"下面打印未排序前v中的元素:"<<endl;
for(i=0;i<v.size();i++)
cout<<v[i]<<'\t';
cout<<endl;
sort(v.begin(),v.end(),cmp);
cout<<"下面打印排序后的元素:"<<endl;
for(i=0;i<v.size();i++)
cout<<v[i]<<'\t';
return 0;
}
- C语言学习笔记(8)
- C语言学习笔记8
- C语言学习笔记8
- C++/C学习笔记--(8)
- C++/C学习笔记
- C语言学习笔记8---预处理
- C/C++学习笔记:基础知识8
- 学习笔记8-C语言文件
- C语言学习笔记8-函数
- 【C/C++】STL学习笔记
- C/C学习笔记/导言
- C/C学习笔记/结构
- 【c/c++】学习笔记1
- C学习笔记1
- TPC-C学习笔记
- c语言学习笔记
- C语言学习笔记
- C语言学习笔记
- HGE:Tutorials:Random Numbers
- C++ 学习笔记7
- Android 对话框(Dialog)大全 建立你自己的对话框
- JNI编程(二) —— 让C++和Java相互调用(1)
- 利用RunWithElevatedPrivileges模拟管理员权限时慎用SPContext
- C++ 学习笔记8
- H3C的路由器配置命令一部分
- 线程 synchronized
- 在win32的窗口程序中加入控制台console窗口
- Bridge----设计模式
- 设计模式-简单工厂模式
- [转载备用]极酷SevenColorPlayer网页播放器(炫彩广告版),最强播放器定制
- HGE:Tutorials:Rectangle Class
- 4. android 设置全屏且无标题