std::string 与函数的字符串文字
我想知道,我通常在代码中使用 std::string ,但是当您在参数中传递字符串进行简单比较时,是否最好只使用文字?
考虑这个函数:
bool Message::hasTag(string tag)
{
for(Uint tagIndex = 0; tagIndex < m_tags.size();tagIndex++)
{
if(m_tags[tagIndex] == tag)
return 0;
}
return 1;
}
尽管事实上它进行比较的属性是一个向量,并且使用这个函数的任何东西都可能将字符串传递给它,但使用 const char* 来避免创建一个新字符串会更好吗?无论如何都会像字符串文字一样使用吗?
I was wondering, I normally use std::string for my code, but when you are passing a string in a parameter for a simply comparison, is it better to just use a literal?
Consider this function:
bool Message::hasTag(string tag)
{
for(Uint tagIndex = 0; tagIndex < m_tags.size();tagIndex++)
{
if(m_tags[tagIndex] == tag)
return 0;
}
return 1;
}
Despite the fact that the property it is making a comparison with is a vector, and whatever uses this function will probably pass strings to it, would it still be better to use a const char* to avoid creating a new string that will be used like a string literal anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想使用类,最好的方法是使用 const 引用:
这样可以最大限度地减少冗余复制,并且可以清楚地表明该方法并不打算修改参数。我认为一个聪明的编译器可以在使用字符串文字调用时发出非常好的代码。
传递字符指针需要您使用
strcmp()
进行比较,因为如果您直接使用==
开始比较指针,将会出现...麻烦。If you want to use classes, the best approach here is a const reference:
That way, redudant copying can be minimized and it's made clear that the method doesn't intend to modify the argument. I think a clever compiler can emit pretty good code for the case when this is called with a string literal.
Passing a character pointer requires you to use
strcmp()
to compare, since if you start comparing pointers directly using==
, there will be ... trouble.简短的回答:这取决于。
长答案:std::string非常有用,因为它为字符串提供了很多实用函数(搜索子字符串、提取子字符串、连接字符串等)。它还为您管理内存,因此字符串的所有权不会混淆。
就您而言,您也不需要。您只需要知道
m_tags
中的任何对象是否与给定的字符串匹配。因此,对于您的情况,使用 const char *s 编写函数就完全足够了。但是,作为脚注:在谈论返回值时,您几乎总是希望使用
std::string
而不是(const) char *
。返回值。这是因为 C 字符串根本没有所有权语义,因此需要非常仔细地记录返回 const char * 的函数,解释谁拥有所指向的内存(调用者或被调用者),并且以防万一被调用者获取它,如何释放它(delete[]
,delete
,free
,其他)。Short answer: it depends.
Long answer:
std::string
is highly useful because it provides a lot of utility functions for strings (searching for substrings, extracting substrings, concatenating strings etc.). It also manages the memory for you, so the ownership of the string cannot be confused.In your case, you don't need either. You just need to know whether any of the objects in
m_tags
matches the given string. So for your case, writing the function using aconst char *s
is perfectly sufficient.However, as a foot note: you almost always want to prefer
std::string
over(const) char *
when talking about return values. That's because C strings have no ownership semantics at all, so a function returning aconst char *
needs to be documented very carefully, explaining who owns the pointed to memory (caller or callee) and, in case the callee gets it, how to free it (delete[]
,delete
,free
, something else).我认为传递引用而不是字符串的值就足够了。我的意思是:
这只会复制对原始
string
值的引用。无论如何,它必须在某个地方创建,但在函数之外。该函数不会复制其任何参数。由于 m_tags 无论如何都是字符串向量(我想),const string& 参数将是更好的主意。
I think it would be enough to pass an reference rather than value of
string
. I mean:That would copy only the reference to the original
string
value. Which must be created somwhere anyway, but outside of the function. This function would not copy its parameter whatsoever.Since
m_tags
is a vector of strings anyway (I suppose),const string&
parameter would be better idea.