C++ examples for STL Algorithm:lexicographical_compare
Comparing Ranges with Different kinds of comparisons
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; template<typename C> void printContainer(const C& c, char delim = ',', ostream& out = cout) { printRange(c.begin(), c.end(), delim, out); } template<typename Fwd> void printRange(Fwd first, Fwd last, char delim = ',', ostream& out = cout) { out << "{"; while (first != last) { out << *first;//w w w. j a va 2 s . com if (++first != last) out << delim << ' '; } out << "}" << endl; } int main() { vector<string> vec1, vec2; vec1.push_back("A"); vec1.push_back("B"); vec1.push_back("C"); vec2.push_back("a"); vec2.push_back("b"); vec2.push_back("c"); if (equal(vec1.begin(), vec1.end(), vec2.begin())) { cout << "The two ranges are equal!" << endl; } else { cout << "The two ranges are NOT equal!" << endl; } string s1 = "abcde"; string s2 = "abcdef"; string s3 = "abc"; cout << boolalpha // Show bools as "true" or "false" << lexicographical_compare(s1.begin(), s1.end(), s1.begin(), s1.end()) << endl; cout << lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end()) << endl; cout << lexicographical_compare(s2.begin(), s2.end(), s1.begin(), s1.end()) << endl; cout << lexicographical_compare(s1.begin(), s1.end(), s3.begin(), s3.end()) << endl; cout << lexicographical_compare(s3.begin(), s3.end(), s1.begin(), s1.end()) << endl; pair<string::iterator, string::iterator> iters = mismatch(s1.begin(), s1.end(), s2.begin()); cout << "first mismatch = " << *(iters.first) << endl; cout << "second mismatch = " << *(iters.second) << endl; }