一、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 判断某元素是否存在、查找指定元素的更多相关文章

  1. vector某元素是否存在、查找指定元素 、去重

    vector.map 判断某元素是否存在.查找指定元素 [C++]判断元素是否在vector中,对vector去重,两个vector求交集.并集 PS:注意重载

  2. 学underscore在数组中查找指定元素

    前言 在开发中,我们经常会遇到在数组中查找指定元素的需求,可能大家觉得这个需求过于简单,然而如何优雅的去实现一个 findIndex 和 findLastIndex.indexOf 和 lastInd ...

  3. Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素

    ylbtech-Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素 1.返回顶部 1. Java 实例 - 在数组中查找指定元素  Java 实例 以下实例 ...

  4. JS判断Array数组中是否包含指定元素

    1.调用方式: var arr=["a","b"]; alert(arr.in_array("a")) 2.JS判断数组是否包含指定元素方法 ...

  5. C++ vector 删除一个指定元素 和 find 一个指定元素以及遍历删除、 map遍历删除元素和删除find到的元素

    vector: 1.delete element 转载:http://www.cnblogs.com/xudong-bupt/p/3522457.html #include <vector> ...

  6. 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。

    // ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  7. index() 方法返回指定元素相对于其他指定元素的 index 位置。

  8. jQuery使用(四):DOM操作之查找兄弟元素和父级元素

    查找兄弟元素 向下查找兄弟元素 next() nextAll() nextUntil() 向上查找兄弟元素 prev() prevAll() prevUntil() 查找所有兄弟元素 siblings ...

  9. JQuery中查找父元素,子元素,追加元素,插入元素和删除元素 及其他常用方法

    Jquery之所以强大,和其在获取对象时使用与css选择器兼容的语法有很大关系.而且它还兼容了CSS3的选择器,而且多出了不少. 所以jQuery的选择器也就变得很多很强大.就最基本的有以下四个: $ ...

随机推荐

  1. postgresql数据库和mysql数据库的对比分析

    1.Posgresql是进程模式,多进程,单线程,类似的还有Oracle.而MYSQL采用的是线程模式,单进程,多线程,对此,大家在运行数据库的时候可以查看任务管理器,SQL Server也是如此. ...

  2. js获取当前日期方法(YYYY-MM-DD格式)

      var myDate = new Date(); var time = myDate.toLocaleDateString().split('/').join('-');将1970/08/08转化 ...

  3. 谷歌浏览器(Chrome)禁止浏览器缓存 设置

    在开发项目期间用谷歌浏览器调试,他总是缓存我的css样式这个很气人啊,后经过摸索找到了方法,如下 先F12或者shift+ctrl+j 打开调试者工具,在找Network这个tab按钮,点击进入,勾选 ...

  4. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

  5. 使用js对WGS-84 ,GCJ-02与BD-09的坐标进行转换

    获取到经纬度在用百度地图进行定位时,却发现行驶轨迹的路线定到海里面去了.从网上查阅,知道此方法. 出处:https://www.jianshu.com/p/53f00ba897f7 一.在进行地图开发 ...

  6. asp.net NPOI导出xlsx格式文件,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

    NPOI导出xlsx格式文件,会出现如下情况: 点击“是”: 导出代码如下: /// <summary> /// 将datatable数据写入excel并下载 /// </summa ...

  7. Viewer.js插件浏览图片

    https://www.jianshu.com/p/e3350aa1b0d0 Viewer.js插件浏览图片 Viewer.js插件浏览图片 Viewer.js插件浏览图片

  8. python数据结构-如何在列表、字典、集合中根据条件筛选数据

    如何在列表.字典.集合中根据条件筛选数据 问题举例: 过滤列表[1, 2, 5, -1, 9, 10]中的负数 筛选字典{“zhangsan”:97, "lisi":80, &qu ...

  9. torch随机数 manual_seed

    import torch seed = 2018 torch.manual_seed(seed) torch.cuda.manual_seed(seed) a=torch.rand([1,5]) # ...

  10. NOIP2012借教室

    题目描述 Description 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海 ...