题目:假设有两个包含整数的vector对象,编写一段程序,检验其中一个vector对象是否是另一个的前缀。为了实现这一目标,对于两个不等长
的vector对象,只需挑出长度较短的那个,把它的所有元素和另一个vector对象比较即可。
例如,如果两个vector对象的元素分别是0、1、1、2 和0、1、1、2、3、5、8,则程序的返回结果应该为真。
下面是我写的,我现在看都觉得有点蠢,不知道以后看会不会羞愤而死。。。。。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec1, ivec2;
int i,temp=1;
cout << "first number:" << endl;
cin >> i;
ivec1.push_back(i);
cout << "second number:" << endl;
cin >> i;
ivec2.push_back(i);
bool mix = (ivec1.size() > ivec2.size());
switch (mix)
{
case 0:
{
for (vector<int>::size_type t = 0; t != ivec1.size(); ++t)
{
if (ivec1[t] == ivec2[t])
++temp;
else
break;
}
if (temp == ivec1.size())
cout << "yes" << endl;
else
cout << "no" << endl;
break;
}
case 1:
{
for (vector<int>::size_type t = 0; t != ivec2.size(); ++t)
{
if (ivec2[t] == ivec1[t])
++temp;
else
break;
}
if (temp == ivec2.size())
cout << "yes" << endl;
else
cout << "no" << endl;
break;
}
}
return 0;
}