本文介绍了std :: map部分匹配的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std ::地图,我想使用一个子字符串来搜索一个键。示例

  #include< iostream> 
#include< map>
#include< string>
using namespace std;

typedef std :: map< std :: string,std :: string> TStrStrMap;
typedef std :: pair< std :: string,std :: string> TStrStrPair;

int main(int argc,char * argv [])
{
TStrStrMap tMap;

tMap.insert(TStrStrPair(John,AA));
tMap.insert(TStrStrPair(Mary,BBB));
tMap.insert(TStrStrPair(Mother,A));
tMap.insert(TStrStrPair(Marlon,C));


return 0;
}



我想搜索包含子字符串Marl 马龙。是否可以?怎么样?



编辑:没有boost库!

解决方案

include< iostream>
#include< map>
#include< string>
#include< algorithm>
using namespace std;

typedef map< string,string> TStrStrMap;
typedef pair< string,string> TStrStrPair;

TStrStrMap :: const_iterator FindPrefix(const TStrStrMap& map,const string& search_for){
TStrStrMap :: const_iterator i = map.lower_bound(search_for);
if(i!= map.end()){
const string& key = i-> first;
if(key.compare(0,search_for.size(),search_for)== 0)//真的是一个前缀吗?
return i;
}
return map.end();
}

void Test(const TStrStrMap& map,const string& search_for){
cout<寻找;
auto i = FindPrefix(map,search_for);
if(i!= map.end())
cout<< '\t'<< i - >第一<< ,< i - >第二;
cout<< endl;
}

int main(int argc,char * argv [])
{
TStrStrMap tMap;

tMap.insert(TStrStrPair(John,AA));
tMap.insert(TStrStrPair(Mary,BBB));
tMap.insert(TStrStrPair(Mother,A));
tMap.insert(TStrStrPair(Marlon,C));

Test(tMap,Marl);
Test(tMap,Mo);
测试(tMap,ther);
Test(tMap,Mad);
Test(tMap,Mom);
Test(tMap,Perr);
Test(tMap,Jo);

return 0;
}

打印:

  Marl Marlon,C 
Mo母亲,A
ther
Mad
妈妈
Perr
Jo John, AA


I have an std::map and I want to search for a key using a substring. For exampe

#include <iostream>
#include <map>
#include <string>
using namespace std;

typedef std::map<std::string, std::string> TStrStrMap;
typedef std::pair<std::string, std::string> TStrStrPair;

int main(int argc, char *argv[])
{
        TStrStrMap tMap;

        tMap.insert(TStrStrPair("John", "AA"));
        tMap.insert(TStrStrPair("Mary", "BBB"));
        tMap.insert(TStrStrPair("Mother", "A"));
        tMap.insert(TStrStrPair("Marlon", "C"));


        return 0;
}

I want to search for the position that holds the substring "Marl" and not "Marlon". Is it possible? How?

EDIT: no boost libraries!

解决方案

You can't efficiently search for substring, but you can for prefix:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

typedef map<string, string> TStrStrMap;
typedef pair<string, string> TStrStrPair;

TStrStrMap::const_iterator FindPrefix(const TStrStrMap& map, const string& search_for) {
    TStrStrMap::const_iterator i = map.lower_bound(search_for);
    if (i != map.end()) {
        const string& key = i->first;
        if (key.compare(0, search_for.size(), search_for) == 0) // Really a prefix?
            return i;
    }
    return map.end();
}

void Test(const TStrStrMap& map, const string& search_for) {
    cout << search_for;
    auto i = FindPrefix(map, search_for);
    if (i != map.end())
        cout << '\t' << i->first << ", " << i->second;
    cout << endl;
}

int main(int argc, char *argv[])
{
    TStrStrMap tMap;

    tMap.insert(TStrStrPair("John", "AA"));
    tMap.insert(TStrStrPair("Mary", "BBB"));
    tMap.insert(TStrStrPair("Mother", "A"));
    tMap.insert(TStrStrPair("Marlon", "C"));

    Test(tMap, "Marl");
    Test(tMap, "Mo");
    Test(tMap, "ther");
    Test(tMap, "Mad");
    Test(tMap, "Mom");
    Test(tMap, "Perr");
    Test(tMap, "Jo");

    return 0;
}

This prints:

Marl    Marlon, C
Mo      Mother, A
ther
Mad
Mom
Perr
Jo      John, AA

这篇关于std :: map部分匹配的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 18:40