C++ primer 第五版 中文版 9.2.7 节练习

C++ primer 第五版 中文版 9.2.7 节练习

练习 9.15:编写程序,判定两个vector是否相等。

答:

#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

	
int main(void)
{
	vector<int> a{1,2,3,4,5,6,7},b{1,2,3,4};
	if(a==b)
		cout<<"a和b相等"<<endl;
	else
		cout<<"a和b不相等"<<endl;
		
	return 0;
}

练习 9.16:重写上一题的程序,比较一个list中元素和一个veotr中的元素。
答:

#include <iostream>
#include <vector>
#include <list>

using std::vector;
using std::list;
using std::cout;
using std::endl;

	
int main(void)
{
	list<int> temp{1,2,3,4,5};
	vector<int> temp2{1,2,3,5},ass;
	ass.assign(temp.cbegin(),temp.cend());
	if(ass==temp2)
		cout<<"temp1和temp2相等"<<endl;
	else
		cout<<"temp1和temp2不相等"<<endl;
		
	return 0;
}

练习9.17 假定c1和c2是两个容器,下面比较操作有何限制?
if(c1<c2)
答: 1、c1和c2容器类型和元素类型必须相同。
2、c1和c2的元素类型要定义了比较运算符才能比较。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值