编写程序判断两个数组是否相等
2个回答
展开全部
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec1, ivec2;
int ival;
//读入两个vector 的元素值
cout << "Enter numbers for vector1(-1 to end):" << endl;
cin >> ival;
while (ival != -1) {
ivec1.push_back(ival);
cin >> ival;
}
cout << "Enter numbers for vector2(-1 to end):" << endl;
cin >> ival;
while (ival != -1) {
ivec2.push_back(ival);
cin >> ival;
}
//判断两个vector 是否相等
if (ivec1.size() != ivec2.size()) //长度不等的vector 不相等
cout << "Vector1 is not equal to vector2." << endl;
else if (ivec1.size() == 0) //长度都为0 的vector 相等
cout << "Vector1 is equal to vector2." << endl;
else {//两个vector 长度相等且不为0
vector<int>::iterator iter1, iter2;
iter1 = ivec1.begin();
iter2 = ivec2.begin();
while (iter1 != ivec1.end() && *iter1 == *iter2) { // 先判断是不是end(),不是才判断是否相等
++iter1;
++iter2;
}
if (iter1 == ivec1.end())//所有元素都相等
cout << "Vector1 is equal to vector2." << endl;
else
cout << "Vector1 is not equal to vector2." << endl;
}
system("pause");
return 0;
}
#include <vector>
using namespace std;
int main()
{
vector<int> ivec1, ivec2;
int ival;
//读入两个vector 的元素值
cout << "Enter numbers for vector1(-1 to end):" << endl;
cin >> ival;
while (ival != -1) {
ivec1.push_back(ival);
cin >> ival;
}
cout << "Enter numbers for vector2(-1 to end):" << endl;
cin >> ival;
while (ival != -1) {
ivec2.push_back(ival);
cin >> ival;
}
//判断两个vector 是否相等
if (ivec1.size() != ivec2.size()) //长度不等的vector 不相等
cout << "Vector1 is not equal to vector2." << endl;
else if (ivec1.size() == 0) //长度都为0 的vector 相等
cout << "Vector1 is equal to vector2." << endl;
else {//两个vector 长度相等且不为0
vector<int>::iterator iter1, iter2;
iter1 = ivec1.begin();
iter2 = ivec2.begin();
while (iter1 != ivec1.end() && *iter1 == *iter2) { // 先判断是不是end(),不是才判断是否相等
++iter1;
++iter2;
}
if (iter1 == ivec1.end())//所有元素都相等
cout << "Vector1 is equal to vector2." << endl;
else
cout << "Vector1 is not equal to vector2." << endl;
}
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询