简体   繁体   English

在一个表达式中提升to_upper char指针

[英]boost to_upper char pointer in one expression

Is it possible to do something like: 有可能做这样的事情:

const char* str = "AaBbCc";
string str_up = boost::to_upper_copy(str); // str_up will be "AABBCC"

Last line doesn't compile. 最后一行无法编译。

Of course I can as below, but it less "canonical": 当然我可以如下,但它不那么“规范”:

const char* str = "AaBbCc";

string str_up = str;
boost::to_upper(str_up);

The declaration of to_upper_copy is: to_upper_copy的声明是:

template<typename SequenceT> 
SequenceT to_upper_copy(const SequenceT &, const std::locale & = std::locale());

From this it should be clear that SequenceT can't be a pointer to char, or even a char array, because there's no (good) way how the returned copy could have the same type. 从这一点可以清楚地看出, SequenceT不能是指向char,甚至是char数组的指针,因为没有(好的)方式返回的副本可以具有相同的类型。

You can explicitly force the type to be string : 您可以显式强制类型为string

string str_up = boost::to_upper_copy<string>(str); 

Here's explanation what SequenceT and RangeT mean in the documentation: String Representation . 这里解释了SequenceTRangeT在文档中的含义: 字符串表示 In short, neither can be a const char* , but RangeT arguments accept arrays ( char [X] ). 简而言之,两者都不能是const char* ,但RangeT参数接受数组( char [X] )。

Why not post the right solution as an answer to the question? 为什么不发布正确的解决方案作为问题的答案?

 const char* str = "AaBbCc";
 std::string str_up = boost::to_upper_copy(std::string(str));

Credits to Konrad. 对康拉德的信用。

Sure! 当然!

inline std::string to_upper_copy(std::string const & src) {
   std::string res(src);
   boost::to_upper(res);
   return res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM