当前位置:网站首页>向量2(友元及拷贝构造)
向量2(友元及拷贝构造)
2022-06-22 14:09:00 【SZU治愈系BUG】
目录
题目描述
在题目向量1的代码上添加类CVector的友元函数Add,计算两个向量的和(对应分量相加)。
Add定义如下:
CVector Add(const CVector V1,const CVector V2) //函数头不可修改。
主函数输入数据,生成两个向量对象V1,V2,调用Add(V1,V2).print()输出向量V1+V2的计算结果。(假设print()为CVector类中的输出函数。)
可根据需要,为类CVector添加拷贝构造函数及其它成员函数。
输入
第一行,输入测试次数t
每组测试数据格式如下:
向量维数n
第一个n维向量值
第二个n维向量值
输出
对每组测试数据,输出两个n维向量与它们的和
输入样例1
2
3
1 2 3
4 5 6
5
1 2 3 4 5
-1 2 4 6 10
输出样例1
1 2 3
4 5 6
5 7 9
1 2 3 4 5
-1 2 4 6 10
0 4 7 10 15
AC代码
#include<iostream>
using namespace std;
class CVector
{
int * data;
int n;
public:
CVector(){
data=new int[5];
for(int i=0;i<5;i++)
data[i]=i;
n=5;
}
CVector(int n1,int *a){
n=n1;
data=new int[n1];
for(int i=0;i<n1;i++)
data[i]=a[i];
}
void print(){
int i;
for(i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[i]<<endl;
}
~CVector(){
if(data)
delete[] data;
data=NULL;
}
friend CVector Add(const CVector & a,const CVector& b);
};
CVector Add(const CVector& V1,const CVector& V2)
{
CVector temp(V1.n,V1.data);
for(int i=0;i<V1.n;i++)
temp.data[i]=V1.data[i]+V2.data[i];
return temp;
}
int main() {
int n,i,t;
cin>>t;
while(t--)
{
cin>>n;
int *p=new int [n];
for(i=0;i<n;i++)
cin>>p[i];
CVector V1(n,p);
V1.print();
for(i=0;i<n;i++)
cin>>p[i];
CVector V2(n,p);
V2.print();
Add(V1,V2).print();
if(p)
delete[] p;
p=NULL;
}
}边栏推荐
- ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
- 数据资产管理:数据发现,发现什么,怎么发现?
- FreeRtos 任务优先级和中断优先级
- After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!
- Vscode个性化设置:让一个小萌妹陪你敲代码
- Common operations in Visual Studio development
- No wonder the postgraduate entrance examination is so hot. These are the "hidden benefits" of Postgraduates!
- [Zhejiang University] information sharing of the first and second postgraduate entrance examinations
- flutter video_ Player monitors and automatically plays the next song
- Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
猜你喜欢

个人免签支付方案推荐

Tree structured binary tree

极致效率,云原生数据库TDSQL-C安身立命的根本
![[Software Engineering] planning and project management](/img/93/4b5b5034fbfb76adef1b4fe93a85cb.png)
[Software Engineering] planning and project management

ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点

Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node

TDengine 连接器上线 Google Data Studio 应用商店
![[PR] basic process](/img/e6/13a5703cfb8e5c51ed06fe9b0d9485.png)
[PR] basic process

壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代

PHP built-in protocols (supported and encapsulated protocols)
随机推荐
Flutter video Le lecteur écoute et joue automatiquement la prochaine chanson
RealNetworks vs. Microsoft: the battle in the early streaming media industry
MySQL learning notes 2022
好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
Once, I had 5 part-time jobs just to buy a new earring for my girlfriend
PHP built-in protocols (supported and encapsulated protocols)
【浙江大学】考研初试复试资料分享
Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
同花顺开户难么?网上开户安全么?
个人免签支付方案推荐
世界上所有的知名网络平台
加密市场进入寒冬,是“天灾”还是“人祸”?
KEIL仿真和vspd
先锋期货靠谱么?期货怎么开户安全些?
phpStudy 2016搭建-pikachu靶场
Detailed explanation of CSAPP Labs
Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
Good wind relies on strength – code conversion practice of accelerating SQL Server Migration with babelfish