当前位置: 代码迷 >> 综合 >> (四)算法与迭代器分析
  详细解决方案

(四)算法与迭代器分析

热度:44   发布时间:2023-10-17 01:19:56.0

目录

算法的形式

迭代器的分类

迭代器的分类对算法的影响

算法源码剖析(11个例子)

sort算法,区分C函数和STL库函数

算法accumulate

算法for_each

算法replace,replace_if,replace_copy

算法count,count_if

算法find,find_if

reverse iterator:rbegin(),rend()

算法binary_search


算法的形式

标准库算法形式

 

迭代器的分类

不同容器iterator类型不同

  • 测试iterator类型
#include <iostream>     // std::cout
#include <iterator>     // std::iterator_traits
#include <typeinfo>     // typeid
namespace jj33
{
void _display_category(random_access_iterator_tag)
{   cout << "random_access_iterator" << endl; }
void _display_category(bidirectional_iterator_tag)
{   cout << "bidirectional_iterator" << endl; }
void _display_category(forward_iterator_tag)
{   cout << "forward_iterator" << endl;  }
void _display_category(output_iterator_tag)
{   cout << "output_iterator" << endl;   }
void _display_category(input_iterator_tag)
{   cout << "input_iterator" << endl;    }template<typename I>
void display_category(I itr)
{typename iterator_traits<I>::iterator_category cagy;_display_category(cagy);cout << "typeid(itr).name()= " << typeid(itr).name() << endl << endl;   //The output depends on library implementation.//The particular representation pointed by the  //returned valueis implementation-defined, //and may or may not be different for different types.   
}void test_iterator_category()
{cout << "\ntest_iterator_category().......... \n";display_category(array<int,10>::iterator());display_category(vector<int>::iterator());display_category(list<int>::iterator());    display_category(forward_list<int>::iterator());  display_category(deque<int>::iterator());display_category(set<int>::iterator());display_category(map<int,int>::iterator());display_category(multiset<int>::iterator());display_category(multimap<int,int>::iterator());display_category(unordered_set<int>::iterator());display_category(unordered_map<int,int>::iterator());display_category(unordered_multiset<int>::iterator());display_category(unordered_multimap<int,int>::iterator());    display_category(istream_iterator<int>());display_category(ostream_iterator<int>(cout,""));
}                                                            
}

 

 istream_iterator,ostream_iterator的iterator_category

迭代器的分类对算法的影响

  • distance函数实现
  • 对于连续空间直接相减,不连续空间需要重新计算

  • type traits对算法的影响
  • traits为萃取机,将iterator和type放进去,然后回答一些属性问题?

  • 输入迭代器可读,输出迭代器可写。
  • 算法模板参数输入名称,有时候暗示该算法输入的迭代器类型

 

算法源码剖析(11个例子)

sort算法,区分C函数和STL库函数

本身遍历容器iterator就是有序的

算法accumulate

算法for_each

算法replace,replace_if,replace_copy

算法count,count_if

全局函数和成员函数的区别

算法find,find_if

reverse iterator:rbegin(),rend()

有序查找,红黑树是怎么查找的?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  相关解决方案