STL迭代器与部分算法学习笔记

迭代器是类似指针的对象,分为5种,输入,输出,前向,双向和随机访问

输入迭代器(InputIterator)

输入迭代器并不是指某种类型,而是指一系列类型
举例
template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

非可变序列算法(不改变容器内容);find, adjacent_find, count, for_each, midmatch, equal, search

find_if,查找序列中第1个使给定的判断函数(函数对象)返回真的元素

template<class InputIterator, class UnaryPredicate>
  InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) return first;
    ++first;
  }
  return last;
}


adjacent_find算法在序列中查找相邻且相等的两个元素(规则由函数对象决定,默认为相等),当找到这样的两个元素时,该算法返回指向两个元素中第一个元素的迭代器

template <class ForwardIterator>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
{
  if (first != last)
  {
    ForwardIterator next=first; ++next;
    while (next != last) {
      if (*first == *next)     // or: if (pred(*first,*next)), for version (2)
        return first;
      ++first; ++next;
    }
  }
  return last;
}


count是一种非可变序列算法,其功能是在序列中查找等于某个给定值的元素的个数

template <class InputIterator, class UnaryPredicate>
  typename iterator_traits<InputIterator>::difference_type
    count (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  while (first!=last) {
    if (pred(*first)) ++ret;
    ++first;
  }
  return ret;
}




   
   
   
   

count_if:

template <class InputIterator, class UnaryPredicate>
  typename iterator_traits<InputIterator>::difference_type
    count (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  while (first!=last) {
    if (pred(*first)) ++ret;
    ++first;
  }
  return ret;
}
例子:
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

int main(void)
{
	int arr[] = {1, 3, 2, 1, 4, 1, 7, 1, 10};
	vector<int> v(&arr[0], &arr[9]);
	int cnt1 = count(v.begin(), v.end(), 1);
	int cnt2 = count_if(v.begin(), v.end(), bind2nd(not_equal_to<int>(), 1));//bind2nd是函数适配器。可将函数对象作为参数
	cout << "the count of 1 in v is " << cnt1 << endl;
	cout << "the cnt of not equaling 1 is " << cnt2 << endl;
	return 0;
}


for_each 对序列中的每个元素施加由函数f指定的操作

template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return fn;      // or, since C++11: return move(fn);
}
例子:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void print(string &s)
{
	cout << s << endl;
}

int main(void)
{
	vector<string> str_v;
	str_v.push_back("Clark");
	str_v.push_back("Rindt");
	str_v.push_back("Senna");
	for_each(str_v.begin(), str_v.end(), print);
	return 0;
}

可变序列算法(可以修改容器的内容)copy fill generate partition shuffle remove replace reverse swap transform

copy将容器中的元素从一个区间复制到另一个区间
template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
  while (first!=last) {
    *result = *first;
    ++result; ++first;
  }
  return result;
}

例子
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;

int main(void)
{
	int arr[] = {1, 2, 3, 4, 5};
	vector<int> v(&arr[0], &arr[5]);
	ostream_iterator<int> out(cout, "\n");
	copy(v.begin(), v.end(), out);
	return 0;
}

partition

对于给定区间[first, last)和一个一元判断函数pred, 类属算法partition可以对该区间内的元素重新排列,以使所有满足判断函数pred的元素排在所有不满足pred的元素前面,该算法还有一个版本stable_partition,能保证分割后的每一组元素的相对位置保持不变,返回值都是一个迭代器,该迭代器代表第一组数据的结尾,同时也是第二组数据的开头
template <class BidirectionalIterator, class UnaryPredicate>
  BidirectionalIterator partition (BidirectionalIterator first,
                                   BidirectionalIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    while (pred(*first)) {
      ++first;
      if (first==last) return first;
    }
    do {
      --last;
      if (first==last) return first;
    } while (!pred(*last));
    swap (*first,*last);
    ++first;
  }
  return first;
}
例子
#include <iostream>
#include <algorithm>
#include <ctime>
#include <iterator>
#include <algorithm>
#include <sys/time.h>
#include <cstdlib>
using namespace std;

struct IsEven  //函数对象
{
	bool operator()(int x) { return (x&0x01)==1; }
};

int myrandom(int i) { return rand()%i; }

int main(void)
{
	srand((unsigned)time(NULL));
	int arr[10];
	struct timeval start, end;
	for(int i=0; i<10; i++)
		arr[i] = i;
	random_shuffle(&arr[0], &arr[10], myrandom);  //将数组元素打乱
	gettimeofday(&start, NULL);
	cout << *(partition(&arr[0], &arr[10], IsEven())) << endl;  //重排数组,将奇偶数分开
        //计算运行时间
	gettimeofday(&end, NULL);
	float t_time = end.tv_sec-start.tv_sec+(end.tv_usec-start.tv_usec)/1000000.0;
	cout << "time used " << t_time << endl;
	ostream_iterator<int> out(cout, " ");
	copy(&arr[0], &arr[10], out); 
	cout << endl;
	return 0;
}


你可能感兴趣的:(STL迭代器与部分算法学习笔记)