C++ STL multimap::emplace_hint() 函数

C++ STL multimap::emplace_hint() 函数

众所周知,C++ 编程语言具有许多内置函数,帮助我们避免编写冗长的代码。C++ STL 的丰富库中就有一个名为 multimap 的查找函数,它将帮助我们返回一个迭代器,指向或指向包含我们创建的长或短 multimap 中的数据遍历的键的位置。multimap emplaces find 比 multimap emplace 快得多。它使用提示插入关键元素,并且前导不影响要插入的位置。

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 initializer 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 that 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;


return 0;

}

输出:

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 assign from gquiz1 is : 
    KEY ELEMENT
    1   40
    2   30
    3   60
    4   50
    5   10
    6   50
    6   10

multimap::emplace_hint()函数在C++ STL中的应用

语法

multimap_name.emplace_hint(position, key, element)

C++代码

// Here we are writing down the C++ programming language code to 
// demonstrate
// the concept of multimap::emplace_hint() function in the 
// 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 initializer container 
    // which will help us with creating a multimap using C++ STL
    multimap mp;

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

    mp.emplace_hint(mp.begin(), 12, 230); // this is faster
    mp.emplace_hint(mp.begin(), 14, 440); // this is faster
    mp.emplace_hint(mp.begin(), 42, 960); // this is slower
    mp.emplace_hint(mp.begin(), 32, 620); // this is slower
    mp.emplace_hint(mp.begin(), 31, 540); // this is faster
    mp.emplace_hint(mp.begin(), 17, 150); // this is faster

    // elements that we are going to display on the Output screen are
    cout << "\nThe multimap is : \n";
    cout << "KEY\tELEMENT\n";

    // here we are writing the for loop, which contains the auto code
    // functionality containing the find() function at different positions 
    for (auto itr = mp.begin(); itr != mp.end(); itr++)
        cout << itr->first << "\t" << itr->second << endl;

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

输出:

The multimap is : 
KEY ELEMENT
12  230
14  440
17  150
31  540
32  620
42  960

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程