不等于字符串搜索 [英] Not-equal string searching

查看:304
本文介绍了不等于字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


这个函数是否是最简单的方法来生成字符串中下一个

非空格的迭代器? (如果没有找到上限迭代器,则为上限迭代器。)

搜索序列是过度且效率低的IMO。


#include< string> < br $>
#include< algorithm>

#include< functional>

std :: string :: iterator find_not_space(std: :string& s)

{

char chSpace ='''';

返回std :: search(s.begin() ,s.end(),& chSpace,& chSpace + 1,

std :: not_equal_to< char>());

}

解决方案

" DavidW" < no@email.providedwrote:


这个函数下面是生成迭代器到下一个

非空格的最简单方法一个字符串?



No.


std :: string :: iterator find_not_space(std :: string& s )

{

char chSpace ='''';

返回std :: search(s.begin(),s.end( ),& chSpace,& chSpace + 1,

std :: not_equal_to< char>());

}



如果你没有使用boost'的lambda库那么:


std :: string :: iterator find_not_space(std :: string& s)

{

返回find_if(s.begin(),s.end(),

bind2nd(not_equal_to< char>(),' '''));

}


如果您使用的是boost'的lambda库:


std :: string :: iterator find_not_space(std :: string& s)

{

返回find_if(s.begin(),s.end(),_ 1 !='''');

}


DavidW写道:


你好,


函数是否是最简单的方法来生成字符串中下一个

非空格的迭代器? (如果没有找到上限迭代器,则为上限迭代器。)

搜索序列是过度且效率低的IMO。


#include< string> < br $>
#include< algorithm>

#include< functional>

std :: string :: iterator find_not_space(std: :string& s)

{

char chSpace ='''';

返回std :: search(s.begin() ,s.end(),& chSpace,& chSpace + 1,

std :: not_equal_to< char>());

}



a)你可以使用std :: find_if和not_equal_to'''作为谓词。

应该是一种方法来摆弄粘合剂,使其成为单一的

线。或者使用lambda:


find_if(s.begin(),s.end(),_ 1!='''');

b)另请注意std :: string的成员函数find_first_not_of。它几乎可以做你想要的,除了它返回元素的索引

而不是迭代器。


Best


Kai-Uwe Bux


2月9日晚上11:39,DavidW < n ... @ email.providedwrote:


这个函数下面是生成下一个迭代器的最简单方法

non字符串中的空格? (如果没有找到上限迭代器,则为上限迭代器。)

搜索序列是一种过度且效率低下的IMO。


#include< string>

#include< algorithm>

#include <功能>


std :: string :: iterator find_not_space(std :: string& s)

{

char chSpace ='''';

返回std :: search(s.begin(),s.end(),& chSpace,& chSpace + 1,

std :: not_equal_to< char>());

}



我不确定我理解。如果您正在寻找的是

下一个非空格,std :: find_if应该使用标准的

功能对象,例如:

std :: find_if(

begin,end,

std :: bind2nd(std :: not_equal_to< char>('''')));


但是,一般来说,这并不是一个好主意,因为它不会像''\\''那样将''\ t''视为空间。我通常使用

函数对象包装为ctype<> :: is(),使用

适当的掩码。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


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>());
}

解决方案

"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 != '' '' );
}


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


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


这篇关于不等于字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