C++ STL中的Multimap find()函数

C++ STL中的Multimap find()函数

我们都知道,C++编程语言有许多内置函数可以帮助我们避免编写冗长的代码。其中一个函数是multimap find函数,它在C++编程语言的丰富库中,即标准模板库(STL)中可用。它将帮助我们返回一个迭代器,该迭代器指向或指向我们创建的长或小的multimap中包含数据遍历的键的位置。假设您具有知道所有基本数据值的不同目标。在这种情况下,您可以参考C++编程语言的丰富库中为我们提供的equal range函数,即标准模板库(STL)。

C++中的Multimap

// Here we are writing down the C++ programming language code to 
// demonstrate
// the concept of multimap find() in C++ STL(Standard Template Library)

#include 
#include 
#include 

using namespace std;
// The main driver code functionality starts from here!
int main()
{
    // Here, we are trying to create an initialiser container 
    // which will help us with creating a multimap using C++ STL
    multimap gquiz1;

     // these insert functions which we have written below help us
    // with creating a multimap holding the values inserted below!

    gquiz1.insert(pair(1, 40));
    gquiz1.insert(pair(2, 30));
    gquiz1.insert(pair(3, 60));
    gquiz1.insert(pair(6, 50));
    gquiz1.insert(pair(6, 10));

     // elements which we are going to display on the Output screen are
    multimap::iterator itr;
    cout << "\nThe multimap gquiz1 is : \n";
    cout << "\tKEY\tELEMENT\n";
    // here we are writing the for loop, which contains the auto code
    // functionality containing the find() function at different positions
    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
        cout << '\t' << itr->first
            << '\t' << itr->second << '\n';
    }
    cout << endl;

    // here, we are trying to add elements randomly,
    // as it helps us to check the sorted keys property in C++ STL
    gquiz1.insert(pair(4, 50));
    gquiz1.insert(pair(5, 10));

    // the below code helps us with printing multimap gquiz1 again

    cout << "\nmultimap quiz1 after"
        << " adding the extra elements is : \n";
    cout << "\tkey\telement\n";
    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
        cout << '\t' << itr->first
            << '\t' << itr->second << '\n';
    }
    cout << endl;

    // the below code snippet below assigning the elements from quiz1 to quiz2
    multimap gquiz2(gquiz1.begin(),
                            gquiz1.end());

// the below code snippet prints all the elements of the multimap gquiz2
    cout << "\nThe multimap gquiz2 after"
        << " assign from gquiz1 is : \n";
    cout << "\tKEY\tELEMENT\n";
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '\t' << itr->first
            << '\t' << itr->second << '\n';
    }
    cout << endl;

    // here, we are trying to remove all elements up to
    // key with the value 3 in quiz2
    cout << "\n the quiz2 after the removal of"
        << " elements less than key = 3 : \n";
    cout << "\t key \t element \n";
    gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '\t' << itr->first
            << '\t' << itr->second << '\n';
    }

    // the below code helps us with removing all the elements with key = 4
    int num;
    num = gquiz2.erase(4);
    cout << "\n quiz 2.erase(4) : ";
    cout << num << " removed \n";
    cout << "\tkey\telement\n";
    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
        cout << '\t' << itr->first
            << '\t' << itr->second << '\n';
    }

// new line printing statement the endl statement
    cout << endl;

    // the lower and upper bounds of the much awaited 
    // for multimap gquiz1 key = 5
    cout << "the quiz1.lower bound (5) : "
        << "\tkey = ";
    cout << gquiz1.lower_bound(5)->first << '\t';
    cout << "\telement = "
        << gquiz1.lower_bound(5)->second
        << endl;
    cout << "the quiz1.upper bound (5): "
        << "\tKEY = ";
    cout << gquiz1.upper_bound(5)->first << '\t';
    cout << "\telement = "
        << gquiz1.upper_bound(5)->second
        << endl;

    return 0;
    // The main driver code functionality ends from here!
}

输出结果:

The multimap gquiz1 is : 
    KEY ELEMENT
    1   40
    2   30
3   60
    6   50
    6   10


Multimap quiz1 after adding the extra elements is : 
    key element
    1   40
    2   30
    3   60
    4   50
    5   10
    6   50
    6   10


The multimap gquiz2 after assignment from gquiz1 is : 
    KEY ELEMENT
    1   40
    2   30
    3   60
    4   50
    5   10
    6   50
    6   10


 quiz2 after the removal of elements less than key = 3 : 
     key     element 
    3   60
    4   50
    5   10
    6   50
    6   10

 quiz 2.erase(4) : 1 removed 
    key element
    3   60
    5   10
    6   50
    6   10

the quiz1.lower bound (5) :     key = 5     element = 10
the quiz1.upper bound (5) :     KEY = 6     element = 50

C++ STL中的Multimap find()

语法

iterator multimap_name.find(key)
constant iterator multimap_name.find(key)

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of multimap find() in C++ STL(Standard Template Library)
#include 
using namespace std;
// The main driver code functionality starts from here!
int main()
{

    // Here, we are trying to create an initialiser container 
    // which will help us with creating a multimap using C++ STL
    multimap mp;

    // these insert functions which we have written below help us
    // with creating a multimap holding the values inserted below!
    mp.insert({ 12, 130 });
    mp.insert({ 11, 140 });
    mp.insert({ 12, 160 });
    mp.insert({ 31, 210 });
    mp.insert({ 11, 501 });
    mp.insert({ 14, 510 });

    // elements which we are going to display on the Output screen are
    cout << "The elements from position 3 in multimap are : \n";
    cout << "Key\telements : \n";

    // here we are writing the for loop, which contains the auto code
    // functionality containing the find() function at position 3
    for (auto itr = mp.find(31); itr != mp.end(); itr++)
        cout << itr->first
            << '\t' << itr->second << '\n';

    return 0;
    // The main driver code functionality ends from here!
}

输出:

The elements from position 3 in multimap are : 
Key elements : 
31  210

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程