📜  C++中的unordered_multimap运算符=

📅  最后修改于: 2021-05-30 18:19:55             🧑  作者: Mango

unordered_multimap :: 运算符=是C++ STL中的内置函数,它执行三种类型的任务,下面将对其进行说明。

  1. 语法(从不同容器复制元素):
    unordered_multimap_name1 operator= (unordered_multimap_name2)

    参数:该函数不接受任何参数。右边的容器是将元素复制到左边的容器的容器。

    返回值:不返回任何内容。

    下面的程序说明了上述函数:

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #include 
    using namespace std;
      
    int main()
    {
      
        // declaration
        unordered_multimap sample1, sample2;
      
        // inserts key and element
        // in sample1
        sample1.insert({ 10, 100 });
        sample1.insert({ 50, 500 });
      
        cout << "Key and Elements of Sample1 before copy  are:";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        cout << "\nThe size of sample2 before copy: "
             << sample2.size();
      
        // opeator= to copy
        sample2 = sample1;
      
        cout << "\nKey and Elements of Sample2 after copy are: ";
        for (auto it = sample2.begin(); it != sample2.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        return 0;
    }
    
    输出:
    Key and Elements of Sample1 before copy  are:{50, 500} {10, 100} 
    The size of sample2 before copy: 0
    Key and Elements of Sample2 after copy are: {50, 500} {10, 100}
    
  2. 语法(用于从不同容器中移动元素):
    unordered_multimap_name1 operator= (unordered_multimap_name2)

    参数:该函数不接受任何参数。右侧的容器是将元素从中移至左侧的容器的容器。使用运算符=后,右侧容器中的元素将被销毁。

    返回值:不返回任何内容。

    下面的程序说明了上述函数:

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #include 
    using namespace std;
      
    // Function to merge two lists
    unordered_multimap merge(unordered_multimap a,
                                         unordered_multimap b)
    {
        unordered_multimap temp(a);
        temp.insert(b.begin(), b.end());
        return temp;
    }
    int main()
    {
      
        // declaration
        unordered_multimap sample1, sample2, sample3;
      
        // inserts key and element
        // in sample1
        sample1.insert({ 'a', 'A' });
        sample1.insert({ 'g', 'G' });
      
        // inserts key and element
        // in sample1
        sample2.insert({ 'b', 'B' });
        sample2.insert({ 'c', 'C' });
        sample2.insert({ 'd', 'D' });
      
        cout << "Key and Elements of Sample1 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        cout << "\nKey and Elements of Sample2 are: ";
        for (auto it = sample2.begin(); it != sample2.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        // merging and moved
        sample3 = merge(sample1, sample2);
        sample1 = sample3;
      
        cout << "\n\nKey and Elements of Sample1 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        return 0;
    }
    
    输出:
    Key and Elements of Sample1 are: {g, G} {a, A} 
    Key and Elements of Sample2 are: {d, D} {b, B} {c, C} 
    
    Key and Elements of Sample1 are: {c, C} {b, B} {d, D} {a, A} {g, G}
    
  3. 语法(用于分配来自不同列表的元素):
    unordered_multimap_name1 operator= (intitializer_list il)

    参数:它不接受任何参数,它在右边具有将分配给容器的列表。

    返回值:不返回任何内容。

    下面的程序说明了上述函数:

    // C++ program to illustrate the
    // unordered_multimap::operator=
    #include 
    using namespace std;
      
    int main()
    {
      
        // declaration by using operator=
        unordered_multimap sample1 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
      
        cout << "Key and Elements of Sample1 are: ";
        for (auto it = sample1.begin(); it != sample1.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        // declaration by using operator=
        unordered_multimap sample2 = { { 'a', 'A' }, { 'b', 'B' }, { 'c', 'C' } };
      
        cout << "\n\nKey and Elements of Sample1 are: ";
        for (auto it = sample2.begin(); it != sample2.end(); it++) {
            cout << "{" << it->first << ", " << it->second << "} ";
        }
      
        return 0;
    }
    
    输出:
    Key and Elements of Sample1 are: {5, 6} {3, 4} {1, 2} 
    
    Key and Elements of Sample1 are: {c, C} {b, B} {a, A}
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”