[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

operator == on vectors

Paul

6/12/2016 11:02:00 AM

I don't seem to understand operators on vectors. I thought that you could write someVec == {} as a statement saying that someVec is empty.

However, this doesn't compile and I don't know why.
My error message is "expected primary-expression before '{' token".
I'd be grateful if someone could enlighten me. The complete code is below.
Thanks again for your help.

Paul

#include <vector>

int main()
{
std::vector<int> z;
if(z == {})
; // empty statement.
}
19 Answers

Victor Bazarov

6/12/2016 12:34:00 PM

0

On 6/12/2016 7:02 AM, Paul wrote:
> I don't seem to understand operators on vectors. I thought that you could write someVec == {} as a statement saying that someVec is empty.
>
> However, this doesn't compile and I don't know why.
> My error message is "expected primary-expression before '{' token".
> I'd be grateful if someone could enlighten me. The complete code is below.
> Thanks again for your help.
>
> Paul
>
> #include <vector>
>
> int main()
> {
> std::vector<int> z;
> if(z == {})
> ; // empty statement.
> }

There is no operator== declared for std::vector. Equality operators are
not compiler-defined for user-defined classes. The library classes like
containers, streams, algorithms, etc. are user-defined from the language
point of view. Some of them (like std::string) do define operators like
== (as members or stand-alone functions). Most do not. You can find
more information on the available functionality if you just RTFM.

V
--
I do not respond to top-posted replies, please don't ask

Bonita Montero

6/12/2016 1:07:00 PM

0

Am 12.06.2016 um 14:34 schrieb Victor Bazarov:

> There is no operator== declared for std::vector. ...

http://www.cplusplus.com/reference/vector/vector/...

--
http://facebook.com/bonit...

Alf P. Steinbach /Usenet

6/12/2016 1:24:00 PM

0

* On 12.06.2016 14:34, Victor Bazarov wrote:
> * On 6/12/2016 7:02 AM, Paul wrote:
>> I don't seem to understand operators on vectors. I thought that you
>> could write someVec == {} as a statement saying that someVec is empty.
>>
>> However, this doesn't compile and I don't know why.
>> My error message is "expected primary-expression before '{' token".
>> I'd be grateful if someone could enlighten me. The complete code is
>> below.
>> Thanks again for your help.
>>
>> Paul
>>
>> #include <vector>
>>
>> int main()
>> {
>> std::vector<int> z;
>> if(z == {})
>> ; // empty statement.
>> }
>
> There is no operator== declared for std::vector. Equality operators are
> not compiler-defined for user-defined classes. The library classes like
> containers, streams, algorithms, etc. are user-defined from the language
> point of view. Some of them (like std::string) do define operators like
> == (as members or stand-alone functions). Most do not.

Adding to that...

With all standard containers (unless I'm mistaken, in which case, with
most all standard containers) you can use the misleadingly named member
function `empty` to check whether it's empty. Contrary to what the name
indicates, it does not empty the collection. It just checks.

std::bitset is not technically a container and has a differently named
empty checker, a method called `none`.

To get more readable code you can define an `is_empty` function for
vectors, like this:

template< class Item >
auto is_empty( std::vector<Item> const& v )
-> bool
{ return v.empty(); }

and then you can write more clearly

if( is_empty( z ) ) { ...

Defining such a wrapper for each container type of interest is some work
though. But defining a single general more smart wrapper that determines
if there is a boolean `empty` method and if so, uses it, is even more
work. I'm not posting my code for that, it would simply be too long, but
if you're interested you can look at it ¹at GitHub.

Cheers & hth.,

- Alf

Links:
¹
https://github.com/alf-p-steinbach/cppx/blob/master/source_code/cppx/core_language_support/sizes_and_l...

Alf P. Steinbach /Usenet

6/12/2016 1:34:00 PM

0

On 12.06.2016 15:06, Bonita Montero wrote:
> Am 12.06.2016 um 14:34 schrieb Victor Bazarov:
>
>> There is no operator== declared for std::vector. ...
>
> http://www.cplusplus.com/reference/vector/vector/...
>

Victor jumped to the wrong conclusion, but it was so reasonable that I
posted a follow-up to that just assuming that he was right.

The actual issue is, I believe, purely syntactical: that the `{}` is
only supported syntactically in a (syntactically) function call.

For you /can/ write this:

if( std::operator==( z, {} ) )
; // empty statement.


Cheers & hth.,

- Alf

Paavo Helde

6/12/2016 1:55:00 PM

0

On 12.06.2016 14:02, Paul wrote:
> I don't seem to understand operators on vectors. I thought that you could write someVec == {} as a statement saying that someVec is empty.
>
> However, this doesn't compile and I don't know why.
> My error message is "expected primary-expression before '{' token".
> I'd be grateful if someone could enlighten me. The complete code is below.
> Thanks again for your help.
>
> Paul
>
> #include <vector>
>
> int main()
> {
> std::vector<int> z;
> if(z == {})
> ; // empty statement.
> }
>

std::operator== is not a member of std::vector, it is a standalone
function which has many overloads. When you pass it z and {} it does not
know which overload because types are not the same and there is no exact
match.

There are a couple of workarounds:

a) cast/convert the operand into correct type manually:

if (z == std::vector<int>({}))


b) use a function which is not overloaded:

