std::find() in wx-2.9.4

19 views
Skip to first unread message

Igor Korot

unread,
May 14, 2013, 3:06:09 AM5/14/13
to wx-u...@googlegroups.com
Hi, ALL,
In my program I'm using std::find() to find an element of std::vector<wxString>.

However, to my surprise using this code:

std::find( myvec.begin(), myvec.end(), "1B" ) == myvec.end();

always return false even if myvec consist of "1B".

What am I missing? Do I need to pass 'L"1B"' to std::find()? But then what will happen on
other platforms?

wx-2.9.4
MSVC-2010

Thank you.

al...@arlross.demon.co.uk

unread,
May 14, 2013, 3:42:34 AM5/14/13
to wx-u...@googlegroups.com
Hi,

Of course the statement as shown does not return anything; do you mean that the find returns an iterator testing equal to myvec.end() in all cases; or that (std::find(.....)  == myvec.end) evaluates to false in all cases?

Of course "1B" is of type const char *.  (And has a value equal to the address where this literal is stored.)  You may be relying here on an implicit conversion of this to a wxString.  Ask yourself what is the type of an iterator into the vector.  Hint: with the vector container, is it, effectively, a pointer - in this case a char *? And if so, even if the vector contains a element constructed out of a "1B", this element will, almost certainly, not be at the same location as the address of the "1B" literal being used in the comparison.  No?  So the use of char * will allow the code to compile, but not to give the effect you're looking for.  (ISTM that there is an analogy here with string comparison of two literal values: strcmp() can find these equal - but a comparison of their addresses would show inequality.)  Can you use the debugger to view the relevant iterator values in your example?  If so, what do you see?

In the case that the vector contains a wxString constructed out of "1B", I'd expect the find to return an iterator not equal to myvec.end(); and equal in all other cases.  Is this what you're expecting, and not finding?  If so, could you post a little more of your test and diagnostic code that is failing?

HTH, and

BR,

Alec


From: wx-u...@googlegroups.com [wx-u...@googlegroups.com] on behalf of Igor Korot [ikor...@gmail.com]
Sent: 14 May 2013 08:06
To: wx-u...@googlegroups.com
Subject: std::find() in wx-2.9.4

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
 
 

Igor Korot

unread,
May 14, 2013, 10:59:06 AM5/14/13
to wx-u...@googlegroups.com
Hi, Alec,

On Tue, May 14, 2013 at 12:42 AM, al...@arlross.demon.co.uk <al...@arlross.demon.co.uk> wrote:
Hi,

Of course the statement as shown does not return anything; do you mean that the find returns an iterator testing equal to myvec.end() in all cases; or that (std::find(.....)  == myvec.end) evaluates to false in all cases?

The latter.
 

Of course "1B" is of type const char *.  (And has a value equal to the address where this literal is stored.)  You may be relying here on an implicit conversion of this to a wxString.  Ask yourself what is the type of an iterator into the vector.  Hint: with the vector container, is it, effectively, a pointer - in this case a char *? And if so, even if the vector contains a element constructed out of a "1B", this element will, almost certainly, not be at the same location as the address of the "1B" literal being used in the comparison.  No?  So the use of char * will allow the code to compile, but not to give the effect you're looking for.  (ISTM that there is an analogy here with string comparison of two literal values: strcmp() can find these equal - but a comparison of their addresses would show inequality.)  Can you use the debugger to view the relevant iterator values in your example?  If so, what do you see?

In the case that the vector contains a wxString constructed out of "1B", I'd expect the find to return an iterator not equal to myvec.end(); and equal in all other cases.

That was my understanding as well.
 
  Is this what you're expecting, and not finding?  If so, could you post a little more of your test and diagnostic code that is failing?

Not sure what else to post. This statement

bool res2 = std::find( positions.begin(), positions.end(), "1B" ) == positions.end();

evaluates res2 as false in all cases, whether positions vector contains "1B" string or not.

Thank you.
 

Eran Ifrah

unread,
May 14, 2013, 11:41:44 AM5/14/13
to wx-u...@googlegroups.com
On Tue, May 14, 2013 at 5:59 PM, Igor Korot <ikor...@gmail.com> wrote:
Hi, Alec,

On Tue, May 14, 2013 at 12:42 AM, al...@arlross.demon.co.uk <al...@arlross.demon.co.uk> wrote:
Hi,

Of course the statement as shown does not return anything; do you mean that the find returns an iterator testing equal to myvec.end() in all cases; or that (std::find(.....)  == myvec.end) evaluates to false in all cases?

The latter.
 

