Chinaunix首页 | 论坛 | 博客
  • 博客访问: 267812
  • 博文数量: 45
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 553
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-22 17:53
文章分类

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-05-06 22:26:19

来自于C++ primer习题4.25

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. int main()
  5. {
  6.     vector<int> ivec1, ivec2;
  7.     int ival;

  8.     cout<<"Enter numbers for vector1(-1 to end): "<<endl;
  9.     cin >> ival;
  10.     while (ival != -1)
  11.     {
  12.         ivec1.push_back(ival);
  13.         cin >> ival;
  14.     }
  15.     cout<<"Enter numbers for vector2(-1 to end): "<<endl;
  16.     cin >> ival;
  17.     while (ival != -1)
  18.     {
  19.         ivec2.push_back(ival);
  20.         cin >> iva2;
  21.     }
  22.     
  23.     if (ivec1.size() != ivec2.size())
  24.         cout<<"Vector1 is not equal to vector2."<<endl;
  25.     else if(ivec1.size() == 0)
  26.         cout<<"Vector1 is equal to vector2"<<endl;
  27.     else
  28.     {
  29.         vector<int>::iterator iter1, iter2;
  30.         iter1 = ivec1.begin();
  31.         iter2 = ivec2.begin();
  32.         while (iter1 != ivec1.end() && iter2 != ivec2.end()
  33.                 && *iter1 == *iter2)
  34.         {
  35.             ++iter1;
  36.             ++iter2;
  37.         }
  38.         if(iter1 == ivec1.end())
  39.             cout<<"Vector1 is equal to vector2."<<endl;
  40.         else
  41.             cout<<"Vector1 is not equal to vector2."<<endl;
  42.     }
  43.     return 0;
  44. }


阅读(1407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~