1: //lexicographical_compare1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <algorithm> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the use of the STL " 11: "lexicographical_compare() algorithm\n(default version) to " 12: "test whether a range of integers in one vector precedes, " 13: "\nin the lexicographical (dictionary) sense, another range " 14: "of integers, in the\nsame vector, or in another vector."; 15: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 17: int a1[] = {4, 3, 7, 5, 6}; 18: vector<int> v1(a1, a1+5); 19: cout << "\nHere are the contents of v1:\n"; 20: for (vector<int>::size_type i=0; i<v1.size(); i++) 21: cout << v1.at(i) << " "; 22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 24: int a2[] = {4, 3, 7, 5, 6, 1, 2}; 25: vector<int> v2(a2, a2+7); 26: cout << "\nHere are the contents of v2:\n"; 27: for (vector<int>::size_type i=0; i<v2.size(); i++) 28: cout << v2.at(i) << " "; 29: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 31: if (lexicographical_compare(v1.begin(), v1.end(), 32: v2.begin(), v2.end())) 33: cout << "\nv1 precedes v2, in dictionary order."; 34: else 35: cout << "\nv1 does not precede v2, in dictionary order."; 36: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 38: int a3[] = {4, 3, 7, 5, 5}; 39: vector<int> v3(a3, a3+5); 40: cout << "\nHere are the contents of v3:\n"; 41: for (vector<int>::size_type i=0; i<v3.size(); i++) 42: cout << v3.at(i) << " "; 43: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 45: if (lexicographical_compare(v1.begin(), v1.end(), 46: v3.begin(), v3.end())) 47: cout << "\nv1 precedes v3, in dictionary order."; 48: else 49: cout << "\nv1 does not precede v3, in dictionary order."; 50: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 52: int a4[] = {4, 3, 7, 5, 7}; 53: vector<int> v4(a4, a4+5); 54: cout << "\nHere are the contents of v4:\n"; 55: for (vector<int>::size_type i=0; i<v4.size(); i++) 56: cout << v4.at(i) << " "; 57: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 59: if (lexicographical_compare(v1.begin(), v1.end(), 60: v4.begin(), v4.end())) 61: cout << "\nv1 precedes v4, in dictionary order."; 62: else 63: cout << "\nv1 does not precede v4, in dictionary order."; 64: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 66: if (lexicographical_compare(v1.begin(), v1.begin()+3, 67: v1.begin()+3, v1.end())) 68: cout << "\nThe first three values of v1 " 69: "precede the last three, in dictionary order."; 70: else 71: cout << "\nThe first three values of v1 " 72: "do not precede the last three, in dictionary order."; 73: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 74: }