[Cs3] lexicographic comparison of vectors

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Feb 2 16:09:53 EST 2022


CS3 students,

I am not sure what my hangup on Tuesday was. This code:

----------
// comparing vectors
// comparison is lexicographical
// Mikhail Nesterenko
// 9/16/2014


#include <vector>
#include <iostream>
#include <string>

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

int main(){

   vector <int> v1 = {1, 2, 3, 4, 5},
                v2 = {1, 2, 3, 4};

  cout << (v1 == v2 ? "v1 == v2" : "v1 != v2") << endl;
  cout << (v1 < v2  ? "v1 < v2" : "v1 => v2") << endl;


  /*
   vector <string>
      v3 = {"one", "two", "three", "four"},
      v4 = {"one", "two", "three"};


   cout << ( v3 < v4 ? "v3 < v4" : "v3 => v4") << endl;
  */
}
---

 Outputs:

v1 != v2
v1 => v2

As it should, because v1 and v2 have the same first four elements and
v1 is longer and, therefore, considered greater.

Thanks,
-- 
Mikhail


More information about the cs3 mailing list