Jump to content

C++ Comparison Algorithm

- - - - -

  • Please log in to reply
No replies to this topic

#1
QuackWare

QuackWare

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
C++ Comparison Algorithm

 /** This code snippet was taken from freecodesnippets.com*/



 

#include <algorithm>

#include <vector>

#include <list>

#include <iostream>

using namespace std;


int main(int argc, char** argv)

{

  vector<int> myVector;

  list<int> myList;


  myVector.push_back(1);

  myVector.push_back(2);

  myVector.push_back(3);

 

  myList.push_back(1);

  myList.push_back(2);

  myList.push_back(3);

 

  if (myList.size() < myVector.size()) {

    cout << "Sorry, the list is not long enough.\n";

    return (0);

  }


  if (equal(myVector.begin(), myVector.end(), myList.begin())) {

    cout << "The two containers have equal elements\n";

  }


  if (lexicographical_compare(myVector.begin(), myVector.end(), myList.begin(),myList.end())) {

    cout << "The vector is lexicographically first.\n";

  } else {

    cout << "The list is lexicographically first.\n";

  }


  return (0);

}

  





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users