vector、map 判断某元素是否存在、查找指定元素
一、vector
1.判断某元素是否存在
vector<string> vStr;
int nRet = std::count(vStr.begin(), vStr.end(), "xiaochun");//判断vector中是否有 "xiaochun" 这个元素
2.查找某个元素
方法一:
自己写循环遍历
方法二:
vector<string> vec;
vector<string>::iterator iter;
string gpcode="SZ000001";
iter = find(vec.begin(), vec.end(), gpcode);
if(iter != vec.end())
{
//vec中存在"SZ000001"
}
else
{
//没找到
}
注意:
如果vector中保存的是自定义类型(结构体/类),则需要为该类型重载==操作符。再用find
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm> //是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模板函数。
using namespace std;
class DemoStruct
{
public:
string gpcode;
int ymd;
vector<int> vec;
DemoStruct()
{
ymd = 20170707;
gpcode = "";
}
bool operator == (const DemoStruct & obj) const //重载 “==” 操作符,函数最后的 const 别忘了,否则会报错。(详见:http://www.cnblogs.com/SZxiaochun/p/7731900.html)
{
return ymd == obj.ymd && gpcode == obj.gpcode; //具体匹配条件,可以自己设定
}
};
int main()
{
vector<DemoStruct> vec_struct;
DemoStruct demo;
demo.gpcode = "SZ000001";
demo.ymd = 20170707;
demo.vec.push_back(0);
vec_struct.push_back(demo);
DemoStruct tmpdemo;
tmpdemo.gpcode = "SZ000001";
tmpdemo.ymd = 20170707;
vector<DemoStruct>::iterator iter;
iter = find(vec_struct.begin(), vec_struct.end(), tmpdemo);
if (iter != vec_struct.end())
{
printf("%s","find it");
}
return 0;
}
注意:
如果vector中保存的是自定义类型(结构体/类),且不方便重载==操作符,可以用 find_if
struct StockInfo
{
short shtSetCode;
string sCode;
};
int main()
{
vector<StockInfo> vStock;
StockInfo stockInfo1;
stockInfo1.shtSetCode = 0;
stockInfo1.sCode = "000002";
vStock.push_back(stockInfo1);
StockInfo stockInfo2;
stockInfo2.shtSetCode = 0;
stockInfo2.sCode = "000003";
vStock.push_back(stockInfo2);
StockInfo stockInfo3;
stockInfo3.shtSetCode = 0;
stockInfo3.sCode = "000004";
vStock.push_back(stockInfo3);
//使用 find_if 自定义比较条件
if (find_if(vStock.begin(), vStock.end(), [&stockInfo1](StockInfo const & obj) {return obj.shtSetCode == stockInfo1.shtSetCode && obj.sCode == stockInfo1.sCode; }) != vStock.end()) //如果编译报错 “‘this’实参时丢弃了类型限定”,就把 & obj 前面的const去掉
{
cout << "111" << endl;
}
return 0;
}
//这种场景,还可以使用set
struct StockCmp //自定义 set 第二个参数
{
bool operator()(const StockInfo &lStock, const StockInfo &rStock) const
{
if (lStock.iMarket < rStock.iMarket)
{
return true;
}
else if (lStock.iMarket == rStock.iMarket)
{
if (lStock.sCode < rStock.sCode)
{
return true;
}
}
return false;
}
};
set<StockInfo, StockCmp> m_setTmp;
StockInfo stStockInfo;
stStockInfo.iMarket = 0;
stStockInfo.sCode = "000001";
m_setTmp.insert(stStockInfo);
stStockInfo.iMarket = 0;
stStockInfo.sCode = "000002";
m_setTmp.insert(stStockInfo);
if ( (auto iter = m_setTmp.find(stStockInfo)) != m_setTmp.end())
{
//找到了
}
二、map
1.判断某key是否存在
map<int, string> mapDemo;
int nRet = mapDemo.count(100);//判断mapDemo中是否有 key = 100 的元素
2.查找某个key
map<int, string>::iterator iter = mapDemo.find(100);
if (iter != m_Int.end())
{
//找到了
}
else
{
//没找到
}
注意:
如果map中的 Key 是自定义类型(结构体/类),则需要自定义map第三个参数。再用find
#include <stdio.h>
#include <iostream>
#include <map>
#include <string>
#include <algorithm> //是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模板函数。
using namespace std;
struct StockInfo // Key 是自定义类型
{
int iMarket;
string sCode;
StockInfo():
iMarket(0)
{
}
};
struct StockCmp //自定义 map 第三个参数
{
bool operator()(const StockInfo &lStock, const StockInfo &rStock) const
{
if (lStock.iMarket < rStock.iMarket)
{
return true;
}
else if (lStock.iMarket == rStock.iMarket)
{
if (lStock.sCode < rStock.sCode)
{
return true;
}
}
return false;
}
};
int main()
{
map<StockInfo, int, StockCmp> mapTestSort;
StockInfo stockInfo3;
stockInfo3.iMarket = 1;
stockInfo3.sCode = "600000";
mapTestSort.insert(make_pair(stockInfo3,2));
StockInfo stockInfo2;
stockInfo2.iMarket = 0;
stockInfo2.sCode = "000002";
mapTestSort.insert(make_pair(stockInfo2,3));
StockInfo stockInfo1;
stockInfo1.iMarket = 0;
stockInfo1.sCode = "000001";
mapTestSort.insert(make_pair(stockInfo1,1));
cout << mapTestSort[stockInfo1] << endl;
map<StockInfo, int, StockCmp>::iterator iter = mapTestSort.find(stockInfo1);
if (iter != mapTestSort.end())
{
cout << iter->second << endl;
}
return 0;
}
vector、map 判断某元素是否存在、查找指定元素的更多相关文章
- vector某元素是否存在、查找指定元素 、去重
vector.map 判断某元素是否存在.查找指定元素 [C++]判断元素是否在vector中,对vector去重,两个vector求交集.并集 PS:注意重载
- 学underscore在数组中查找指定元素
前言 在开发中,我们经常会遇到在数组中查找指定元素的需求,可能大家觉得这个需求过于简单,然而如何优雅的去实现一个 findIndex 和 findLastIndex.indexOf 和 lastInd ...
- Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素
ylbtech-Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素 1.返回顶部 1. Java 实例 - 在数组中查找指定元素 Java 实例 以下实例 ...
- JS判断Array数组中是否包含指定元素
1.调用方式: var arr=["a","b"]; alert(arr.in_array("a")) 2.JS判断数组是否包含指定元素方法 ...
- C++ vector 删除一个指定元素 和 find 一个指定元素以及遍历删除、 map遍历删除元素和删除find到的元素
vector: 1.delete element 转载:http://www.cnblogs.com/xudong-bupt/p/3522457.html #include <vector> ...
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- index() 方法返回指定元素相对于其他指定元素的 index 位置。
- jQuery使用(四):DOM操作之查找兄弟元素和父级元素
查找兄弟元素 向下查找兄弟元素 next() nextAll() nextUntil() 向上查找兄弟元素 prev() prevAll() prevUntil() 查找所有兄弟元素 siblings ...
- JQuery中查找父元素,子元素,追加元素,插入元素和删除元素 及其他常用方法
Jquery之所以强大,和其在获取对象时使用与css选择器兼容的语法有很大关系.而且它还兼容了CSS3的选择器,而且多出了不少. 所以jQuery的选择器也就变得很多很强大.就最基本的有以下四个: $ ...
随机推荐
- spring boot中的jave注解学习
在spring中,不仅框架作者会使用java注解,开发者也常使用. 可以随手给个例子:在org.springframework.boot.autoconfigure.jdbc.DataSourcePr ...
- 第五天 py if使用
if 的结果缩进 用个Tab 缩进四个空格就好了
- web开发——入门篇(上)
作为一名IT届的后生,当初也经历过懵懂无知的实习期,对那种无力感深有体会.在这,希望能用我这几年的开发经验,让各位即将踏入或者刚刚踏入web开发领域的新人们少走些弯路.鉴于这是入门篇,下面我就从零为大 ...
- day16:内置函数二
1,大作业,yield 返回之后可以对数据进行处理了就,注意函数的解耦,每一个小功能写成一个函数,增强可读性,写之前自己要先把整体功能分块,先做什么,在做什么 # 现在需要对这个员工信息文件进行增删改 ...
- pandas 2
============== sdf={'rkey':[1,2,3,2],'name':['rkey1','rkey2','rkey3','rkey4']}sdf2={'lkey':[1,2,3],' ...
- Spring mvc下Ajax获取JSON对象问题 406错误
spring 通过@ResponseBody标签返回JSON数据的方法都报406错: Failed to load resource: the server responded with a stat ...
- springMVC(五): 通过 HandlerMapping 获取 HandlerExecutionChain
请求具体过程 一.HandlerMapping Interface to be implemented by objects that define a mapping between request ...
- hibernate重要知识点总结
一.使用注解方式-----实体和表之间的映射 配置spring的applicationContext.xml文件: <bean id="sessionFactory" cla ...
- 把ResNet-L152模型的ckpt文件转化为pb文件
import tensorflow as tf from tensorflow.python.tools import freeze_graph #os.environ['CUDA_VISIBLE_D ...
- 十一、无事勿扰,有事通知(2)——KVO
概述 Key-Value-Observe,简称KVO,和上节介绍的Notification师出同门,主要目的都是为了实现观察者模式. 虽说是同门师兄弟,但是各自精通的技艺却是各不相同的. 不像Noti ...