- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一张代表数据库对象的 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/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我正在尝试使用std:map类型的键和值制作std::any Visual Studio 2017 std::map m("lastname", "Ivanov"); std::cout (m["la
我已经在 C++ 的 map 中声明了一个集合为 std::map> .如何循环访问或打印设定值? 最佳答案 如果你知道如何迭代 std::map或 std::set单独地,您应该可以毫无问题地组合迭
如何循环? 我已经试过了: //----- code std::vector >::iterator it; for ( it = users.begin(); it != users.end();
我有两个用例。 A.我想同步访问两个线程的队列。 B.我想同步两个线程对队列的访问并使用条件变量,因为其中一个线程将等待另一个线程将内容存储到队列中。 对于用例 A,我看到了使用 std::lock_
我正在查看这两种类型特征的文档,但不确定有什么区别。我不是语言律师,但据我所知,它们都适用于“memcpy-able”类型。 它们可以互换使用吗? 最佳答案 不,这些术语不能互换使用。这两个术语都表示
我有以下测试代码,其中有一个参数 fS,它是 ofstream 的容器: #include #include #include #include int
这是这个问题的延续 c++ function ptr in unorderer_map, compile time error 我试图使用 std::function 而不是函数指针,并且只有当函数是
std::unordered_map str_bool_map = { {"a", true}, {"b", false}, {"c", true} }; 我们可以在此映射上使
我有以下对象 std::vector> vectorList; 然后我添加到这个使用 std::vector vec_tmp; vec_tmp.push_back(strDRG); vec_tmp.p
为什么 std::initializer_list不支持std::get<> , std::tuple_size和 std::tuple_element ?在constexpr中用得很多现在的表达式,
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
假设我有一个私有(private)std::map在我的类(class)里std::map 。我怎样才能将其转换为std::map返回给用户?我想要下面的原型(prototype) const std
假设我有一个私有(private)std::map在我的类(class)里std::map 。我怎样才能将其转换为std::map返回给用户?我想要下面的原型(prototype) const std
问题 我正在尝试将 lambda 闭包传递给 std::thread,它使用任意封闭参数调用任意封闭函数。 template std::thread timed_thread(Function&& f
我想创建一个模板类,可以容纳容器和容器的任意组合。例如,std::vector或 std::map ,例如。 我尝试了很多组合,但我必须承认模板的复杂性让我不知所措。我编译的关闭是这样的: templ
我有一个 std::vector>我将其分配给相同类型的第二个 vector 。 我收到这个编译器错误: /opt/gcc-8.2.0/include/c++/8.2.0/bits/stl_algob
有时候,我们有一个工厂可以生成一个 std::unique_ptr vector ,后来我们想在类/线程/你命名的之间共享这些指针。因此,最好改用 std::shared_ptr 。当然有一种方法可以
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 6 年前。 我创建了一个 vector vector ,我想根据我定义的参
我有三个类(class)成员: public: std::vector > getObjects(); std::vector > getObjects() const; privat
我是一名优秀的程序员,十分优秀!