ptr_fun&让人困惑 [英] ptr_fun & tolower confusion

查看:62
本文介绍了ptr_fun&让人困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个混合大小写字符串转换为小写字符串。我试过

代码:


std :: transform(mixedCaseString.begin(),mixedCaseString :: end(),

mixedCaseString.begin(),std :: ptr_fun(tolower));


即使我包括cctype和算法,我也会得到编译器(g

++ 3.3.6)错误:


没有匹配函数来调用`ptr_fun(< unknown type>)''

>
我只能通过使用:: tolower来解决这个问题。而不是tolower。

然后我开始使用谷歌搜索。它在我看来

这是不安全的。与

类似主题的许多类型的回复相混淆。


有人能指出我的**安全(便携式),更少 - 麻烦**

使用std :: transform或任何其他算法更改std :: string

的方法?使用boost也是可以接受的(但是我没有比使用shared_ptr和polymorphic_cast更多地使用提升其他的

)给我。


问候,

~Soumen

解决方案

Soumen写道:


我想将一个混合大小写字符串转换为小写字符串。我试过

代码:


std :: transform(mixedCaseString.begin(),mixedCaseString :: end(),

mixedCaseString.begin(),std :: ptr_fun(tolower));


即使我包括cctype和算法,我也会得到编译器(g

++ 3.3.6)错误:


没有匹配函数来调用`ptr_fun(< unknown type>)''

>
我只能通过使用:: tolower来解决这个问题。而不是tolower。

然后我开始使用谷歌搜索。它在我看来

这是不安全的。与

类似主题的许多类型的回复相混淆。


有人能指出我的**安全(便携式),更少 - 麻烦**

使用std :: transform或任何其他算法更改std :: string

的方法?使用boost也可以接受(但是我没有使用过比使用shared_ptr和polymorphic_cast更多的其他

)。



从存档中略微修改:

#include< tr1 / memory>

#include< cstdlib>

#include< locale>


template< typename CharT>

class to_lower {


typedef std :: ctype< CharT char_type;


std :: tr1 :: shared_ptr< std :: locale the_loc_ptr;

char_type const * the_type_ptr;


public:


to_lower(std :: locale const& r_loc = std :: locale())

:the_loc_ptr(new std :: locale(r_loc))

,the_type_ptr(& std :: use_facet< ; char_type>(* the_loc_ptr))

{}


CharT operator()(CharT chr)const {

return (the_type_ptr-> tolower(chr));

}


};

这与std一起使用: :转换如下:


std :: transform(mixedCaseString.begin(),mixedCaseString :: end(),

mixedCaseString.begin(),

to_lower< char>());


你也可以从不同的语言环境初始化to_lower。

Best


Kai-Uwe Bux


std :: transform(mixedCaseString.begin(),mixedCaseString :: end(),
< blockquote class =post_quotes>
mixedCaseString.begin(),的std :: ptr_fun(tolower的));



不存在语法错误 - 它应该是一个点而不是

的冒号。


下面的代码片段适用于gcc 3.4.6


#include< iostream>

#include< string>

#include< algorithm>

#include< iterator>

#include< cctype>


int main()

{

std :: string str(MARY HAD A LITTLE LAMB);


std :: transform(str.begin(),str.end(),

str.begin(),

std :: ptr_fun(tolower));


std :: copy(str.begin(),str.end(),

std :: ostream_iterator< char>(std :: cout)) ;

返回0;

}


7月4日,2:55 * pm,Amal Pillai< amal.pil ... @ gmail.comwrote:


std :: transform(mixedCaseString.begin(),mixedCaseString :: end (),

m ixedCaseString.begin(),std :: ptr_fun(tolower));



不存在语法错误 - 它应该是一个点而不是
冒号。



是的,帖子中有一个拼写错误。谢谢你的指点。但是在实际代码中,它是一个点。

即使这样我也会收到错误。只有:: tolower解决了这个错误。


以下代码片段适用于gcc 3.4.6


#include < iostream>

#include< string>

#include< algorithm>

