std::string 与函数的字符串文字

发布于 2024-09-08 13:49:32 字数 400 浏览 8 评论 0原文

我想知道,我通常在代码中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若相惜即相离 2024-09-15 13:49:32

如果您想使用类,最好的方法是使用 const 引用:

bool Message::hasTag(const string& tag);

这样可以最大限度地减少冗余复制,并且可以清楚地表明该方法并不打算修改参数。我认为一个聪明的编译器可以在使用字符串文字调用时发出非常好的代码。

传递字符指针需要您使用 strcmp() 进行比较,因为如果您直接使用 == 开始比较指针,将会出现...麻烦。

If you want to use classes, the best approach here is a const reference:

bool Message::hasTag(const string& tag);

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.

迷爱 2024-09-15 13:49:32

简短的回答:这取决于。

长答案:std::string非常有用,因为它为字符串提供了很多实用函数(搜索子字符串、提取子字符串、连接字符串等)。它还为您管理内存,因此字符串的所有权不会混淆。

就您而言,您也不需要。您只需要知道 m_tags 中的任何对象是否与给定的字符串匹配。因此,对于您的情况,使用 const char *s 编写函数就完全足够了。

但是,作为脚注:在谈论返回值时,您几乎总是希望使用 std::string 而不是 (const) char *返回值。这是因为 C 字符串根本没有所有权语义,因此需要非常仔细地记录返回 const char * 的函数,解释谁拥有所指向的内存(调用者或被调用者),并且以防万一被调用者获取它,如何释放它(delete[]deletefree,其他)。

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 a const 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 a const 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).

苏大泽ㄣ 2024-09-15 13:49:32

我认为传递引用而不是字符串的值就足够了。我的意思是:

bool Message::hasTag(const string& tag)

这只会复制对原始 string 值的引用。无论如何,它必须在某个地方创建,但在函数之外。该函数不会复制其任何参数。

由于 m_tags 无论如何都是字符串向量(我想),const string& 参数将是更好的主意。

I think it would be enough to pass an reference rather than value of string. I mean:

bool Message::hasTag(const string& tag)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文