1. 程式人生 > >map按value值查詢——find_if的使用

map按value值查詢——find_if的使用

最近是經常使用stl中的map,於是就想記一些關於map的東西。這一篇中會講到map按照value值查詢的方法,就是find_if函式。

大家都知道在map中,排序是按照key值排的,map自帶的find方法也是按著key值查詢的,這在某些情況下可能會遇到一些麻煩。

譬如,map<int, char*> m_str中,傳入一個char*需要查詢在m_str中是否存在這個字串,當然你大可以使用iterator遍歷一些map,

如果你堅持這麼做,那就可以直接關閉網頁了。

    1.先來看看find_if的原型:


template <class InputIterator, class Predicate> 
InputIterator find_if(InputIterator first, InputIterator last,Predicate pred) 
{ 
       while (first != last && !pred(*first)) ++first; 
       return first; 
} 

 find_if是一個模板函式,接受兩個資料型別:InputItearator迭代器,Predicate用於比較數值的函式或者函式物件(仿函式)。find_if對迭代器要求很低,只需要它支援自增操作即可。當前遍歷到的記錄符合條件與否,判斷標準就是使得pred()為真。至此可能還有些不是很明瞭,下面舉幾個例子實際操練下的它的用法。注意觀察第三個引數pred。

 2.find_if在std::map查詢時的應用

 假如我們有個map物件是這麼宣告的:


std::map<int, std::string> mymap;
mymap.insert(std::make_pair(20, "USA"));
mymap.insert(std::make_pair(10, "CHINA")); 
mymap.insert(std::make_pair(30, "English"));
mymap.insert(std::make_pair(40, "Hongkong"));

插入值後我們想得到值為”english”的這條記錄,要怎樣寫程式呢?下面是個範例參考下:
 #include <map>


#include <string>
#include <algorithm>
class map_value_finder
{
public:
       map_value_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}
       bool operator ()(const std::map<int, std::string>::value_type &pair)
       {
            return pair.second == m_s_cmp_string;
       }
private:
        const std::string &m_s_cmp_string;                    
};
 
int main()
{
    std::map<int, std::string> my_map;
    my_map.insert(std::make_pair(10, "china"));
    my_map.insert(std::make_pair(20, "usa"));
    my_map.insert(std::make_pair(30, "english"));
    my_map.insert(std::make_pair(40, "hongkong"));    
    
    std::map<int, std::string>::iterator it = my_map.end();
    it = std::find_if(my_map.begin(), my_map.end(), map_value_finder("English"));
    if (it == my_map.end())
       printf("not found\n");       
    else
       printf("found key:%d value:%s\n", it->first, it->second.c_str());
       
    return 0;        
}

 class map_finder即用於比較的函式物件,它的核心就是過載的()運算子。因為每個容器迭代器的*運算子得到的結果都是該容器的value_type值,所以該運算子的形參就是map迭代器指向的value_type型別的引用。而map的value_type到底是什麼型別,就得看下STL的原始碼是如何定義的。


template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map
{
public:
typedef Key key_type;
typedef pair<const Key, T> value_type;
......
};

從上面的定義可以看出,map的value_type是std::pair<const Key, t>型別,它的first值就是關鍵字,second值儲存map的值域。
3.find_if在vector中的應用與上面的類似,就不舉例子了。 區別就是vecotor的value_type和map的value_type不一樣,想大家應該是明白的。

相關推薦

mapvalue查詢——find_if的使用

最近是經常使用stl中的map,於是就想記一些關於map的東西。這一篇中會講到map按照value值查詢的方法,就是find_if函式。 大家都知道在map中,排序是按照key值排的,map自帶的find方法也是按著key值查詢的,這在某些情況下可能會遇到一些麻煩。 譬

c++ mapvalue排序

map預設是按key值從小到大排序的,先改為按value排序。 基本思路就是:想直接用sort排序是做不到的,sort只支援陣列、vetctor等的排序,所以我們可以先把map裝進pair裡,然後再放

JAVA中Mapvalue進行逆序排序

RT。程式碼如下。 //按照頻率的逆序進行排序 Set set=map.entrySet(); Map.Entry[] entries=(Map.Entry[

c++中mapvalue排序

int cmp(const pair<string, int>& x, const pair<string, int>& y)   //定義sort排序方式 {     return x.second > y.second;}

Mapvalue排序

大家都知道,在java中的集合Map中按鍵值key排序比較簡單,只需引用集合TreeMap即可,可是怎樣實現按value值排序呢?下面我們來測試一下: public class TestHashMap { public static void main(String[]

C++MapValue排序

turn con pre clas using code ios first esp #include <iostream> #include <map> #include <algorithm> #include <string

Map根據value排序

Map的key是機場名字,value 是流量。根據流量倒序排序 /** * 使用 Map按value進行排序 * @param map * @return */ public static Map<String, Integer>

java8 實現mapvalue排序

import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.stream.Collector; import java.util.stream.C

mapvalue找key的兩種方法

map用value值找key的兩種方法 Map中是一個key有且只有一個value. 但是一個value可以對應多個key值. 只用用特殊方法才能用value值來找key,以下就是用value值找key的兩種方法 public static void main(Str

Java中MapValue排序

前言 Map是鍵值對的集合介面,它的實現類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。 TreeMap 基於紅黑樹(Red-Black tree)的 NavigableMap 實現,該對映根據其鍵的自然順序進行排序,或者

根據Mapvalue進行排序

Map的實現類中,有順序的是LinkedHashMap和TreeMap。LinkedHashMap是按照插入的順序排序,TreeMap根據key自然順序排序,或者傳入一個Comparator。 package collaborativeFilterin

C++中mapvalue排序

我們知道C++ STL中的map是以key排序的。 int main() { map<int, int> iMap; iMap[1] = 20; iMap[2]

el表示式中動態獲取mapvalue

先說下業務場景,查詢介面,欄位通過迴圈list展示出來,但是其中有一個欄位為最後操作人的id,需要轉換成其對應的名稱。 頁面程式碼: <c:forEach items="${servi

java對MapValue進行排序

List <Map.Entry<String,Integer>> mapList = null; mapList=new ArrayList<Map.Entry<String,Integer>>(myMap.entry

C++中 使用陣列作為map容器VAlue的解決方法

1)是用Vector容器代替陣列 2)使用陣列指標(需要注意區域性變數的問題,指標是否需要用new建立) int red [ 3 ] = { 1 , 0 , 0 }; int green [

java map按照value來比較大小並且返回最終結果

package com.zuidaima.util; import java.util.Comparator; import java.util.HashMap; import java.u

Map按照Value進行排序

1  TreeMap按照value進行排序 public class Testing { public static void main(String[] args) { HashMap<String,Double> map = ne

python 實現txt檔案 value排序從高到低

with open('sort.txt','w+') as w:      while True:           sorted_lines=sorted(open('1.txt'), key=lambda s: s.split()[4],reverse=1)      

大日誌檔案中如何統計單詞個數?及mapvalue排序lambda表示式版

package com.nys.countwords; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections;

實現Comparator接口和Comparable接口,以及Mapvalue排序 ,map遍歷

package oid ring add arraylist todo [] tint map() 繼承Comparator接口,重寫compare()方法 import java.util.ArrayList; import java.util.Arrays;