unique函数(STL)
摘要:unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include,具体用法如下: int num[100]; unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
阅读全文
posted @
2014-02-18 11:24
吉大依恋
阅读(294)
推荐(0)
原生指针auto_ptr的用法
摘要:原生指针auto_ptr在头文件<memory>中,auto_ptr角括号内放的是“原生指针所指对象”的型别,而不是原生指针的型别。auto_ptr如下用法,和原生指针一模一样:#include<iostream>#include<string>#include<memory>using namespace std;void func(){ auto_ptr<string> ps(new string("jjhou")); cout<<*ps<<endl;//输出jjhoucout<&
阅读全文
posted @
2013-04-26 10:04
吉大依恋
阅读(341)
推荐(0)
临时对象的产生与运用
摘要:#include<iostream>#include<vector>#include<algorithm>using namespace std;template <typename T>class print{public:void operator()(const T& elem){ cout<<elem<<' ';}};int main(){int ia[6]={0,1,2,3,4,5};vector<int> iv(ia,ia+6);for_each(iv.begin(),iv.
阅读全文
posted @
2013-04-23 10:37
吉大依恋
阅读(170)
推荐(0)
自增自减操作符
摘要:#include<iostream>using namespace std;class INT{friend ostream& operator<<(ostream& os,const INT& i);public:INT(int i):m_i(i){};//prefix ++INT& operator++(){ ++(this->m_i);return *this;}//postfix ++const INT operator++(int){ INT temp=*this; ++(*this); return temp;}//pr
阅读全文
posted @
2013-04-23 10:35
吉大依恋
阅读(176)
推荐(0)
function call 操作符
摘要:#include<iostream>using namespace std;int fcmp(const void* elem1,const void* elem2){ const int* i1=(const int*)elem1; const int* i2=(const int*)elem2; return (*i1-*i2);}int main(){int ia[10]={32,92,67,58,10,4,25,52,59,54};for(int i=0;i<10;i++){ cout<<ia[i]<<" ";}cout&l
阅读全文
posted @
2013-04-23 10:34
吉大依恋
阅读(228)
推荐(0)
重载operator()
摘要:#include<iostream>using namespace std;template<class T>struct plus{ T operator()(const T& x,const T& y) const{ return x+y;}};template<class T>struct minus{ T operator()(const T& x,const T& y) const { return x-y; }};int main(){ plus<int> plusobj;minus<int>
阅读全文
posted @
2013-04-23 10:32
吉大依恋
阅读(297)
推荐(1)
将operator()多载化实现仿函式
摘要:STL算法的特殊版本所接受的所谓 条件 或 策略 或 一组动作,都以仿函式形式呈现。所谓仿函式(functor)就是使用起来像函式一样的东西。如果你针对某个class 进行operator()多载化,它就成为一个仿函式。至于要成为一个可配接的仿函式,还需要一写额外的努力。#include<iostream>using namespace std;//由于将operator()多载化了,因此plus成了一个仿函式template <class T>struct plus{T operator()(const T& x,const T& y){return
阅读全文
posted @
2012-11-03 10:45
吉大依恋
阅读(251)
推荐(0)
STL六大组件
摘要:STL提供六大组件,彼此可以组合套用:1.容器(containers):各种数据结构,如vector,list,deque,set,map,用来存放数据。从实作的角度看,STL容器是一种class template。2.算法(algorithms):各种常用算法如sort,search,copy,erase...,从实作的角度看,STL算法是一种function template。3.迭代器(iterators):扮演容器与算法之间的胶着剂,是所谓的 泛型指标,是一种将operator*,operator->,operator++,operator--等指标相关操作予以多载化的class
阅读全文
posted @
2012-11-02 21:41
吉大依恋
阅读(486)
推荐(0)