STL算法分析

STL power算法:

template<class T , class Integer, class MonoidOperation>

T power(T x ,Iteger n, MonoidOperation op){

if(n == 0) return identity_element(op); //证同元素,如果是0的话,代表加法的正同元素

else{

while((n & 1) == 0){ //n为偶数的时候

n >>= 1;

x = op(x,x)

}

T result = x;

n >>= 1;

while(n != 0){  //n为奇数的形式

x = op(x,x);

if(n !=0 )result = op(result, x);

n >>= 1;

}

return result;

}

}


//字符串逻辑比较的特化版

inline bool lexicographical_compare(const unsigned char* first1, const unsigned char* last1, const unsigned char* first2, const unsigned char* last2)

{

const size_t len1 = last1 - first1;

const size_T len2 = last2 - first2;

const int result = memcmp(first1, first2,min(len1, len2) ); //非常好的方法,先比较公共的部分。

return result != 0? result < 0 : len1 < len2;

}


//取两个集合的并集

template<class InputIterator1 , class InputIterator2, class OutputIterator >

OutputIterator set_union(InputIterator1 first1, InputIterator last1, InputIterator first2, InputIterator last2, OutputIterator result)

{

while(first1  !=  last1 && first2 != last2){

if(*first1 < *first2){

*result = *first1;

++*first1;

}

else if (* first1 > *first2){

*result = *first2;

++first2;

}

else{

*result = *first1;

++first1;

++first2;

}

}

++result;

return copy(first2, last2,copy(first1, first2, result));

}


//查找满足条件的相邻元素。

template<class ForwardIterator>

ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last )

{

if(first == last) return last;

ForwardIterator next = first;

while(++next != last){

if(*first == *next)return first;

first = next;

}

return last;

}


//find_end函数,在序列一[first1,last1) 所覆盖的区间中,查找序列二[first2,last2)的最后一次出现点

ForwardIterator前向迭代器

template<class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 _find_end(ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2, forward_iterator_tag, forward_iterator_tag)

{

if(first2 == last2)return last1;

else{

ForwardIterator1 result = last1;

while(1){

ForwardIterator1 new_result = search(first1, last1,first2,last2); //查找

if(new_result == last1)

return result;

else{              //多次查找,查找到匹配的点

result = new_result;

first1 = new_result;

++first1;

}

}

}

}

search,在序列一所涵盖的区间中,查找序列二的首次出现点,单纯的讲解方法不再考虑泛化。


char *  search(char* first1, char* last1, char* first2, char* last2)

{

int len1 = last1 - first1;  //字符串1长度

int len2 = last2 - first2; //字符串2长度

if(len2 > len1) return last1;

char* cur1 = first1;  //当前字符

char* cur2 = first2; //当前字符

while(cur2 != last2){

if(*cur1 == *cur2){

cur1++;

cur2++;

}

else{

if(len1 == len2) return last1;

else{

cur1 = ++first;

cur2 = first2;

--len1;

}

}

}

return first1;

}


char* search_n(char* first, char *last, int count, int value)

{

if(count <= 0) return first;

else{

first = find(first, last, value);

while(first != last){

int n = count -1;

char* i = first;

++i;

while(i != last && n !=0 && *i == value){++i; --n;}

if(n == 0) return first;

else

 first = find(i, last, value);

}

return last;

}

}



猜你喜欢

转载自blog.csdn.net/w1012747007/article/details/76385862