operators < and == for vector

R

Ralf Goertz

I was going to use vector<int> as the key for map preparing myself to
have to provide operators < and == for vector<int>. Surprisingly, they
already exist (g++). However, in
http://www.cplusplus.com/reference/stl/vector/ these operators are not
mentioned. Does the standard guarantee the existence of these operators
and if so does it also specify the sort order for <?

Ralf
 
G

gwowen

Does the standard guarantee the existence of these operators
No.

and if so does it also specify the sort order for <?

No (inevitably, given previous answer). However, the STL does provide
std::equal() and std::lexicographrical_compare() for precisely this
purpose. That's what g++ calls, the operator< and operator== are just
wrappers to those other functions.
 
V

Victor Bazarov

I was going to use vector<int> as the key for map preparing myself to
have to provide operators< and == for vector<int>. Surprisingly, they
already exist (g++). However, in
http://www.cplusplus.com/reference/stl/vector/ these operators are not
mentioned. Does the standard guarantee the existence of these operators
and if so does it also specify the sort order for<?

Yes, the Standard does enumerate them, but doesn't specify what they do
and how, or at least I couldn't find anything after about 10 minutes of
looking for it.

V
 
V

Victor Bazarov

Yes, the Standard does enumerate them, but doesn't specify what they do
and how, or at least I couldn't find anything after about 10 minutes of
looking for it.

My bad. The operator < is defined in the table 65 in terms of
'lexicographical_compare' and operator == is defined in the same table
(equivalence relation: same size *and* 'std::equal' returns 'true').

So, YES, definitely. And gwowen is wrong, unfortunately.

V
 
V

Victor Bazarov

The specifications are in Table 65 in C++03.

operator== checks whether the two vectors have the same number of
elements, and if so, checks corresponding elements for equality.

operator< calls the algorithm lexicographical_compare() on the two ranges.

Yes, I found those, thanks. I used to be better at looking things up in
the Standard <sigh>

V
 
S

Saeed Amrollahi

I was going to use vector<int> as the key for map preparing myself to
have to provide operators < and == for vector<int>. Surprisingly, they
already exist (g++). However, inhttp://www.cplusplus.com/reference/stl/vector/these operators are not
mentioned. Does the standard guarantee the existence of these operators
and if so does it also specify the sort order for <?

Ralf

Hi Ralf

According to C++ standard documents
- ISO-IEC_14882.2003 (C++03) section 23.2.4 Class template vector
- N3092-Draft International Standard (C++0x) section 23.3.6 Class
template vector
the comparison operators (==, <, >, <=, >=, !=) should be defined for
vectors.
Two vectors v1 and v2 compare equal if v1.size()==v2.size() and
v1[n]==v2[n] for every
valid index n . Similarly, < is a lexicographical ordering. This
means, v1 is less than v2
if the first element v1 that is not equal to the corresponding
element
v2 is less than v2[i ], or v1.size() < v2.size() with every v1
equal to its corresponding v2. For other relop(s), the logic is
similar.
Also please see the C++ Programming Language by B. Stroustrup section
16.3.10

HTH,
-- Saeed Amrollahi
 
R

Ralf Goertz

Ralf said:
I was going to use vector<int> as the key for map preparing myself to
have to provide operators < and == for vector<int>. Surprisingly, they
already exist (g++). However, in
http://www.cplusplus.com/reference/stl/vector/ these operators are not
mentioned. Does the standard guarantee the existence of these operators
and if so does it also specify the sort order for <?

Thanks for the answers!
 
J

Juha Nieminen

Ralf Goertz said:
I was going to use vector<int> as the key for map preparing myself to
have to provide operators < and == for vector<int>.

Btw, std::map does not require operator== from the elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top