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;
}
输出:
在此示例中,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 =()函数将引发错误。