#include< iterator>

#include< cctype>


int main()

{

* * std :: string str (MARY HAD A LITTLE LAMB);


* * std :: transform(str.begin(),str.end(),

* * * * * * * * * * str.begin(),

* * * * * * * * * * std :: ptr_fun(tolower));

* * std :: copy(str.begin(),str.end(),

std :: ostream_iterator< char>(std :: cout));

* *返回0;


}


I wanted convert a mixed case string to a lower case one. And I tried
following code:

std::transform(mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(), std::ptr_fun(tolower));

Even though I''s including cctype and algorithm, I''s getting compiler (g
++ 3.3.6) error:

no matching function for call to `ptr_fun(<unknown type>)''

I could resolve this only by using "::tolower" instead of "tolower".
But then I started googling. And it looks to me
this is not safe. And got confused with many types of responses on
similar topic.

Can someone point me what''s the **safe (portable), less-cumbersome**
way to change case of an std::string
using std::transform or any other algorithm? Using boost is also
acceptable (but I''ve not used boost much other
than using shared_ptr and polymorphic_cast) to me.

Regards,
~ Soumen

解决方案

Soumen wrote:

I wanted convert a mixed case string to a lower case one. And I tried
following code:

std::transform(mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(), std::ptr_fun(tolower));

Even though I''s including cctype and algorithm, I''s getting compiler (g
++ 3.3.6) error:

no matching function for call to `ptr_fun(<unknown type>)''

I could resolve this only by using "::tolower" instead of "tolower".
But then I started googling. And it looks to me
this is not safe. And got confused with many types of responses on
similar topic.

Can someone point me what''s the **safe (portable), less-cumbersome**
way to change case of an std::string
using std::transform or any other algorithm? Using boost is also
acceptable (but I''ve not used boost much other
than using shared_ptr and polymorphic_cast) to me.


Slightly modified from the archive:
#include <tr1/memory>
#include <cstdlib>
#include <locale>

template < typename CharT >
class to_lower {

typedef std::ctype< CharT char_type;

std::tr1::shared_ptr< std::locale the_loc_ptr;
char_type const * the_type_ptr;

public:

to_lower ( std::locale const & r_loc = std::locale() )
: the_loc_ptr ( new std::locale ( r_loc ) )
, the_type_ptr ( &std::use_facet< char_type >( *the_loc_ptr ) )
{}

CharT operator() ( CharT chr ) const {
return ( the_type_ptr->tolower( chr ) );
}

};
This is to be used with std::transform like so:

std::transform( mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(),
to_lower<char>() );

You could also initialize to_lower from a different locale.
Best

Kai-Uwe Bux


std::transform(mixedCaseString.begin(), mixedCaseString::end(),

mixedCaseString.begin(), std::ptr_fun(tolower));

Isn''t there a syntax error - it should be a dot instead
of colons.

The following snippet works fine for me with gcc 3.4.6

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>

int main()
{
std::string str("MARY HAD A LITTLE LAMB");

std::transform(str.begin(), str.end(),
str.begin(),
std::ptr_fun(tolower));

std::copy (str.begin(), str.end(),
std::ostream_iterator<char>(std::cout));
return 0;
}


On Jul 4, 2:55*pm, Amal Pillai <amal.pil...@gmail.comwrote:

std::transform(mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(), std::ptr_fun(tolower));


Isn''t there a syntax error - it should be a dot instead
of colons.

Yes, there''s a typo _here_ in the posting. Thanks for pointing. But in
actual code, it''s a dot.
Even then I''s getting the error. Only ::tolower resolved the error.

The following snippet works fine for me with gcc 3.4.6

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>

int main()
{
* * std::string str("MARY HAD A LITTLE LAMB");

* * std::transform(str.begin(), str.end(),
* * * * * * * * * *str.begin(),
* * * * * * * * * *std::ptr_fun(tolower));

* * std::copy (str.begin(), str.end(),
std::ostream_iterator<char>(std::cout));
* * return 0;

}


这篇关于ptr_fun&amp;让人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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