簡體   English   中英

C++11 用復合鍵搜索 std::map

[英]C++11 search an std::map with composite key

我想在 std::map 上的搜索下方實現一個函數 (findn) 以查找元素。 但是在我的情況下,鍵是復合值,它是一個<int,int>

我如何在這里使用std::map.find

#include <iostream>
#include <map>
#include <utility>
#include <string>

using namespace std;

std::map<std::pair<int, int>, std::string> studentMap;

int insert(int i, int j, std::string name) {
        if( !studentMap.insert( std::make_pair ( std::make_pair(i,j), name)).second ) { 
                std::cout << "game not added" << std::endl;
        } else {
                std::cout << "game added" << std::endl;
        }
        return 0;
}

void findn(int i, int j) {
// how to find when we have composite key?
}

int main() {
        insert(1,1,"test");
        insert(1,1,"tes");
        insert(1,2,"test 2");

        std::cout << studentMap.size() << std::endl;
        findn(1,1);
}

這將完成以下工作:

auto it = mymap.find(std::make_pair(i,j));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM