473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Not-equal string searching

Hello,

Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.

#include <string>
#include <algorithm>
#include <functional>

std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}

Feb 9 '08 #1
15 1855
"DavidW" <no@email.providedwrote:
Is the function below the simplest way to produce an iterator to the next
non-space in a string?
No.
std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}
If you aren't using boost's lambda library then:

std::string::iterator find_not_space(std::string &s)
{
return find_if( s.begin(), s.end(),
bind2nd( not_equal_to<char>(), ' ' ) );
}

If you are using boost's lambda library:

std::string::iterator find_not_space(std::string &s)
{
return find_if( s.begin(), s.end(), _1 != ' ' );
}
Feb 9 '08 #2
DavidW wrote:
Hello,

Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.

#include <string>
#include <algorithm>
#include <functional>

std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}
a) You could use std::find_if with not_equal_to ' ' as the predicate. There
ought to be a way to fiddle around with binders to get it into a single
line. Or using lambda:

find_if( s.begin(), s.end(), _1 != ' ' );
b) Also notice the member function find_first_not_of of std::string. It
almost does what you want, except that it returns the index of the element
and not an iterator.

Best

Kai-Uwe Bux
Feb 9 '08 #3
On Feb 9, 11:39 pm, "DavidW" <n...@email.providedwrote:
Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.
#include <string>
#include <algorithm>
#include <functional>
std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}
I'm not sure I understand. If all you're looking for is the
next non-space, std::find_if should work using the standard
functional objects, e.g.:
std::find_if(
begin, end,
std::bind2nd( std::not_equal_to< char >( ' ' ) ) ) ;

Generally speaking, however, this isn't a good idea, since it
doesn't consider things like '\t' as spaces. I usually use
functional object wrappers for ctype<>::is(), with the
appropriate mask.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 9 '08 #4
On Feb 10, 12:12 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
DavidW wrote:
b) Also notice the member function find_first_not_of of
std::string. It almost does what you want, except that it
returns the index of the element and not an iterator.
To get the iterator, of course, just add it to begin(). I still
prefer std::find_if, since it's what I'd do with any other
container.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 9 '08 #5
"DavidW" <no@email.providedwrote:
Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.

#include <string>
#include <algorithm>
#include <functional>

std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}
Someone correct me on this, but isn't the above undefined behavior?
chSpace isn't an array so I don't think &chSpace + 1 is valid even if it
isn't dereferenced.

I think it would have to be something more like:

char chSpace[] = " ";
return search( s.begin(), s.end(), chSpace, chSpace + 1,
not_equal_to<char>() );
Feb 10 '08 #6
"Daniel T." <da******@earthlink.netwrote
"DavidW" <no@email.providedwrote:
Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.

#include <string>
#include <algorithm>
#include <functional>

std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}

Someone correct me on this, but isn't the above undefined behavior?
chSpace isn't an array so I don't think &chSpace + 1 is valid even if it
isn't dereferenced.
It would be invalid to dereference it even if it were an array (of length 1).
I think it would have to be something more like:

char chSpace[] = " ";
Except that the null terminator is a wasted character. Better would be: char
chSpace[] = {' '};
return search( s.begin(), s.end(), chSpace, chSpace + 1,
not_equal_to<char>() );
I don't know if it's undefined behaviour not to use an array. It seems an
unnecessary restriction if so.

Feb 10 '08 #7
James Kanze wrote:
On Feb 10, 12:12 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>DavidW wrote:
>b) Also notice the member function find_first_not_of of
std::string. It almost does what you want, except that it
returns the index of the element and not an iterator.

To get the iterator, of course, just add it to begin().
I am not sure whether the index-based string function mix so well with
iterators. I had a cursory look into the standard, but I was not able to
confirm what happens to

s.begin() + s.find_first_not_of( " " );

if find_first_not_of() does not find anything and returns npos. My gut tells
me, I better be worried that it might be undefined behavior.
I still
prefer std::find_if, since it's what I'd do with any other
container.
Agreed.
Best

Kai-Uwe Bux
Feb 10 '08 #8
"James Kanze" <ja*********@gmail.comwrote
>On Feb 9, 11:39 pm, "DavidW" <n...@email.providedwrote:
>Is the function below the simplest way to produce an iterator to the next
non-space in a string? (Or the upper-bound iterator if none is found).
Searching for a sequence is overkill and inefficient IMO.
>#include <string>
#include <algorithm>
#include <functional>
>std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}

I'm not sure I understand. If all you're looking for is the
next non-space, std::find_if should work using the standard
functional objects, e.g.:
std::find_if(
begin, end,
std::bind2nd( std::not_equal_to< char >( ' ' ) ) ) ;
Thanks, and to the others who suggested bind2nd. I wanted to use find_if and I
looked for such a predicate, but obviously not hard enough.

I would also like a std::find with a predicate, e.g., std::find(s.begin(),
s.end(), chSpace, std::not_equal_to<char>()). It looks a little neater. The
current std::find is specialized for equality.
>Generally speaking, however, this isn't a good idea, since it
doesn't consider things like '\t' as spaces. I usually use
functional object wrappers for ctype<>::is(), with the
appropriate mask.
In this case it's specific to the space character.

Feb 10 '08 #9
DavidW wrote:
"Daniel T." <da******@earthlink.netwrote
>"DavidW" <no@email.providedwrote:
[snip]
>
> return search( s.begin(), s.end(), chSpace, chSpace + 1,
not_equal_to<char>() );

I don't know if it's undefined behaviour not to use an array. It seems an
unnecessary restriction if so.
Yes, it is. It has to be an array object.

Krishanu
Feb 10 '08 #10
Daniel T. wrote:
...
Someone correct me on this, but isn't the above undefined behavior?
chSpace isn't an array so I don't think &chSpace + 1 is valid even if it
isn't dereferenced.
...
It is not undefined behavior. C++ standard explicitly states that rules of
pointer arithmetic are immediately applicable to a standalone non-array object,
as if this object is an array with 1 element (see 5.7/4)

--
Best regards,
Andrey Tarasevich
Feb 10 '08 #11
Krishanu Debnath wrote:
DavidW wrote:
>"Daniel T." <da******@earthlink.netwrote
>>"DavidW" <no@email.providedwrote:

[snip]
>>
>> return search( s.begin(), s.end(), chSpace, chSpace + 1,
not_equal_to<char>() );

I don't know if it's undefined behaviour not to use an array. It seems an
unnecessary restriction if so.

Yes, it is. It has to be an array object.
Could you please explain why you believe that? I'm pretty sure you're
mistaken.
Feb 10 '08 #12
On Feb 10, 1:37 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
James Kanze wrote:
On Feb 10, 12:12 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
DavidW wrote:
b) Also notice the member function find_first_not_of of
std::string. It almost does what you want, except that it
returns the index of the element and not an iterator.
To get the iterator, of course, just add it to begin().
I am not sure whether the index-based string function mix so well with
iterators. I had a cursory look into the standard, but I was not able to
confirm what happens to
s.begin() + s.find_first_not_of( " " );
if find_first_not_of() does not find anything and returns
npos. My gut tells me, I better be worried that it might be
undefined behavior.
Good point. It would be undefined behavior.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 10 '08 #13
On Feb 10, 12:58 am, "Daniel T." <danie...@earthlink.netwrote:
"DavidW" <n...@email.providedwrote:
Is the function below the simplest way to produce an
iterator to the next non-space in a string? (Or the
upper-bound iterator if none is found). Searching for a
sequence is overkill and inefficient IMO.
#include <string>
#include <algorithm>
#include <functional>
std::string::iterator find_not_space(std::string &s)
{
char chSpace = ' ';
return std::search(s.begin(), s.end(), &chSpace , &chSpace+1,
std::not_equal_to<char>());
}
Someone correct me on this, but isn't the above undefined behavior?
No.
chSpace isn't an array so I don't think &chSpace + 1 is valid
even if it isn't dereferenced.
For purposes of address calculation, a scalar object behaves
like an array with one element.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 10 '08 #14
Kai-Uwe Bux wrote:
James Kanze wrote:
>On Feb 10, 12:12 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>>DavidW wrote:
b) Also notice the member function find_first_not_of of
std::string. It almost does what you want, except that it
returns the index of the element and not an iterator.
To get the iterator, of course, just add it to begin().

I am not sure whether the index-based string function mix so well with
iterators. I had a cursory look into the standard, but I was not able to
confirm what happens to

s.begin() + s.find_first_not_of( " " );

if find_first_not_of() does not find anything and returns npos. My gut tells
me, I better be worried that it might be undefined behavior.
Yup, but good old strspn( s.c_str(), " " ) works better.

But I feel somewhat uneasy: should I replace a function with a clear
name, but confusing effect by another one with a confusing name, but a
straightforward result?

-- ralph
Feb 12 '08 #15
Andrey Tarasevich wrote:
Daniel T. wrote:
>...
Someone correct me on this, but isn't the above undefined behavior?
chSpace isn't an array so I don't think &chSpace + 1 is valid even if
it isn't dereferenced.
...

It is not undefined behavior. C++ standard explicitly states that rules
of pointer arithmetic are immediately applicable to a standalone
non-array object, as if this object is an array with 1 element (see 5.7/4)
Ok so far. But if I need an object, that must behave like an array of
size 1, I just define it as such:

char const chSpace[1] = " ";

It saves me from digging in the reference, and from using the address-of
operator.

-- ralph
Feb 12 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Phil Powell | last post by:
<b>Agency 4</b> <input type="radio" name="permission" value="1" checked> Yes <input type="radio" name="permission" value="0" > No <br> <b>Agency 5</b> <input type="radio" name="permission"...
2
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my...
52
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
5
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the...
33
by: Bob | last post by:
When I first started in web programming (I left it for more than a few years) I programmed my web pages in C and a little perl. Now that I have a need to come back to web programming I find that...
16
by: Andrea A | last post by:
Hi, i'm developing a website that will have an huge amount of visitors a day --> it will be a contest with 100.000$ of prize. I'm concerning about using a template class (and which one do you...
13
by: Bruce A. Julseth | last post by:
I'm a newbie to PHP and am have problems getting simple PHP page (php_test_page.php) to display correctly. The page is: <html> <body>
9
by: joealey2003 | last post by:
Hi all... A simple mail example like... <? mail("acco...@yahoo.com","Subject of Message","Message"); ?> does not work to yahoo or spymac.com, but the same works to gmail and other servers.
7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
198
by: Michael N. Christoff | last post by:
Java, the software developed by Sun Microsystems in the mid-1990s as a universal operating system for Internet applications, gave NASA a low-cost and easy-to-use option for running Spirit, the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.