RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1292422
Accepted
artEA
artEA
Asked:2022-06-06 22:57:10 +0000 UTC2022-06-06 22:57:10 +0000 UTC 2022-06-06 22:57:10 +0000 UTC

问题是返回 operator[] 值

  • 772

问题是返回值,问题是 string& operator[] (const string index)和 bool& operator[] (const string index),它不允许我从 string 中获取值(返回 1)但是当我交换字符串时( bool& 到 string& 以及它们的内容)然后一切都反过来并返回空虚,如何解决?我的代码:

#include <iostream>
#include <string>
#include <map>

using namespace std;

class StringData
{
private:
    map<string, string> dataStrings;
    map<string, bool> dataBools;
public:
    string& operator[] (const string index)
    {
        return dataStrings[index];
    }
    bool& operator[] (const string index)
    {
        return dataBools[index];
    }
};

int main() 
{
    StringData a;
    a["test"] = false;
    a["test2"] = "Hello";
    cout << a["test"];
    cout << a["test2"];
    system("Pause");
}

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    KoVadim
    2022-06-07T05:05:11Z2022-06-07T05:05:11Z

    @Harry 的选项非常奇怪,我会严重拒绝为此审查代码。让我们接受@HolyBlackCat 的报价并安排

    #include <iostream>
    #include <string>
    #include <map>
    #include <variant>
    
    using namespace std;
    
    class StringData
    {
    public:
        // будем храть все в варианте, но это с++17
        using two_t = std::variant<string,bool>;
    private:
        map<string, two_t> dataStrings;
    public:
        two_t& operator[] (const string index)
        {
            return dataStrings[index];
        }
    };
    // нам нужен оператор вывода этой структуры
    std::ostream& operator<<(std::ostream& os, const StringData::two_t& t) {
        if(auto pval = std::get_if<std::string>(&t)) {
          os << *pval;
        }
        else if (auto pval = std::get_if<bool>(&t)) {
          os << *pval;
        }
        else {
            os << "<none>"; // ну мало чего
        }
         
        return os;
    }
    
    int main() 
    {
        StringData a;
        a["test"] = false;
        a["test2"] = "Hello";
        cout << a["test"];
        cout << a["test2"];
    }
    

    诚然,它仅适用于 C++17 及更早版本,但您始终可以绕过这个问题。

    • 5
  2. Harry
    2022-06-07T00:42:49Z2022-06-07T00:42:49Z

    这个想法本身是无效的 - 一个重载给出了两个具有相同签名的函数:

    string& operator[] (const string index);
    bool&   operator[] (const string index);
    

    不允许这样的 C++。

    推荐解决方案:

    class StringData
    {
    private:
        map<string, string> dataStrings;
        map<string, bool> dataBools;
    public:
        string& operator[] (const string index)
        {
            return dataStrings[index];
        }
        bool& operator[] (const char * index)
        {
            return dataBools[index];
        }
    };
    
    int main() 
    {
        StringData a;
        a["test"] = false;
        a["test2"s] = "Hello";
        cout << a["test"] << endl;
        cout << a["test2"s] << endl;
    }
    
    • 0

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5