stl源码剖析 详细学习笔记 算法(3)

//---------------------------15/03/30----------------------------

//min_element

template<class ForwardIterator>

ForwardIterator min_element(ForwardIterator first, ForwardIterator last)

{

if(first == last)

return first;

ForwardIterator result = first;

while(++first != last)

if(*first < *result)   
//自定义版本if(comp(*first, *result))

result = first;

return result;

}

//partition

//快排的基本步骤:划分,让判断true的元素放在区间前,让判断为false的元素放在区间后

//返回第一个非true的位置:第一种情况 end。第二种情况
元素被判断为flase

//这是不稳定的。

template<class BidirectionalIterator,
class Predicate>

BidirectionalIterator partition(BidirectionalIterator first,

BidirectionalIterator last,

Predicate pred)

{

while (true)

{

while (true)

{

if(first == last)

return first;

else if(pred(*first))

++first;

else

break;

}

--last;             //第一次指向的事end所以--
之后指向的是判断过的数,所以要--

while (true)

{

if(first == last)

return first;

else if(!pred(*last))

--last;

else

break;

}

iter_swap(first, last);

++first;           
//接下来要判断下一个

}

}

//remove

template<class ForwardIterator,
class T>

ForwardIterator remove(ForwardIterator first, ForwardIterator last,

const T& value)

{

first = find(first, last, value);

ForwardIterator next = first;

return first == last ? first : remove_copy(++next, last, first, value);

}

template<class InputIterator,
class OutputIterator,
class T>

OutputIterator remove_copy(InputIterator, first, InputIterator last,

OutputIterator result,
const T& value)

{

for(; first != last; ++first)

if(*first != value)        
//remove_copy_if版本  if(!pred(*first))

{

*result = *first;

++result;

}

return result;

}

//replace

template<class ForwardIterator,
class T>

void replace(ForwardIterator first, ForwardIterator last,

const T& old_value,
const T& new_value)

{

for(; first != last; ++first)

if(*first == old_value)

*first = new_value;

//replace_if版本 if(pred(*first)) *first = new_value;

}

template<class InputIterator,
class OutputIterator,
class T>

OutputIterator replace_copy(InputIterator first, InputIterator last,

OutputIterator result,
const T& old_value,

const T& new_value)

{

for(; first != last; ++first, ++result)

*result = *first == old_value ? new_value : *first;

//replace_copy_if版本 *result = pred(*first) ? new_value : *first;

return result;

}

//reverse

template<class BidirectionalIterator>

inline void reverse(BidirectionalIterator first, BidirectionalIterator last)

{

__reverse(first, last, iterator_category(first));

}

template<class BidirectionalIterator>

void __reverse(BidirectionalIterator first, BidirectionalIterator last,

bidirectional_iterator_tag)

{

while (true)

{

if(first == last || first == --last)

return;

else

iter_swap(first++, last);

}

}

//随机迭代器的版本只需要判断一次
而非随机迭代器要判断两次,故效率上有差距,所以分开写

template<class RandomAccessIterator>

void __reverse(RandomAccessIterator first, RandomAccessIterator last,

random_access_iterator_tag)

{

while(first < last)

iter_swap(first++, --last);

}

template<class BidirectionalIterator,
class OutputIterator>

OutputIterator reverse_copy(BidirectionalIterator first,

BidirectionalIterator last,

OutputIterator result)

{

while (first != last)

{

--last;

*result = *last;

++result;

}

return result;

}

//rotate

template<class ForwardIterator>

inline void rotate(ForwardIterator first, ForwardIterator middle,

ForwardIterator last)

{

if(first == middle || middle == last)

return;

__rotate(first, middle, last, distance_type(first),

iterator_category(first));

}

//单向的迭代器走这边

//只能单向行走,效率最低,要赋值起码1.5n次(当两者区间相同时)。

//调用一次swap就要赋值3次

template<class ForwardIterator,
class Distance>

void __rotate(ForwardIterator first, ForwardIterator middle,

ForwardIterator last, Distance*, forward_iterator_tag)

{

for(ForwardIterator i = middle; ;)

{

iter_swap(first, i);

++first;

++i;

if(first == middle)

{

if(i == last)

return;

middle = i;

}

else if(i ==last)

i = middle;

}

}

//三次反转
固定赋值1.5n次

template<class BidirectionalIterator,
class Distance>

void __rotate(BidirectionalIterator first, BidirectionalIterator middle,

BidirectionalIterator last, Distance*,

bidirectional_iterator_tag)

{

reverse(first, middle);

reverse(middle, last);

reverse(first, last);

}

template<class RandomAccessIterator,
class Distance>

void __rotate(RandomAccessIterator first, RandomAccessIterator middle,

RandomAccessIterator last, Distance*,

random_access_iterator_tag)

