Matching Elements Using the equals and mismatch Operations : Vector « Data Structure « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Data Structure » VectorScreenshots 
Matching Elements Using the equals and mismatch Operations
Matching Elements Using the equals and mismatch Operations

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor);

int main()
{
    typedef vector<int>::iterator    VectorInterator;
    vector<int>    intValueVector(4);
    intValueVector[01;
    intValueVector[12;
    intValueVector[23;
    intValueVector[34;

    vector<int>    intValueVector2(3);
    intValueVector2[01;
    intValueVector2[12;
    intValueVector2[23;

    VectorInterator    first1 = intValueVector.begin();
    VectorInterator    last1  = intValueVector.end();
    VectorInterator    first2 = intValueVector2.begin();
    if (equal(first1, last1, first2))
        cout << "intValueVector == intValueVector2\n";
    else
        cout << "intValueVector != intValueVector2\n";

    pair<VectorInterator, VectorInterator> pi = mismatch(first1, last1, first2);
    cout << "First mismatch element in intValueVector = ";
    ShowElement(intValueVector, pi.first);
    cout << "\n";
    cout << "First mismatch element in intValueVector2 = ";
    ShowElement(intValueVector2, pi.second);
    cout << "\n";

    return 0;
}

template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor)
{
    if (itor != c.end())
    {
        if (itor != c.begin())
            cout << *itor << "\tthe previous element is " << *(itor - 1);
        else
            cout << "first";
    }
    else
        cout << "last";
}

           
       
Related examples in the same category
1.Perform an in-place merge for two vectorsPerform an in-place merge for two vectors
2.Using Other Search functionsUsing Other Search functions
3.Creating and Resizing VectorsCreating and Resizing Vectors
4.Demonstrate count and count_if.Demonstrate count and count_if.
5.Demonstrate remove_copy in VectorDemonstrate remove_copy in Vector
6.Access a vector using an iterator.Access a vector using an iterator.
7.Demonstrate insert and erase.Demonstrate insert and erase.
8.Store a class object in a vector. Store a class object in a vector.
9.Demonstrate allocator's max_size() fucntion in vectorDemonstrate allocator's max_size() fucntion in vector
10.Demonstrate count() in vectorDemonstrate count() in vector
11.Demonstrate count_if().Demonstrate count_if().
12.Demonstrate reverse in vectorDemonstrate reverse in vector
13.Demonstrate insert_iterator in vectorDemonstrate insert_iterator in vector
14.Demonstrate adjacent_difference() in vectorDemonstrate adjacent_difference() in vector
15.Demonstrate inner_product() in vectorDemonstrate inner_product() in vector
16.Demonstrate partial_sum() in Vector
17.Storing Class Objects with overloaded operators in a Vector
18.Vector Init Array
19.Use istream_iterator with the copy algorithm
20.Demonstrate remove_copy and replace_copy.
21.Vector: Insert Erase Sort
22.Demonstrate accumulate() in vector
23.end() in vector
24.Use pop_back() and empty().
25.Access the elements of a vector through an iterator.
26.The basic operation of a vector: size, push_back,
27.Accessing a Vector Through an Iterator
28.Use istream_iterator to read various data types
29.Create permutations based on vector
30.Work with heaps: make_heap from vector
31.Demonstrating the four ways that vectors can be created.
32.Vector Capacity vs size
33. Using clear()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.