every blog every motto: You will never know unless you try

0. 前言

对于判断指定元素是否在vector中小结

1. 正文

1.1 while

#include<iostream>
using namespace std;#include<vector>void test()
{vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);vector<int>::iterator itBegin = v.begin(); // 起始迭代器vector<int>::iterator itEnd = v.end(); // 结束迭代器while (itBegin != itEnd){cout << *itBegin << endl;if (*itBegin == 2){cout << "所查元素在其中" << endl;break;}itBegin++;}
}int main() {test();system("pause");}

1.2 for

#include<iostream>
using namespace std;#include<vector>void test()
{vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << endl;if (*it == 2){cout << "所查元素在其中" << endl;break;}}
}int main() {test();system("pause");}

1.3 find

1.3.1 内置数据类型查找

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>void test()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}// 查找是否有5这个元素vector<int>::iterator it;it = find(v.begin(), v.end(), 5);if (it == v.end())cout << "没有找到指定元素" << endl;elsecout << "找到指定元素" << endl;}int main() {test();system("pause");}

1.3.1自定义数据类型查找

需要重载“==”

案例一:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>// 自定义数据类型查找
class Person
{public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}// 重载==bool operator==(const Person& p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age){return true;}return false;}public:string m_Name;int m_Age;
};void test022()
{vector<Person> v;// 创建数据Person p1("aaa", 1);Person p2("bbb", 2);Person p3("ccc", 3);Person p4("ddd", 4);// 添加数据v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<Person>::iterator it = find(v.begin(), v.end(), p2);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到姓名:" << it->m_Name << " 年龄: " << it->m_Age << endl;}}int main()
{test022();system("pause");
}

案例二:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>// 自定义数据类型查找// 结构体
struct Point
{int x;int y;// 重载==bool operator==(const Point& p){if (this->x== p.x&&this->y == p.y){return true;}return false;}
};void test033()
{Point point;vector<Point>v; // 创建vectorfor (int i = 0; i < 4; i++){point.x = i;point.y = i + 1;v.push_back(point);}// findPoint p;p.x = 1;p.y = 1;vector<Point>::iterator it = find(v.begin(), v.end(), p);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到了" << endl;}// 遍历打印输出//for (vector<Point>::iterator it = v.begin(); it != v.end(); it++)//{//    cout << "x:" << it->x << " y: " << it->y << endl;//}
}
int main()
{test033();system("pause");
}

【C++】判断指定元素是否在vector中的若干种方法小结相关推荐

  1. Python列表(获取列表中指定元素的索引、获取列表中的多个元素、判断指定元素是否在列表中存在、列表元素的遍历、列表元素的增加操作、 列表元素的删除操作、列表元素的修改操作、列表元素的排序操作)

    1.获取列表中指定元素的索引 eg1:未指定索引范围查找索引 zyr=['憨憨','憨宝'] print(zyr.index('憨宝')) print(zyr[1]) eg2:在指定索引范围内查找元素 ...

  2. ubuntu编写python脚本_python在ubuntu中的几种方法(小结)

    通过ubuntu官方的apt工具包安装 通过PPA(Personal Package Archive) 的apt工具包安装 通过编译python源代码安装 通过ubuntu官方的apt工具包安装 安装 ...

  3. Python入门--列表元素的判断及遍历,判断指定元素在列表中是否存在,列表元素的遍历,

    #列表中元素查询操作 #判断指定元素在列表中是否存在 #元素 in 列表名 #元素 not in 列表名 #列表元素的遍历(遍历:就是将列表中的元素依次输出) #for 迭代变量 in 列表名:for ...

  4. 删除数组中某个指定元素或删除数组中某个对象元素

    ES6--删除数组中某个指定元素或删除数组中某个对象 1.删除数组中某个指定元素 2.删除数组中的某个对象 1.删除数组中某个指定元素 let index = this.array.indexOf(n ...

  5. MATLAB 保存imshow绘制图片到指定文件夹中的两种方法

    MATLAB 保存imshow绘制图片到指定文件夹中的两种方法 imwrite函数 imwrite(image,filename) imwrite(image,filename)是将图像数据image ...

  6. Android中的13种Drawable小结

    本节引言: 从本节开始我们来学习Android中绘图与动画中的一些基础知识,为我们进阶部分的自定义打下基础!而第一节我们来扣下Android中的Drawable!Android中给我们提供了多达13种 ...

  7. c++语言定义排序函数,关于C++中定义比较函数的三种方法小结

    C++编程优与Pascal的原因之一是C++中存在STL(标准模板库).STL存在很多有用的方法. C++模板库中的许多方法都需要相关参数有序,例如Sort().显然,如果你想对一个集合进行排序,你必 ...

  8. python字典取值_python 字典中取值的两种方法小结

    python 字典中取值的两种方法小结 如下所示: a={'name':'tony','sex':'male'} 获得name的值的方式有两种 print a['name'],type(a['name ...

  9. python取整的几种方法,Python中取整的几种方法小结

    Python中取整的几种方法小结 前言 对每位程序员来说,在编程过程中数据处理是不可避免的,很多时候都需要根据需求把获取到的数据进行处理,取整则是最基本的数据处理.取整的方式则包括向下取整.四舍五入. ...

  10. html元素隐藏js 控制,JS控制HTML元素的显示和隐藏的两种方法

    JS控制HTML元素的显示和隐藏的两种方法 利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一 ...

最新文章

  1. Windows Server 2012 r2 显示计算机图标
  2. 奥鹏C语言专科在线作业答案,奥鹏13春电子科大《C语言(专科)》在线作业3答案...
  3. STM32 之五 Core Coupled Memory(CCM)内存
  4. c++调用python接口作用是_利用Boost::Python实现C++调用python接口
  5. 2019全球程序员薪酬报告:软件开发比机器学习抢手!40岁后收入下滑
  6. 五一小长假|不得不说的一些话
  7. 我为什么想并且要学习Scala
  8. 网页qq邮箱链接html,使用QQ邮箱打开网页上的电子邮件链接
  9. shell解析HTML
  10. 第一篇,嵌入式ubantu系统安装及常见ubantu命令,C语言数据类型和变量的定义
  11. 京东开源组件库NutUI 3.1 正式发布:开启多端开发之路
  12. Android控件之Button
  13. php+laravel 扫码二维码签到
  14. Unity Scroll View在Clamped模式下无法移动
  15. 具体分析contrex-A9的汇编代码__switch_to(进程切换)
  16. linux 文件格式elf,linux ELF 文件格式 | ZION
  17. C++课程基础语法小结
  18. CImage图像旋转与缩放
  19. Streams AQ: qmn coordinator waiting for slave to start
  20. 使用C#中的CultureInfo类实现全球化

热门文章

  1. docker(podman)命令参考
  2. -bash: cd: /.ssh: 没有那个文件或目录
  3. java文件中注释出现乱码解决办法
  4. 快速列出所有字段_【小麦课堂】快速查询明细数据的操作
  5. HighCharts:PlotLine的label文字不显示
  6. php 图片水印删除,PHP图片水印
  7. Halcon和Opencv区别
  8. Java -Dfile.encoding=UTF-8 出现乱码问题原因分析
  9. 何川L3管理课_模块5_给评价
  10. 机器学习课程笔记【十一】- 因子分析