【问题标题】:Why I can use const char* as key in std::map<std::string, int>为什么我可以在 std::map<std::string, int> 中使用 const char* 作为键
【发布时间】:2012-12-05 14:52:25
【问题描述】:

我已经定义了一个数据结构

std::map<std::string, int> a;

我发现我可以将 const char* 作为键传递,如下所示:

a["abc"] = 1;

哪个函数提供从 const char* 到 std::string 的自动类型转换?

【问题讨论】:

  • 隐含地做这样的事情听起来像是很难找到错误的秘诀。
  • 不,一些隐式转换很好。 C字符串和std::string在语义上是等价的,只是C字符串是无用的废话。
  • @Warren,它在 C++ 中很常见,尤其是对于 std::string 等常用结构。每次进行查找时,您都可以享受临时 std::string 构造带来的性能冲击......
  • 又一个 C++ 滑坡参数。上次咬我的时候,我想我还在抽搐。

标签: c++ type-conversion


【解决方案1】:

std::string 有一个constructor that allows the implicit conversion from const char*。

basic_string( const CharT* s,
              const Allocator& alloc = Allocator() );

表示隐式转换如

std::string s = "Hello";

是允许的。

这相当于做类似的事情

struct Foo
{
  Foo() {}
  Foo(int) {} // implicit converting constructor.
};

Foo f1 = 42;
Foo f2;
f2 = 33 + 9;

如果您想禁止隐式转换构造,请将构造函数标记为explicit:

struct Foo 
{
  explicit Foo(int) {}
};

Foo f = 33+9; // error
Foo f(33+9); // OK
f = Foo(33+9); // OK

【讨论】:

    【解决方案2】:

    std::string 有一个构造函数,它以 const char* 作为参数。

    string::string(const char*);
    

    除非构造函数被显式声明,否则编译器将在需要调用任何函数时应用一种使用定义的转换。

    【讨论】:

      【解决方案3】:

      见string constructor。构造函数提供映射中键的转换。相当于

      a[std::string("abc")] = 1;
      

      【讨论】:

        【解决方案4】:

        在 C++ 中,如果你创建一个只接受一个参数的类构造函数,那么(除非你用explicit 另有说明),那么该参数的类型将是你的类的implicitly convertable。

        std::string 有这样一个 char * 的构造函数

        是的,这有时会导致一些意外行为。这就是为什么您通常应该将 explicit 放在单参数构造函数上的原因,除非您真的想要这些静默转换。

        【讨论】:

          猜你喜欢
          • 2010-12-23
          • 2011-06-23
          • 1970-01-01
          • 2011-05-08
          • 1970-01-01
          • 1970-01-01
          • 2018-08-23
          • 1970-01-01
          相关资源
          最近更新 更多