{

Distance n = __gcd(last - first, middle - first);

while (n--)

__rotate_cycle(first, last, first + n, middle - first,

value_type(first));

}

template<class EuclideanRingElement>

EuclideanRingElement __gcd(EuclideanRingElement m, EuclideanRingElement n)

{

while (n != 0)

{

EuclideanRingElement t = m % n;

m = n;

n = t;

}

return m;

}

//最差的情况赋值1.5n次(当区间相等时),最少赋值n+1次(第一个或第二个区间为1时)。

template<class RandomAccessIteratorm,
class Distance, class T>

void __rotate_cycle(RandomAccessIterator first, RandomAccessIterator last,

RandomAccessIterator initial, Distance shift, T*)

{

T value = *initial;

RandomAccessIterator ptr1 = initial;

RandomAccessIterator ptr2 = ptr1 + shift;

while (ptr2 != initial)

{

*ptr1 = *ptr2;

ptr1 = ptr2;

if(last - ptr2 > shift)

ptr2 += shift;

else

ptr2 = first + (shift - (last - ptr2));

}

*ptr1 = value;

}

//rotate_copy

template<class ForwardIterator,
class OutputIterator>

OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle,

ForwardIterator last, OutputIterator result)

{

return copy(first, middle, copy(middle, last, result));

}

//search

template<class ForwardIterator1,
class ForwardIterator2>

inline ForwardIterator search(ForwardIterator1 first1,

ForwardIterator1 last1,

ForwardIterator2 first2,

ForwardIterator last2)

{

return __search(first1, last1, first2, last2,distance_type(first1),

distance_type(first2));

}

template<class ForwardIterator1,
class ForwardIterator2,
class distance1,

class Distance2>

ForwardIterator1 __search(ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2,

Distance1*, Distance2*)

{

Distance1 d1 =
0;

distance(first1, last1, d1);

Distance2 d2 =
0;

distance(first2, last2, d2);

if(d1 < d2) return last1;

ForwardIterator1 current1 = first1;

ForwardIterator2 current2 = first2;

while (current2 != last2)

{

if (*current1 == *current2)

{

++current1;

++current2;

}

else

{

if (d1 == d2)

return last1;

else

{

current1 = ++first1;

current2 = first2;

--d1;

}

}

}

return first1;

}

//search_n

template<class ForwardIterator,
class Interger, class T>

ForwardIterator search_n(ForwardIterator first,

ForwardIterator last,

Interger count,
const T& value)

{

if(count <= 0)

return first;

else

{

//先找到第一个

first = find(first, last, value);

while (first != last)

{

Interger n = count -
1;

ForwardIterator i = first;

++i;

//找连续符合的元素,都找到了的话 n会等于0;

while (i != last && n !=
0 && *i == value)

{

++i;

--n;

}

if(n == 0)

return first;

else

first = find(i, last, value);

}

return last;

}

}

template<class ForwardIterator,
class Interger, class T,

class BinaryPredicate>

ForwardIterator search_n(ForwardIterator first,

ForwardIterator last,

Interger count,
const T& value,

BinaryPredicate binary_pred)

{

if(count <= 0)

return first;

else

{

//自己实现find,其实可以使用find_if版本的
不过需要使用bind函数

while (first != last)

{

if(binary_pred(*first, value))

break;

++first;

}

while (first != last)

{

Interger n = count -
1;

ForwardIterator i = first;

++i;

//找连续符合的元素,都找到了的话 n会等于0;

while (i != last && n !=
0 && *i == value)

{

++i;

--n;

}

if(n == 0)

return first;

else

{

while (i != last)

{

if(binary_pred(*first, value))

break;

++i;

}

first = i;

}

}

return last;

}

}

//swap_ranges

//区间不能重叠,不然会产生未知结果

template<class ForwardIterator1,
class ForwardIterator2>

ForwardIterator2 swap_ranges(ForwardIterator1 first1,

ForwardIterator1 last1,

ForwardIterator2 first2)

{

for(; first != last1; ++first1, ++first2)

iter_swap(first1, first2);

return first2;

}

//transform

//对每个元素执行op操作后,复制到目的区间

template<class InputIterator,
class OutputIterator,
class UnaryOperation>

OutputIterator transform(InputIterator first1, InputIterator last,

OutputIterator result, UnaryOperation op)

{

for(; first != last; ++first, ++result)

*result = op(*first);

return result;

}

template<class InputIterator1,
class InputIterator2,
class OutputIterator,

class BinaryOperation>

OutputIterator transform(InputIterator1 first1, InputIterator last1,

InputIterator2 first2, InputIterator last2,

BinaryOperation binary_op)

