views:

106

answers:

2

I wan to user the function lexicographical_compare in algorithms library in c++.

But I do not know what to write as far as the using statement. For example

using std::lexicographical_compare ??

How can I figure this out for my self in the future?

Thanks

+1  A: 

What you have is fine - you will also need to include the algorithm header:

#include <algorithm>

As to how to find out these things for yourself, I strongly recommend getting a copy of The C++ Standard Library.

anon
+2  A: 

Just do

 using std::lexicographical_compare;

and then (copied from SGI STL Doc)

int main()
{
  int A1[] = {3, 1, 4, 1, 5, 9, 3};
  int A2[] = {3, 1, 4, 2, 8, 5, 7};
  int A3[] = {1, 2, 3, 4};
  int A4[] = {1, 2, 3, 4, 5};

  const int N1 = sizeof(A1) / sizeof(int);
  const int N2 = sizeof(A2) / sizeof(int);
  const int N3 = sizeof(A3) / sizeof(int);
  const int N4 = sizeof(A4) / sizeof(int);

  bool C12 = lexicographical_compare(A1, A1 + N1, A2, A2 + N2);
  bool C34 = lexicographical_compare(A3, A3 + N3, A4, A4 + N4);

  cout << "A1[] < A2[]: " << (C12 ? "true" : "false") << endl;
  cout << "A3[] < A4[]: " << (C34 ? "true" : "false") << endl;
}

Alternatively

// no using statement

int main()
{
   //... same as above
   bool C12 = std::lexicographical_compare(A1, A1 + N1, A2, A2 + N2);
   bool C34 = std::lexicographical_compare(A3, A3 + N3, A4, A4 + N4);
   //... same as above
}

To know yourself in the future, read a C++ book (e.g. Stroustrup's "The C++ Programming Language") from cover to cover.

Amit Kumar