c++STL剖析(3)之find()

本文详细介绍了C++ STL中的find()函数,包括其参数和返回值。find()函数用于在给定范围内查找指定值的第一个出现位置。如果找到,返回该位置的迭代器;否则,返回范围结束的迭代器。文章还提到,当应用于自定义类型时,需要重载'=='运算符。并通过一个班级成绩表的例子展示了find()函数的实际运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

find()

参考官方网址:http://www.cplusplus.com/reference/algorithm/find/?kw=find

格式:

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);

 解释:first、last分别是搜索的起始地址,在这个空间中寻找与val相等的对象,如果找到就返回这个相等对象的地址,如果没有就返回last。如果存在多个相等的对象,只返回第一个的地址。有时候我们会想知道查找到的结果位于数组的第几个,这时候我们可以将返回来的地址减去数组的首地址。

例子:

// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector

int main () {
  // using std::find with array and pointer:
  int myints[] = { 10, 20, 30, 40 ,30};
  int * p;

  p = std::find (myints, myints+5, 30);
  if (p != myints+5)
    std::cout << "数组下标是"<< (p - myints)<<"\nElement found in myints: " << *p << '\n';
  else
    std::cout << "Element not found in myints\n";

  // using std::find with vector and iterator:
  std::vector<int> myvector (myints,myints+4);
  std::vector<int>::iterator it;

  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end())
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myvector\n";

  return 0;
}

如果只有这么简单,那么这篇博文也就没有什么参考价值了。先来看一下源码:

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 example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector
using namespace std;

struct student{
	string name;
	float total;
	float chinese;
	float english;
	float math;
};

bool operator== (student student_1,string str){
	return student_1.name == str;
}

int main(){
	vector<student> my_Class;
	student zhangsan = {"张三",230,96,63,65};
	my_Class.push_back(zhangsan);
	student lisi = {"李四",230,97,75,85};
	my_Class.push_back(lisi);
	student wangwu = {"王五",240,75,67,65};
	my_Class.push_back(wangwu);
	student songliu = {"宋六",240,96,63,65};
	my_Class.push_back(songliu);
	student zhaoqi = {"赵七",240,96,63,66};	
	my_Class.push_back(zhaoqi);
	
	vector<student>::iterator p = find(my_Class.begin(),my_Class.end(),"王五");
	
	cout << p->name << "\n总分:" <<  p->total
	     << "\n语文:" << p->chinese
		 << "\n英语:" << p->english
		 << "\n数学:" << p->math << endl; 
	
	return 0;
}

结果:

王五
总分:240
语文:75
英语:67
数学:65

 

神书-STL实现原理,对于强化数据结构-算法的程序员必备、必读书籍。The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值