首页 > 代码库 > C++STL之algorithm(一)

C++STL之algorithm(一)

小编最近在努力学习C++进阶编程知识,趁着周末总结一下自己学习的STL算法部分的一些方法,和大家一起分享一下。

非修改性算法:

 一、for_each 算法 

原型: for_each(Iterator begin,Iterator end,Function fn)

功能:遍历容器中的元素

用法:

	vector <int> myvector,myvector2;
	fillValue(myvector);
	fillValue(myvector2);
	for_each(myvector.begin(), myvector.end(), printValue);
	cout << endl;

	for_each(myvector.begin(), myvector.end(), Multiple<int>(2));
	for_each(myvector.begin(), myvector.end(), printValue);
	cout << endl;

	double sum = for_each(myvector.begin(), myvector.end(), SUM());
	cout << sum << endl;

二、元素计数算法

原型: count(Iterator begin,Iterator end,const T& value)
          count(Iterator begin,Iterator end,UnaryPredicate op)

功能:统计容器中满足value和op的元素个数

用法:

	int ct = count(myvector.begin(), myvector.end(), 4);
	int ctif = count_if(myvector.begin(), myvector.end(), isEven);
	int ctg = count_if(myvector.begin(), myvector.end(), bind2nd(greater<int>(), 2));

三、最大值最小值

原型:Iterator min_element(Iterator beg , Iterator end)
     Iterator min_element(Iterator beg , Iterator end , compFunc op)
     Iterator max_element(Iterator beg , Iterator end)
     Iterator max_element(Iterator beg , Iterator end , compFunc op)

功能:寻找容器中的最大值和最小值,返回其地址。

用法:

	int minNum = *min_element(myvector2.begin(), myvector2.end());
	int maxNum = *max_element(myvector2.begin(), myvector2.end());

	int minAbsNum = *min_element(myvector2.begin(), myvector2.end(), absLess);
	int maxAbsNum = *max_element(myvector2.begin(), myvector2.end(), absLess);

四、搜寻元素

1. find / find_if

原型:Iterator find(Iterator begin , Iterator end , const T& value)
     Iterator fin_if(Iterator begin ,Iterator end , UnaryPredicate op)

功能:寻找容器中满足value和op的元素

P S : distance(InputIterator first,InputIterator last) 计算两个迭代器变量间的距离

用法:

	vector<int>::iterator pos_find_1, pos_find_if;
	
	pos_find_1 = find(myvector2.begin(), myvector2.end(), 5);
	
	// 返回第一个值大于3的位置
	
	pos_find_if = find_if(myvector2.begin(), myvector2.end(), bind2nd(greater<int>(),3));
	cout << "first value  = http://www.mamicode.com/5 pos :" << distance(myvector2.begin(),pos_find_1) + 1 << endl;
	cout << "first value  > 5 pos :" << distance(myvector2.begin(),pos_find_if) + 1 << endl;

2. search_n

原型:Iterator search_n (Iterator begin, Iterator end, Size count, const T& value)
     Iterator search_n (Iterator begin, Iterator end, Size count, const T& value,BinaryPredicate op)

功能:搜索前n个连续匹配的值

用法:

	vector<int>::iterator pos_search_n_1,pos_search_n_2;
	
	// 搜索vector 中连续两个值为2的起始位置
	pos_search_n_1= search_n(myvector2.begin(), myvector2.end(), 2, 2);
	
	// 搜索vector 中连续值大于2的起始位置 
	pos_search_n_2 = search_n(myvector2.begin(), myvector2.end(),4,4,greater<int>());
	
	if (pos_search_n_1 != myvector2.end())
	{
		cout << "2个连续大于2的值的起始位置是: " << distance(myvector2.begin(), pos_search_n_1) + 1 << endl;
	}
	else
	{
		cout << "没有找到符合要求的元素" << endl;
	}
		
	if (pos_search_n_2 != myvector2.end())
	{
		cout << "4个连续大于4的值的起始位置是: " << distance(myvector2.begin(), pos_search_n_2) + 1 << endl;
	}
	else
	{
		cout << "没有找到符合要求的元素" << endl;
	}