bool AreEqual(const std::vector<int>& a, const std::vector<int>& b) {
return a==b;
}

....
if (AreEqual(z, {}))

Of course, for the problem at hand you could rather use

if (z.empty())

instead, as suggested by Alf.

hth
Paavo

Paavo Helde

6/12/2016 2:15:00 PM

0

On 12.06.2016 16:34, Alf P. Steinbach wrote:
> On 12.06.2016 15:06, Bonita Montero wrote:
>> Am 12.06.2016 um 14:34 schrieb Victor Bazarov:
>>
>>> There is no operator== declared for std::vector. ...
>>
>> http://www.cplusplus.com/reference/vector/vector/...
>>
>
> Victor jumped to the wrong conclusion, but it was so reasonable that I
> posted a follow-up to that just assuming that he was right.
>
> The actual issue is, I believe, purely syntactical: that the `{}` is
> only supported syntactically in a (syntactically) function call.

It is supported also in initialization

std::vector<int> z = {};
std::vector<int> z2 {};

>
> For you /can/ write this:
>
> if( std::operator==( z, {} ) )
> ; // empty statement.
>

Sorry, this does not work:

1>main.cpp(26): error C2665: 'std::operator ==' : none of the 5
overloads could convert all the argument types
1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\system_error(410): could be 'bool std::operator ==(const
std::error_condition &,const std::error_code &) throw()'
1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\system_error(402): or 'bool std::operator ==(const
std::error_code &,const std::error_condition &) throw()'
1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\exception(507): or 'bool std::operator ==(const
std::exception_ptr &,std::nullptr_t)'
1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\exception(502): or 'bool std::operator
==(std::nullptr_t,const std::exception_ptr &)'
1> C:\Program Files (x86)\Microsoft Visual Studio
12.0\VC\include\exception(497): or 'bool std::operator ==(const
std::exception_ptr &,const std::exception_ptr &)'
1> while trying to match the argument list
'(std::vector<int,std::allocator<_Ty>>, initializer-list)'
1> with
1> [
1> _Ty=int
1> ]




Alf P. Steinbach /Usenet

6/12/2016 2:22:00 PM

0

On 12.06.2016 16:15, Paavo Helde wrote:
> On 12.06.2016 16:34, Alf P. Steinbach wrote:
>> On 12.06.2016 15:06, Bonita Montero wrote:
>>> Am 12.06.2016 um 14:34 schrieb Victor Bazarov:
>>>
>>>> There is no operator== declared for std::vector. ...
>>>
>>> http://www.cplusplus.com/reference/vector/vector/...
>>>
>>
>> Victor jumped to the wrong conclusion, but it was so reasonable that I
>> posted a follow-up to that just assuming that he was right.
>>
>> The actual issue is, I believe, purely syntactical: that the `{}` is
>> only supported syntactically in a (syntactically) function call.
>
> It is supported also in initialization
>
> std::vector<int> z = {};
> std::vector<int> z2 {};

