📜  使用C ++中的std :: map函数在地图中搜索

📅  最后修改于: 2021-06-26 00:56:47             🧑  作者: Mango

通常,使用map stl容器的主要目的是进行有效的搜索操作和排序后的订单检索。当地图存储键值对时,所有搜索操作都花费“ O(log(n)) ”时间(n是地图的大小)。 C++语言中存在不同类型的搜索功能,每种功能都有不同的功能。在竞争性编程的情况下,这在需要搜索操作且比其他容器性能更好的情况下很有用。一些搜索操作将在下面讨论。

std :: map :: find()

find()用于搜索键值对,并在其参数中接受“键”以找到它。如果找到元素,则此函数返回指向元素的指针,否则返回指向map的最后位置的指针,即“ map.end() ”。

// C++ code to demonstrate the working of find()
  
#include
#include // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map< char, int > mp;
      
    // declaring iterators
    map::iterator it ;
    map::iterator it1 ;
      
    // inserting values 
    mp['a']=5;
    mp['b']=10;
    mp['c']=15;
    mp['d']=20;
    mp['e']=30;
      
    // using find() to search for 'b' 
    // key found
    // "it" has address of key value pair.
    it = mp.find('b');
      
    if(it == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair present : " 
          << it->first << "->" << it->second ;
      
    cout << endl ;
      
    // using find() to search for 'm' 
    // key not found
    // "it1" has address of end of map.
    it1 = mp.find('m');
      
    if(it1 == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair present : " 
            << it1->first << "->" << it1->second ;
      
}

输出:

Key-value pair present : b->10
Key-value pair not present in map

std :: map :: lower_bound()

lower_bound()也用于搜索操作,但有时也返回有效的键值对,即使map中不存在该键值对。 lower_bound()返回键值对的地址,如果map中存在一个,则返回该地址到大于其参数中提到的键的最小键。如果所有键都小于要找到的键,则它指向“ map.end()”

// C++ code to demonstrate the working of lower_bound()
  
#include
#include // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map< char, int > mp;
      
    // declaring iterators
    map::iterator it ;
    map::iterator it1 ;
    map::iterator it2 ;
      
      
    // inserting values 
    mp['a']=5;
    mp['b']=10;
    mp['c']=15;
    mp['h']=20;
    mp['k']=30;
      
    // using lower_bound() to search for 'b' 
    // key found
    // "it" has address of key value pair.
    it = mp.lower_bound('b');
      
    if(it == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : " 
            << it->first << "->" << it->second ;
      
    cout << endl ;
      
    // using lower_bound() to search for 'd' 
    // key not found
    // "it1" has address of next greater key.
    // key - 'h'
    it1 = mp.lower_bound('d');
      
    if(it1 == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : " 
            << it1->first << "->" << it1->second ;
      
    cout << endl;
      
    // using lower_bound() to search for 'p' 
    // key not found
    // "it2" has address of next greater key.
    // all keys are smaller, hence returns mp.end()
    it2 = mp.lower_bound('p');
      
    if(it2 == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : "
            << it2->first << "->" << it2->second ;
      
}

输出:

Key-value pair returned : b->10
Key-value pair returned : h->20
Key-value pair not present in map

std :: map :: upper_bound()

upper_bound()也用于搜索操作,并且从不返回searched的键值对如果映射中存在一个键,则upper_bound()返回键值对的地址,该地址恰好位于搜索到的键旁边。如果所有键都小于要找到的键,则它指向“ map.end()”

// C++ code to demonstrate the working of upper_bound()
  
#include
#include // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map< char, int > mp;
      
    // declaring iterators
    map::iterator it ;
    map::iterator it1 ;
    map::iterator it2 ;
      
      
    // inserting values 
    mp['a']=5;
    mp['b']=10;
    mp['c']=15;
    mp['h']=20;
    mp['k']=30;
      
    // using upper_bound() to search for 'b' 
    // key found
    // "it" has address of key value pair next to 'b' i.e 'c'.
    it = mp.upper_bound('b');
      
    if(it == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : " 
            << it->first << "->" << it->second ;
      
    cout << endl ;
      
    // using upper_bound() to search for 'd' 
    // key not found
    // "it1" has address of next greater key.
    // key - 'h'
    it1 = mp.upper_bound('d');
      
    if(it1 == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : " 
        << it1->first << "->" << it1->second ;
      
    cout << endl;
      
    // using upper_bound() to search for 'p' 
    // key not found
    // "it2" has address of next greater key.
    // all keys are smaller, hence returns mp.end()
    it2 = mp.upper_bound('p');
      
    if(it2 == mp.end())
        cout << "Key-value pair not present in map" ;
    else
        cout << "Key-value pair returned : " 
            << it2->first << "->" << it2->second ;
      
}

输出:

Key-value pair returned : c->15
Key-value pair returned : h->20
Key-value pair not present in map

std :: map ::等于范围

在地图中搜索的又一个函数,它返回包含搜索到的键的范围。由于map包含唯一元素,因此返回的范围最多包含1个元素。此函数返回一个对的迭代器,其第一个元素指向搜索的键对的lower_bound(),第二个元素指向搜索的键的upper_bound()。如果key不存在,则第一个元素和第二个元素都指向下一个更大的元素。

// C++ code to demonstrate the working of equal_range()
  
#include
#include // for map operations
using namespace std;
  
int main()
{
    // declaring map
    // of char and int
    map< char, int > mp;
      
    // declaring iterators
    pair::iterator, map::iterator> it;
      
      
    // inserting values 
    mp['a']=5;
    mp['b']=10;
    mp['c']=15;
    mp['h']=20;
    mp['k']=30;
      
    // using equal_range() to search for 'b' 
    // key found
    // 1st element of "it" has the address to lower_bound (b)
    // 2nd element of "it" has the address to upper_bound (c) 
    it = mp.equal_range('b');
      
    cout << "The lower_bound of key is : " 
        << it.first -> first << "->" << it.first -> second;
    cout << endl;
      
    cout << "The upper_bound of key is : " 
        << it.second -> first << "->" << it.second -> second;
      
    cout << endl << endl ;
      
    // using equal_range() to search for 'd' 
    // key not found
    // Both elements of it point to next greater key
    // key - 'h'
    it = mp.equal_range('d');
      
    cout << "The lower_bound of key is : " 
        << it.first -> first << "->" << it.first -> second;
    cout << endl;
      
    cout << "The upper_bound of key is : " 
        << it.second -> first << "->" << it.second -> second;
      
      
}

输出:

The lower_bound of key is : b->10
The upper_bound of key is : c->15

The lower_bound of key is : h->20
The upper_bound of key is : h->20
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”