Of course "1B" is of type const char *.  (And has a value equal to the address where this literal is stored.)  You may be relying here on an implicit conversion of this to a wxString.  Ask yourself what is the type of an iterator into the vector.  Hint: with the vector container, is it, effectively, a pointer - in this case a char *? And if so, even if the vector contains a element constructed out of a "1B", this element will, almost certainly, not be at the same location as the address of the "1B" literal being used in the comparison.  No?  So the use of char * will allow the code to compile, but not to give the effect you're looking for.  (ISTM that there is an analogy here with string comparison of two literal values: strcmp() can find these equal - but a comparison of their addresses would show inequality.)  Can you use the debugger to view the relevant iterator values in your example?  If so, what do you see?

In the case that the vector contains a wxString constructed out of "1B", I'd expect the find to return an iterator not equal to myvec.end(); and equal in all other cases.

That was my understanding as well.
 
  Is this what you're expecting, and not finding?  If so, could you post a little more of your test and diagnostic code that is failing?

Not sure what else to post. This statement

bool res2 = std::find( positions.begin(), positions.end(), "1B" ) == positions.end();

evaluates res2 as false in all cases, whether positions vector contains "1B" string or not.

I just tried it with g++ / mingw - works as advertised (i.e. res2 is false when "1B" exists in positions vector, else its true)
Maybe its a compiler thing...
Have you tried wrapping "1B" in a wxString() ? like this:

bool res2 = std::find( positions.begin(), positions.end(), wxString("1B") ) == positions.end();



--
Eran Ifrah
Author of codelite, a cross platform open source C/C++ IDE: http://www.codelite.org
wxCrafter, a wxWidgets RAD: http://wxcrafter.codelite.org

al...@arlross.demon.co.uk

unread,
May 14, 2013, 12:36:11 PM5/14/13
to wx-u...@googlegroups.com
Hi Igor,

Apart from Eran's suggestions, if this remains unsolved, can you post - to the list, privately to me, or somewhere else - a very, very, small, but complete, example of code that shows the problem?

Alec


Sent: 14 May 2013 15:59
To: wx-u...@googlegroups.com
Subject: Re: std::find() in wx-2.9.4

Igor Korot

unread,
May 15, 2013, 3:38:47 PM5/15/13
to wx-u...@googlegroups.com
Hi, Eran,

On Tue, May 14, 2013 at 8:41 AM, Eran Ifrah <eran....@gmail.com> wrote:



On Tue, May 14, 2013 at 5:59 PM, Igor Korot <ikor...@gmail.com> wrote:
Hi, Alec,

On Tue, May 14, 2013 at 12:42 AM, al...@arlross.demon.co.uk <al...@arlross.demon.co.uk> wrote:
Hi,

Of course the statement as shown does not return anything; do you mean that the find returns an iterator testing equal to myvec.end() in all cases; or that (std::find(.....)  == myvec.end) evaluates to false in all cases?

The latter.
 

Of course "1B" is of type const char *.  (And has a value equal to the address where this literal is stored.)  You may be relying here on an implicit conversion of this to a wxString.  Ask yourself what is the type of an iterator into the vector.  Hint: with the vector container, is it, effectively, a pointer - in this case a char *? And if so, even if the vector contains a element constructed out of a "1B", this element will, almost certainly, not be at the same location as the address of the "1B" literal being used in the comparison.  No?  So the use of char * will allow the code to compile, but not to give the effect you're looking for.  (ISTM that there is an analogy here with string comparison of two literal values: strcmp() can find these equal - but a comparison of their addresses would show inequality.)  Can you use the debugger to view the relevant iterator values in your example?  If so, what do you see?

In the case that the vector contains a wxString constructed out of "1B", I'd expect the find to return an iterator not equal to myvec.end(); and equal in all other cases.

That was my understanding as well.
 
  Is this what you're expecting, and not finding?  If so, could you post a little more of your test and diagnostic code that is failing?

Not sure what else to post. This statement

bool res2 = std::find( positions.begin(), positions.end(), "1B" ) == positions.end();

evaluates res2 as false in all cases, whether positions vector contains "1B" string or not.

I just tried it with g++ / mingw - works as advertised (i.e. res2 is false when "1B" exists in positions vector, else its true)
Maybe its a compiler thing...
Have you tried wrapping "1B" in a wxString() ? like this:

bool res2 = std::find( positions.begin(), positions.end(), wxString("1B") ) == positions.end();

Wrapping it up in wxString() solves it.

Thank you.

P.S. Now I need to find out where is the flow in my logic and how to overcome it... ;-)

Eran Ifrah

unread,
May 15, 2013, 4:17:01 PM5/15/13
to wx-u...@googlegroups.com
The bug is that std::find tried to do a naive compare between between two "const char*"
instead of comparing the content - the address was compared. Wrapping it in wxString forced it to use the == operator of wxString
Reply all
Reply to author
Forward
0 new messages