C++杂谈

本文探讨了 C++ 中 vector 作为函数参数时的不同行为,包括值传递导致的拷贝问题及引用传递对 vector 的实际修改效果。通过具体代码示例展示了如何避免不必要的对象拷贝,提高程序效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:函数参数为vector<T>,在函数中为vector添加元素,函数结束后,对vector的更改是无效的。通过vector地址的输出,可以看到传参时确实对vector进行了拷贝。

测试代码:

void test_vector( vector<int> vec)
{
cout<<&vec<<endl;
vec.push_back(1);
vec.push_back(2);
}


int main()
{
vector<int> arr = vector<int>(1,0);//第一个参数必须非负,可以为0
cout<<&arr<<endl;
for( int i=0; i<arr.size(); ++i )
cout<<arr[i]<<" ";
cout<<endl;
test_vector(arr);
cout<<"after call test_vector function"<<endl;
for( int i=0; i<arr.size(); ++i )
cout<<arr[i]<<" ";
cout<<endl;
/*0054FA84
0
0054F954
after call test_vector function
0

*/

}

二:在函数中申请局部变量加入vector,会调用复制构造函数。

#include<iostream>
#include<vector>
using namespace std;
class T
{
public:
<span style="white-space:pre">	</span>int data;
<span style="white-space:pre">	</span>T()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>data = 0;
<span style="white-space:pre">		</span>cout<<"no paragram constructor. data= "<<data<<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>T(int d)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>data = d;
<span style="white-space:pre">		</span>cout<<"paragram constructor. data= "<<data<<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>T(const T& t)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>data = t.data;
<span style="white-space:pre">		</span>cout<<"copy constructor    data="<<data<<endl;
<span style="white-space:pre">	</span>}
};


void test_vector( vector<T> vec)
{
<span style="white-space:pre">	</span>cout<<"call function"<<endl;
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(1);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(2);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(3);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(4);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(5);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
<span style="white-space:pre">	</span>vec.push_back(6);
<span style="white-space:pre">	</span>cout<<"vec capacity :"<<vec.capacity()<<endl;
}


int main()
{
<span style="white-space:pre">	</span>vector<T> arr;
<span style="white-space:pre">	</span>arr.push_back(0);
<span style="white-space:pre">	</span>for( int i=0; i<arr.size(); ++i )
<span style="white-space:pre">		</span>cout<<arr[i].data<<" ";
<span style="white-space:pre">	</span>cout<<endl;
<span style="white-space:pre">	</span>test_vector(arr);
<span style="white-space:pre">	</span>cout<<"after call test_vector function"<<endl;
<span style="white-space:pre">	</span>for( int i=0; i<arr.size(); ++i )
<span style="white-space:pre">		</span>cout<<arr[i].data<<" ";
<span style="white-space:pre">	</span>cout<<endl;
<span style="white-space:pre">	</span>/*
<span style="white-space:pre">	</span>paragram constructor. data= 0
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>0
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>call function
<span style="white-space:pre">	</span>vec capacity :1
<span style="white-space:pre">	</span>paragram constructor. data= 1
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>copy constructor    data=1
<span style="white-space:pre">	</span>vec capacity :2
<span style="white-space:pre">	</span>paragram constructor. data= 2
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>copy constructor    data=1
<span style="white-space:pre">	</span>copy constructor    data=2
<span style="white-space:pre">	</span>vec capacity :3
<span style="white-space:pre">	</span>paragram constructor. data= 3
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>copy constructor    data=1
<span style="white-space:pre">	</span>copy constructor    data=2
<span style="white-space:pre">	</span>copy constructor    data=3
<span style="white-space:pre">	</span>vec capacity :4
<span style="white-space:pre">	</span>paragram constructor. data= 4
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>copy constructor    data=1
<span style="white-space:pre">	</span>copy constructor    data=2
<span style="white-space:pre">	</span>copy constructor    data=3
<span style="white-space:pre">	</span>copy constructor    data=4
<span style="white-space:pre">	</span>vec capacity :6
<span style="white-space:pre">	</span>paragram constructor. data= 5
<span style="white-space:pre">	</span>copy constructor    data=5
<span style="white-space:pre">	</span>vec capacity :6
<span style="white-space:pre">	</span>paragram constructor. data= 6
<span style="white-space:pre">	</span>copy constructor    data=0
<span style="white-space:pre">	</span>copy constructor    data=1
<span style="white-space:pre">	</span>copy constructor    data=2
<span style="white-space:pre">	</span>copy constructor    data=3
<span style="white-space:pre">	</span>copy constructor    data=4
<span style="white-space:pre">	</span>copy constructor    data=5
<span style="white-space:pre">	</span>copy constructor    data=6
<span style="white-space:pre">	</span>vec capacity :9
<span style="white-space:pre">	</span>after call test_vector function
<span style="white-space:pre">	</span>0
<span style="white-space:pre">	</span>*/
<span style="white-space:pre">	</span>return 0;
}
从输出中可以看到每次为vector添加一个int会自动调用有参构造函数构造一个临时对象然后再调用复制构造函数对临时对象进行复制。(注意vector容量增长)

三:函数参数改为vector& ,对vector的更改确实生效了。

#include<iostream>
#include<vector>
using namespace std;
void test_vector( vector<int>& vec)
{
	cout<<&vec<<endl;
	cout<<"call function"<<endl;
	vec.push_back(1);
	vec.push_back(2);	
}

int main()
{
	vector<int> arr;
	arr.push_back(0);
	cout<<&arr<<endl;
	for( int i=0; i<arr.size(); ++i )
		cout<<arr[i]<<" ";
	cout<<endl;
	test_vector(arr);
	cout<<"after call test_vector function"<<endl;
	for( int i=0; i<arr.size(); ++i )
		cout<<arr[i]<<" ";
	cout<<endl;
	/*
	0050F8DC
	0
	0050F8DC
	call function
	after call test_vector function
	0 1 2
	*/
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值