c++ map: 根据value逆向查找key

#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

class finder
{
public:
    finder(const std::string &cmp_string) :s_(cmp_string){}
    bool operator ()(const std::map<int, std::string>::value_type &item)
    {
        return item.second == s_;
    }
private:
    const std::string &s_;
};



int main()
{
    map<uint32_t, string> t;
	t.insert(std::make_pair(1, "cpu_syscall_pid"));
	t.insert(std::make_pair(2, "cpu_syscall_cpu"));
	t.insert(std::make_pair(3, "cpu_syscall_sys"));
	t.insert(std::make_pair(4, "cpu_contxt_pid"));

	int n = 0;
	auto it = std::find_if(t.begin(), t.end(), finder("cpu_syscall_pid"));
    if (it != t.end())
    {
        n = (*it).first;
    }

	cout << "n:" << n << endl;
	return 0;

}

原文链接: https://www.cnblogs.com/muahao/p/8854817.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ map: 根据value逆向查找key

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/272436

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午10:42
下一篇 2023年2月14日 下午10:42

相关推荐