Skip to main content

Section 13.4 Vectors and Operators

We have already seen that = works with vectors to make copies of them. And that << does not work. What about other operators?
Most operators do not work with vectors. You can not use >> to read data into a vector. Nor can you use + or += to add vectors or add data to a vector. None of the arithmetic operators work with vectors. Other than assignment, the only common operators that work with vectors are the comparison operators.
We can use == to compare two vectors for equality. Two vectors are equal if they have the same size and all corresponding elements are equal. Here is an example:
Listing 13.4.1.
Inequalities also work with vectors. The operators <, >, <=, and >= can be used to compare two vectors lexicographically. This means that the vectors are compared element by element from the beginning until a difference is found. The result of the comparison is determined by the first pair of differing elements. If all elements are equal but one vector is shorter, the shorter vector is considered less than the longer one. Here is an example:
Listing 13.4.2.
You have attempted of activities on this page.