使用const std::字符串指针作为函数参数 Pr>多年来编写java之后,我想深入研究C++。
虽然我认为我能处理好它,但我不知道我是否能以“最先进的”方式处理它 目前,我试图理解如何处理作为常量指针传递给方法的std::字符串作为参数 在我的理解中,我想对指针的内容(实际字符串)执行的任何字符串操作都是不可能的,因为它是常量 我有一个方法,可以将给定的字符串转换成小写,为了使给定的字符串可编辑,我做了大量的工作(我相信)。看看:使用const std::字符串指针作为函数参数 Pr>多年来编写java之后,我想深入研究C++。,c++,C++,虽然我认为我能处理好它,但我不知道我是否能以“最先进的”方式处理它 目前,我试图理解如何处理作为常量指针传递给方法的std::字符串作为参数 在我的理解中,我想对指针的内容(实际字符串)执行的任何字符串操作都是不可能的,因为它是常量 我有一个方法,可以将给定的字符串转换成小写,为了使给定的字符串可编辑,我做了大量的工作(我相信)。看看: class Util { public: static std::string toLower(const std::string& word)
class Util
{
public:
static std::string toLower(const std::string& word)
{
// in order to make a modifiable string from the const parameter
// copy into char array and then instantiate new sdt::string
int length = word.length();
char workingBuffer[length];
word.copy(workingBuffer, length, 0);
// create modifiable string
std::string str(workingBuffer, length);
std::cout << str << std::endl;
// string to lower case (include <algorithm> for this!!!!)
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::cout << str << std::endl;
return str;
}
};
class-Util
{
公众:
静态标准::字符串下限(常量标准::字符串和单词)
{
//为了从const参数生成可修改的字符串
//复制到char数组中,然后实例化新的sdt::string
int length=word.length();
字符工作缓冲区[长度];
复制(工作缓冲区,长度,0);
//创建可修改字符串
std::string str(工作缓冲区,长度);
std::cout您应该替换所有这些:
// in order to make a modifiable string from the const parameter
// copy into char array and then instantiate new sdt::string
int length = word.length();
char workingBuffer[length];
word.copy(workingBuffer, length, 0);
// create modifiable string
std::string str(workingBuffer, length);
简单来说:
std::string str(word);
它应该可以正常工作=)参数是const
(它是引用而不是指针!),但这并不妨碍您复制它:
// create modifiable string
std::string str = word;
也就是说,为什么首先要将参数设为const
引用?使用const
引用可以避免参数被复制,但如果您无论如何都需要副本,那么只需使用副本即可:
std::string toLower(std::string word) {
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
// ....
<>记住C++不是java,值是不是引用,即拷贝是真拷贝,修改函数<代码> Word < /C>函数对传递给函数的参数没有任何影响。 ,因为你必须复制输入字符串,你也可以按值取。(使用命名空间也比使用包含静态成员的类更好):
查看我为何转换为未签名字符
请查看本书的前几章;)你的代码中没有指向字符串的常量指针……如果你有一个只包含静态成员函数的类,名称空间
更合适……@Aconcagua我喜欢有人用更好的词重新表述我的草率评论,我可以删除我的:P@Aconcagua值得一提的是,这些名称空间函数将是静态的
默认情况下。@Ron我知道你的意思,但你的措辞完全错了:因为函数现在驻留在一个名称空间中,static
改变了它的含义,并且会限制对当前编译单元的可见性!默认可见性是“extern”,不过……或者简单地说:return convert\u to\u lower(str)
…不确定我是否希望将void作为第一个函数的返回值,即使(或perhpas,因为?)。这会阻止像convert\u to\u lower(str)这样的代码。推回(n)
考虑到OP thinkstring&
是一个指针,你应该解释一下拥有这两个指针的意义overloads@Aconcagua返回convert_to_lower(str);
真的这样做吗?不应该创建新的字符串。@Walter[@+#$!],您是对的!!!假设返回值和引用将产生相同的代码,但即使使用std::string&ss=str;return ss;
而不使用中间函数调用(您永远不会停止学习…)也会证明这是错误的.但这难道不是让转换返回无效的一个论点吗?谢谢!这似乎比我问的问题要远一点,我非常感激。对我来说,所有的事情都是了解和体验如何处理我的“问题”.对于这里的解决方案,我还需要再思考一下,这意味着什么。正如我在上面的评论中所说的,参数为const只是为了测试目的。我没有意识到std::string str=word;会从字符串中删除常量属性。因此,事实上,该表达式给了我一个“word”的副本。我测试了它,然后当然,它工作得很好。谢谢!@Kiamur它没有“从字符串中删除常量属性”对不起,但我仍然在那句话中听到了java的原理;)。在std::string str=word;
中str
是一个(非常量)从word的副本创建的字符串。就像你拿一支粉笔把你的护照上的id号复制到黑板上一样。你在黑板上写的id不是常量,但你护照上的id仍然刚刚意识到我一直在自动驾驶仪上写const std::string&
,期待Java在C上发生++是的,它是有效的,事实上这正是我一直在寻找的。任何提示,我到底可以在哪里找到某种文档,很好地解释,用这种构造,新的str不再是const?我希望你不介意这个问题。我一直在自己挖掘,但也许你们中的一些人有一些很棒的教程,在哪里描述得非常好。谢谢!(我接受这个作为我问题的答案,因为它是所有问题中最直接的一个。如上所述,非常感谢所有其他输入!)@Kiamurconst std::string&word
表示您传递的不是实际字符串,而是常量引用,编译器不允许您修改此参数(除非你做一些特殊的施法技巧),这样通过传递一个引用而不是整个对象来节省内存。通常的做法是将参数作为const
传递,以确保它们不会被修改。而str
只是参数的本地副本,可以修改,因为你需要它。
namespace util {
// modifies the input string (taken by reference), then returns a reference to
// the modified string
inline std::string&convert_to_lower(std::string&str)
{
for(auto&c : str)
c = std::tolower(static_cast<unsigned char>(c));
return str;
}
// returns a modified version of the input string, taken by value such that
// the passed string at the caller remains unaltered
inline std::string to_lower(std::string str)
{
// str is a (deep) copy of the string provided by caller
convert_to_lower(str);
// return-value optimisation ensures that no deep copy is made upon return
return str;
}
}
std::string str = "Hello";
auto str1 = util::to_lower(str);
std::cout << str << ", " << str1 << std::endl;
Hello, hello