一、vector
vector 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> |
| |
| class DemoStruct |
| { |
| public : |
| string gpcode; |
| int ymd; |
| vector< int > vec; |
| DemoStruct() |
| { |
| ymd = 20170707; |
| |
| gpcode = "" ; |
| } |
| |
| bool operator == ( const DemoStruct & obj) const |
| { |
| 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; |
| } |
| |
二、map
1.判断某元素是否存在
```
map< int , string> mapDemo;
| bool bRet = mapDemo.count(100) |
| |
| 2.查找某个元素 |
| ``` |
| map< int , string>::iterator iter = mapDemo.find(100) |
| if (iter != m_Int.end()) |
| { |
| |
| } |
| else |
| { |
| |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2021-05-20 C语言常用的一些转换工具函数!
2021-05-20 C语言如何实现动态扩容的string
2021-05-20 当初数据结构与算法这样学,现在也不至于这样!!!
2021-05-20 回调函数原来这么容易理解