{

for(; first1 != last1; ++first1, ++first2, ++result)

*result = binary_op(*first1, *first2);

return result;

}

//unique

//只能移除相邻的重复元素(首先要有相邻元素才会开始删除,相邻元素后面如果还有同样的元素也会被删除)

//要移除所有重复元素必须先排序

template<class ForwardIterator>

ForwardIterator unique(ForwardIterator first, ForwardIterator last)

{

first = adjacent_find(first, last);

return unique_copy(first, last, first);

}

template<class InputIterator,
class ForwardIterator>

inline OutputIterator unique_copy(InputIterator first,

InputIterator last,

OutputIterator result)

{

if(first == last)
return result;

return __unique_copy(first, last, result, iterator_category(result));

}

template<class InputIterator,
class ForwardIterator>

ForwardIterator __unique_copy(InputIterator first,

InputIterator last,

ForwardIterator result,

forward_iterator_tag)

{

*result = *first;

while (++first != last)

{

if(*result != *first)

*++result = *first;

}

return ++result;

}

template<class InputIterator,
class OutputIterator>

inline OutputIterator __unique_copy(InputIterator first,

InputIterator last,

ForwardIterator result,

output_iterator_tag)

{

return __unique_copy(first, last, result, value_type(first));

}

template<class InputIterator,
class OutputIterator,
class T>

OutputIterator __unique_copy(InputIterator first, InputIterator last,

OutputIterator result, T*)

{

T value = *first;

*result = value;

while (++first != last)

{

if(value != *first)
//如果result为只读,这里就只能通过值判断
而不能使用(*result != *first)

{

value = *first;

*++result = value;

}

}

return ++result;

}

时间: 2024-11-08 02:33:29

stl源码剖析 详细学习笔记 算法(3)的相关文章

stl源码剖析 详细学习笔记 算法总览

//****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标记 算法名称              算法用途         质变                   所在文件 accumulate          元素累计            否                   <stl_numeric.h> adjacent_differenc

stl源码剖析 详细学习笔记 算法(1)

//---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都作用在由迭代器[first,last)所标示出来的区间上.质变算法 就是 运算过程会更改区间内的元素内容 非质变算法:和质变算法相反 */ /* stl算法的一般形式 1>所有的泛型算法的前两个参数都是一对迭代器,通常称为first和last,用以标示算法的操作区间 2>stl习惯采用前闭后开区间

stl源码剖析 详细学习笔记 算法(2)

//---------------------------15/03/29---------------------------- //****************************set相关算法***************************** /* 1>set相关算法一共有4种:并集(union),交集(intersection),差集(difference), 对称差集(symmetric difference). 2>set相关算法只接受set/multiset. 3

stl源码剖析 详细学习笔记 算法(4)

//---------------------------15/03/31---------------------------- //lower_bound(要求有序) template<class ForwardIterator, class T> inline ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value) { return __lower_bound

stl源码剖析 详细学习笔记 算法(5)

//---------------------------15/04/01---------------------------- //inplace_merge(要求有序) template<class BidirectionalIterator> inline void inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) { if(first

stl源码剖析 详细学习笔记 空间配置器

//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调用::operator new配置内存(底层使用malloc来申请内存). 2>调用函数的构造函数,构造对象内容. delte和new一样,先调用析构函数,再调用::operator delete释放内存. 2:为了效率,stl把两个阶段分开来. 1>内存配置操作: alloc::allocate

stl源码剖析 详细学习笔记 仿函数

//---------------------------15/04/01---------------------------- //仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操作,比如比大小,返回bool值,对元素进行操作等 //虽然这些函数也能实现,但是如果配合配接器(adapter)可以产生更灵活的变化. //为了使对象像函数一样,就必须重载operator() //unary_function template<class Arg, class Result> s

stl源码剖析 详细学习笔记 配接器

//---------------------------15/04/03---------------------------- /* 配接器概述: 1:adapter是一种设计模式:将一个class的接口转换为另一个class的接口,使得原本因接口不兼容而 不能合作的classes可以一起工作. 2:改变仿函数接口的,称为function adapter,改变容器接口的,称为container adapter, 改变迭代器接口的,称为iterator adapter. 3:container

《STL源码剖析》学习笔记-第6章(一) set相关算法

STL中定义的set要求元素不得重复且已经排序.而set算法要求的都是有序区间(输出也是有序的),但元素可以重复出现. STL提供了4个set相关的算法,分别是并集(union).交集(intersection).差集(difference)和对称差集(symmetric difference),这4个算法接受的set必须是有序区间,都至少接受4个参数,分别表示两个set区间.一般而言,set算法前4个参数分别表示两个区间,第五个参数表示存放结果的区间的起始位置. 1.set_union 求两个