3. search

原型:Iterator search (Iterator1 begin, Iterator1 end, Iterator2 searchBegin, Iterator2 searchEnd)

     Iterator search (Iterator1 begin, Iterator1 end,

            Iterator2 searchBegin, Iterator2 searchEnd, BinaryPredicate op)

功能:搜索第一个子区间

用法:

	vector<int> subVector; 
	vector<int>::iterator pos_search_1,pos_search_2;

	setValue(subVector, -1, 3);
	pos_search_1 = search(myvector2.begin(), myvector2.end(), subVector.begin(), subVector.end());

	if (pos_search_1 != myvector2.end())
	{
		cout << "子串在原串的位置是(search):" << distance(myvector2.begin(), pos_search_1) + 1 << endl;
	}
	else
	{
		cout << "没有搜索到子串" << endl;
	}

	bool checkEvenArr[3] = { true, false, true };
	pos_search_2 = search(myvector2.begin(), myvector2.end(), checkEvenArr, checkEvenArr + 3, checkEven);

	if (pos_search_2 != myvector2.end())
	{
		cout << "子串在原串的位置是(search + op):" << distance(myvector2.begin(), pos_search_2) + 1 << endl;
	}
	else
	{
		cout << "没有搜索到子串" << endl;
	}

4. find_end 

原型:Iterator find_end(Iterator begin,Iterator end,Iterator2 searchBegin,Iterator2,searchEnd)
     Iterator find_end(Iterator begin,Iterator end,

             Iterator2 searchBegin,Iterator2,searchEnd,binaryPredicate op)

功能:搜索最后一个子区间

用法:

	vector<int>::iterator pos_find_end_1;
	pos_find_end_1 = find_end(myvector2.begin(), myvector2.end(), subVector.begin(), subVector.end());
	if (pos_find_end_1 != myvector2.end())
	{
		cout << "子串在原串的位置是(find_end):" << distance(myvector2.begin(), pos_find_end_1) + 1 << endl;
	}
	else
	{
		cout << "没有搜索到子串" << endl;
	}

5.find_first_of

原型:Iterator find_first_of(Iterator begin,Iterator end,Iterator2 searchBegin,Iterator2,searchEnd)
     Iterator find_first_of(Iterator begin,Iterator end,

               Iterator2 searchBegin,Iterator2,searchEnd,binaryPredicate op)

功能:搜索某一元素第一次出现的位置

用法:

	subVector.clear();
	myvector.clear();
	setValue(myvector, -3, 12);
	setValue(myvector, -3, 6);
	setValue(subVector, -1, 3);

	vector<int>::iterator pos_find_first_of_1;
	pos_find_first_of_1 = find_first_of(myvector.begin(), myvector.end(), subVector.begin(), subVector.end());
	
	if (pos_find_first_of_1 != myvector.end())
	{
		cout << "子串在原串的位置是(find_first_of):" << distance(myvector.begin(), pos_find_first_of_1) + 1 << endl;
	}
	else
	{
		cout << "没有搜索到子串" << endl;
	}

	vector<int>::reverse_iterator rpos;
	rpos = find_first_of(myvector.rbegin(), myvector.rend(), subVector.begin(), subVector.end());
	cout << "原串中最后一个子串的位置是:" << distance(myvector.begin(), rpos.base()) << endl;

6. adjacent_find

原型:adjacent_find(T first,T last)
     adjacent_find(T first,T last,Pred op)

功能:搜索两个连续相等的元素

 

最后再说两句:

    上述总结如果有错误的地方欢迎大家指正。小编正在努力学习,文章也会不断更新,敬请关注!

 

C++STL之algorithm(一)