Broad Network


Vector Equality Operators in C++

Container Library Sequences in C++ Simplified – Part 9

Forward: In this part of the series, we look at Vector Equality Operators in C++.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is the part 9 of my series, Containers Library Sequences in C++, Simplified. In this part of the series, we look at Vector Equality Operators in C++. I assume that you have read the previous parts of this series.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
If the vector x and the vector y have the same size and the corresponding elements are equal, then the == operator returns true; otherwise it returns false. Try,

#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor;

        myVtor.push_back('A');
        myVtor.push_back('B');

     vector<char> herVtor;

        herVtor.push_back('A');
        herVtor.push_back('B');

        if (myVtor == herVtor)
            {
                cout << "The two vectors are equal.";
            }

        return 0;
    }

bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
The != operator is the opposite of ==. The following code illustrates this:

#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor;

        myVtor.push_back('A');
        myVtor.push_back('B');

     vector<char> herVtor;

        herVtor.push_back('A');
        herVtor.push_back('C');

        if (myVtor != herVtor)
            {
                cout << "The vectors are not equal.";
            }
        else
            {
                cout << "The two vectors are equal.";
            }

        return 0;
    }

The vector class has just these two equality operators. We end here and continue in the next part, in a new division.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message