Yep, thanks!


>> For you /can/ write this:
>>
>> if( std::operator==( z, {} ) )
>> ; // empty statement.
>>
>
> Sorry, this does not work:
>
> 1>main.cpp(26): error C2665: 'std::operator ==' : none of the 5
> overloads could convert all the argument types
> 1> C:\Program Files (x86)\Microsoft Visual Studio
> 12.0\VC\include\system_error(410):

It's a Good Idea to compile code for C++11 and later, with a compiler
that supports the relevant feature(s).

You should upgrade from VS 2012 to VS 2015.

The latest update is 3, but I've only gotten around to 2.


{example}
[C:\my\forums\so\156]
> g++ --version | find "++"
g++ (tdm64-1) 5.1.0

[C:\my\forums\so\156]
> cl /nologo- 2>&1 | find "++"
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23725 for x86

[C:\my\forums\so\156]
> type foo.cpp
#include <vector>

int main()
{
std::vector<int> z;
if( std::operator==( z, {} ) )
; // empty statement.
}

[C:\my\forums\so\156]
> g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:7:7: warning: suggest braces around empty body in an 'if'
statement [-Wempty-body]
; // empty statement.
^

[C:\my\forums\so\156]
> cl foo.cpp /Feb
foo.cpp
foo.cpp(7): warning C4390: ';': empty controlled statement found; is
this the intent?

[C:\my\forums\so\156]
> _
{/example}

Which exemplifies that I'm not always so pedantically correct in where I
place the small example code snippets.

Hrmf.


Cheers & hth.,

- Alf

Alf P. Steinbach /Usenet

6/12/2016 2:28:00 PM

0

On 12.06.2016 15:54, Paavo Helde wrote:
> On 12.06.2016 14:02, Paul wrote:
>>
>> â?¦ this doesn't compile and I don't know why.
>>
>> #include <vector>
>>
>> int main()
>> {
>> std::vector<int> z;
>> if(z == {})
>> ; // empty statement.
>> }
>>
>
> std::operator== is not a member of std::vector, it is a standalone
> function which has many overloads. When you pass it z and {} it does not
> know which overload because types are not the same and there is no exact
> match.

No no, passing `z` and `{}` /to the function/ is supported and works
nicely with modern compilers.

It's the operator notation that doesn't support `{}`.

Else-thread you remarked that it's not only supported syntactically for
function calls, but also for initializers. And I can add to that, also
for `return` statements. Which however can be regarded as initialization
of the returned object.

[snip]


Cheers & hth.,

- Alf

Paavo Helde

6/12/2016 2:30:00 PM

0

On 12.06.2016 17:15, Paavo Helde wrote:
> On 12.06.2016 16:34, Alf P. Steinbach wrote:
>>
>> For you /can/ write this:
>>
>> if( std::operator==( z, {} ) )
>> ; // empty statement.
>>
>
> Sorry, this does not work:

Correction: it seems this might accidentally work if one avoids
including most standard headers (the errors in my previous post appeared
because of #include <iostream>). However, this is not guaranteed because
standard headers like <vector> can freely include other standard headers.

Alf P. Steinbach /Usenet

6/12/2016 2:31:00 PM

0

On 12.06.2016 16:29, Paavo Helde wrote:
> On 12.06.2016 17:15, Paavo Helde wrote:
>> On 12.06.2016 16:34, Alf P. Steinbach wrote:
>>>
>>> For you /can/ write this:
>>>
>>> if( std::operator==( z, {} ) )
>>> ; // empty statement.
>>>
>>
>> Sorry, this does not work:
>
> Correction: it seems this might accidentally work if one avoids
> including most standard headers (the errors in my previous post appeared
> because of #include <iostream>). However, this is not guaranteed because
> standard headers like <vector> can freely include other standard headers.
>

I can report that it works niceley with `<iostream>` included, with g++
5.1 and MSVC 2015.

And there's no accident about it. :)


Cheers & hth.,

- Alf