C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Deque operator=()

C++ Deque operator=()

C++ Deque运算符=()函数将新内容分配给容器,并替换相同类型的当前内容。可以根据需要修改 Deque 的大小。

语法

deque& operator(deque& x);

参数

x : 这是一个 Deque 容器,其内容将被复制到另一个 Deque 对象。

返回值

它返回* this。

示例1

让我们看一个简单的示例
#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> a={1,2,3,4,5};
  deque<int> b;
  b.operator=(a);
  for(int i=0;i<b.size();i++)
  {
      cout<<b[i];
      cout<<" ";
  }
   return 0;
}
输出:
1 2 3 4 5 
在此示例中,operator =()将'a'容器的内容分配给'b'容器。

示例2

让我们看看这两个 Deque 的类型不同的简单示例。
#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> a={10,20,30,40,50};
  deque<char> b;
  b.operator=(a);
  for(int i=0;i<b.size();i++)
  {
      cout<<b[i];
      cout<<" ";
  }
  
   return 0;
}
输出:
error: no matching function for call to 'std::deque<char>::operator=(std::deque<int>&)'
在此示例中," a"和" b"的类型不同。因此,operator =()函数将引发错误。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4