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;

二、元素计数算法

1.count / count_if 

原型: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));

三、最大值最小值

1.min_element / max_element

原型: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);

	cout << "minNum is:" << minNum << endl;
	cout << "maxNum is:" << maxNum << endl;
	cout << "minAbsNum is:" << minAbsNum << endl;
	cout << "maxAbsNum is:" << maxAbsNum << endl;
<span style="font-size:18px;">四、搜寻元素
</span>
<span style="font-size:18px;">1.find_if / find</span>
<span style="font-size:18px;">原型:Iterator find(Iterator begin , Iterator end,const T& value)
      Iterator fin_if(Iterator begin ,Iterator end , UnaryPredicate op)</span>
<span style="font-size:18px;">功能:在容器中搜索满足value和op的元素</span>
<span style="font-size:18px;">P S :distance(InputIterator first,InputIterator last)计算两个迭代器变量间的距离</span>
<span style="font-size:18px;">用法:</span><pre name="code" class="cpp" style="font-size: 18px;">	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  = 5 pos :" << distance(myvector2.begin(),pos_find_1) + 1 << endl;
	cout << "first value  > 5 pos :" << distance(myvector2.begin(),pos_find_if) + 1 << endl;

 

 
<strong><span style="font-size:18px;">2. search_n</span><span style="font-size:18px;">
</span></strong>
<strong><span style="font-size:18px;">原型: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)</span></strong>
<strong><span style="font-size:18px;">功能:搜索前n个连续匹配的值</span></strong>
<strong><span style="font-size:18px;">用法:</span></strong><pre name="code" class="cpp">	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)

功能:搜索第一个子区间
 
用法:<pre name="code" class="cpp">	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)
功能:搜索最后一个子区间
用法:<pre name="code" class="cpp">	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 << "子串在原串的位置是:" << distance(myvector2.begin(), pos_find_end_1) + 1 << endl;
	}
	else
	{
		cout << "没有搜索到子串" << endl;
	}

 
 
5.find_first_of 
 
<strong><span style="font-size:18px;">原型:Iterator find_first_of(Iterator begin,Iterator end,Iterator2 searchBegin,Iterator2,searchEnd)
      Iterator find_first_of(Iterator begin,Iterator end,</span></strong>
<strong><span style="font-size:18px;">                              Iterator2 searchBegin,Iterator2,searchEnd,binaryPredicate op)</span></strong>
功能: 搜索某一元素第一次出现的位置
用法:
	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,再努力提高自己的C++编程技术。文章中如果有不对的地方,欢迎大家指正。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值