学习笔记:STL通用算法模板中find函数的用法

参考书目:C/C++规范设计简明教程,P367

目的:学习STL通用算法模板中find函数的用法

//通用算法find的参数——使用迭代器
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>	//通用函数头文件
using namespace std;
int main()
{
	cout << "Hello World!\n";
	vector<int> v;
	int score[5] = { 78,76,84,93,60 };
	for (int i = 0; i < 5; i++)
	{
		v.push_back(score[i]);
	}
	
	vector<int>::iterator it;		//vector的迭代器
	for (it = v.begin(); it <v.end(); ++it)	//遍历输出所有元素
	{
		cout <<  *it <<" ";
	}
	cout << endl;

	it = find(v.begin(), v.end(), 76);
	if (it != v.end())
	{
		cout << "发现" << *it << endl;
	}
	else
	{
		cout << "没有发现" << *it << endl;
	}


	getchar();
}

运行结果如下:

发布了34 篇原创文章 · 获赞 1 · 访问量 726

猜你喜欢

转载自blog.csdn.net/qq_41708281/article/details/104178434
今日推荐