我想要判断较短的数组是否是较长数组的前缀,我想要先输入数组元素,可是为什么两个while不能连着一起使用呢?
#include <iostream>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::vector;
int main()
{
vector<int> ivec1, ivec2;
int num;
cout << "Enter the first vector:";
while (cin >> num)
ivec1.push_back(num);
cout << "Enter the second vector:";
while (cin >> num)
ivec2.push_back(num);
int flag = 1;
for (int i = 0; i < (ivec1.size() <= ivec2.size() ? ivec1.size() : ivec2.size()); ++i)
if (ivec1[i] != ivec2[i]) {
flag = 0;
break;
}
if (flag)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
运行结果如下:
