c++学习-基础-函数参数传递 -vector和其他容器类型的形参

歌曲名:MELANCHOLY,歌手名:White Cherry,专辑名:MELANCHOLY
1.Lil peep的haunt u
2.Kina的u’re mine
3.Dawson的i’m so sorry
4.Ты просто люби меня.
5.Savior的 l can’t let this go on any further
6.nadiak的Hide
7.kina的 Can we kiss forever
8.Madi Marie的grief
9.Nicole Dollanganger的Creek Blues
10.nadiak的The Belly

/*
	Description: 函数参数传递 -vector和其他容器类型的形参 
		不要使用普通的非引用vector形参
		要使用引用形参传递vector
		事实上,程序员更喜欢传递vector的迭代器 
		顺序容器:vector,list,deque等等 
		关联容器:map,set等等 
*/
#include<iostream>
#include<vector>

using namespace std;

//          	非引用形参 
void print(vector<double> v)
{
	vector<double>::iterator begin = v.begin();
	while(begin != v.end())
	{
		cout<<*begin<<endl;
		++begin;
	}
}

//				引用形参 
void print_2(vector<double>& v)
{
	vector<double>::iterator begin = v.begin();
	while(begin != v.end())
	{
		cout<<*begin<<endl;
		++begin;
	}
}

void print_3(vector<double>::const_iterator beg,vector<double>::const_iterator end)
{
	while(beg != end)
	{
		cout<<*beg++<<endl;  	
	}
	
} 

double vectorSum(vector<double>::const_iterator begin,vector<double>::const_iterator end)
{
	double sum = 0;
	while(begin != end)
	{
		sum += *begin++;
	}
	return sum;
}

int main()
{
	vector<double> dvec;
//	dvec.push_back(1.1);
//	dvec.push_back(2.2);
//	dvec.push_back(3.3);

	cout<<"enter double type elements for vector:(ctrl + z to end)"<<endl;
	double dval;
	while(cin>>dval)
	{
		dvec.push_back(dval);
	}
	
	//把dvec复制到形参v 
	print(dvec);
	
	print_2(dvec);
	
	print_3(dvec.begin(),dvec.end());
	
	cout<<vectorSum(dvec.begin(),dvec.end());
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值