一、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; }