gpt4 book ai didi

c++ - std::string 常量的正确习语?

转载 作者:IT老高 更新时间:2023-10-28 13:00:16 25 4
gpt4 key购买 nike

我有一张代表数据库对象的 map 。我想从中获得“众所周知”的值(value)

 std::map<std::string, std::string> dbo;
...
std::string val = map["foo"];

一切都很好,但让我感到震惊的是,“foo”在每次调用时都被转换为临时字符串。当然,最好有一个常量 std::string (当然,与刚刚获取对象的磁盘 IO 相比,它可能是一个很小的开销,但我认为它仍然是一个有效的问题)。那么 std::string 常量的正确用法是什么?

例如-我可以拥有

 const std::string FOO = "foo";

在 hdr 中,但后来我得到了多个拷贝

编辑:还没有答案说明如何声明 std::string 常量。忽略整个 map 、STL 等问题。很多代码都是面向 std::string 的(我的当然是),很自然地需要常量而不为内存分配反复支付

EDIT2:从 Manuel 那里取出 PDF 回答的第二个问题,添加了坏习语的例子

EDIT3:答案摘要。请注意,我没有包括那些建议创建新字符串类的内容。我很失望,因为我希望有一个简单的东西只能在头文件中工作(比如 const char * const )。反正

a) 来自马克 b

 std::map<int, std::string> dict;
const int FOO_IDX = 1;
....
dict[FOO_IDX] = "foo";
....
std:string &val = dbo[dict[FOO_IDX]];

b) 来自弗拉德

 // str.h
extern const std::string FOO;
// str.cpp
const std::string FOO = "foo";

c) 来自 Roger P

 // really you cant do it

(b) 似乎最接近我想要的,但有一个致命缺陷。我不能拥有使用这些字符串的静态模块级代码,因为它们可能尚未构建。我考虑过(a)并且实际上在序列化对象时使用了类似的技巧,发送索引而不是字符串,但是对于通用解决方案来说似乎需要很多管道。所以遗憾的是 (c) 赢了,std:string 没有 simple const idiom

最佳答案

“字符串文字优化”的复制和缺乏正是 std::strings 的工作方式,您无法准确获得所需的内容。部分原因是因为明确避免了虚拟方法和 dtor。无论如何,std::string 接口(interface)很多很复杂。

该标准要求 std::string 和 std::map 都有特定的接口(interface),而这些接口(interface)恰好不允许您想要的优化(作为其其他要求的“意外后果”,而不是明确地)。至少,如果您想真正遵循标准的所有细节,他们不允许这样做。而且您确实想要这样,尤其是当使用不同的字符串类进行此特定优化非常容易时。

但是,单独的字符串类可以解决这些“问题”(正如您所说,这很少成为问题),但不幸的是,世界上已经有 number_of_programmers + 1 个。即使考虑到轮子的重新发明,我发现拥有一个具有 std::string 接口(interface)子集的 StaticString 类很有用:使用 begin/end、substr、find 等。它也不允许修改(并且适合字符串文字这样),只存储一个字符指针和一个大小。您必须稍微小心,它仅使用字符串文字或其他“静态”数据进行初始化,但构造接口(interface)在一定程度上缓解了这一点:

struct StaticString {
template<int N>
explicit StaticString(char (&data)[N]); // reference to char array
StaticString(StaticString const&); // copy ctor (which is very cheap)

static StaticString from_c_str(char const* c_str); // static factory function
// this only requires that c_str not change and outlive any uses of the
// resulting object(s), and since it must also be called explicitly, those
// requirements aren't hard to enforce; this is provided because it's explicit
// that strlen is used, and it is not embedded-'\0'-safe as the
// StaticString(char (&data)[N]) ctor is

operator char const*() const; // implicit conversion "operator"
// here the conversion is appropriate, even though I normally dislike these

private:
StaticString(); // not defined
};

用途:

StaticString s ("abc");
assert(s != "123"); // overload operators for char*
some_func(s); // implicit conversion
some_func(StaticString("abc")); // temporary object initialized from literal

请注意,此类的主要优点是明确避免复制字符串数据,因此可以重用字符串文字存储。这些数据在可执行文件中有一个特殊的位置,并且通常可以很好地优化,因为它可以追溯到 C 的早期及以后。事实上,如果不是出于 C 兼容性要求,我觉得这个类接近于 C++ 中的字符串文字。

通过扩展,如果这对您来说非常常见,您也可以编写自己的 map 类,这可能比更改字符串类型更容易。

关于c++ - std::string 常量的正确习语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2312860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com