RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 836076
Accepted
Александр
Александр
Asked:2020-06-01 02:08:50 +0000 UTC2020-06-01 02:08:50 +0000 UTC 2020-06-01 02:08:50 +0000 UTC

如何将对象添加到地图?

  • 772

我是否正确地向地图添加了几个值?

cout << "Size: " << ((map <BYTE,ifunct*>)FuncBytes[sign]).size() << endl;
((map <BYTE,ifunct*>)FuncBytes[sign]).insert(pair<byte,ifunct*>(n,changMethod));
cout << "Size: " << ((map <BYTE,ifunct*>)FuncBytes[sign]).size() << endl;

和以前一样,插入后 size() 返回零。也就是值\u200b\u200bare没有加?

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

2 个回答

  • Voted
  1. Best Answer
    AnT stands with Russia
    2020-06-01T02:59:03Z2020-06-01T02:59:03Z

    您的显式类型转换(map <BYTE,ifunct*>)创建了一个临时对象 type map <BYTE,ifunct*>。每个这样的转换都会创建一个单独的临时对象,该对象会在表达式结束时自动销毁。你做的所有动作,都是用这些临时对象做的。对这些临时对象之一也添加了一个元素,当然,它也随之消失得无影无踪。

    该对象FuncBytes[sign]不受您的操作的影响。

    • 1
  2. vt-egorov
    2020-06-01T17:36:53Z2020-06-01T17:36:53Z

    一个更好地理解显式类型转换会发生什么的示例:

    #include <iostream>
    #include <vector>
    class Bar;
    class Foo{
    public:
        Foo():
            m_id{makeId()},
            m_data{}
        {
            std::cout << "Foo::Foo() self id: " << m_id << std::endl;
        }
        Foo(int data):
            m_id{makeId()},
            m_data{data}
        {
            std::cout << "Foo::Foo(int) self id:" << m_id << std::endl;
        }
        int getData() const{
            return m_data;
        }
        void setData(int data){
            m_data = data;
        }
        Foo(const Foo& other):
        m_id{makeId()},
        m_data{other.m_data}{
            std::cout << "Foo::Foo(const Foo& other) self id: " << m_id << "  other id: " << other.m_id << std::endl;
        }
        Foo& operator=(const Foo& other){
            std::cout << "Foo::operator=(const Foo& other) self id: " << m_id << "  other id: " << other.m_id << std::endl;
            if (this != &other){
                m_data = other.m_data;
            }
            return *this;
        }
        virtual ~Foo(){
            std::cout << "Foo::~Foo() self id: " << m_id << std::endl;
        }
    private:
        static int makeId(){
            return m_idCounter++;
        }
        int m_id;
        static int m_idCounter;
        int m_data;
    };
    
    
    int Foo::m_idCounter = 0;
    
    class Bar{
    public:
        Bar():
            m_id{makeId()},
            m_data{}
        {
            std::cout << "Bar::Bar() self id:" << m_id << std::endl;
        }
        Bar(int data):
            m_id{makeId()},
            m_data{data}
        {
            std::cout << "Bar::Bar(int) self id:" << m_id << std::endl;
        }
        Bar(const Bar& other):
            m_id{makeId()},
            m_data{other.m_data}
        {
            std::cout << "Bar::Bar(const Bar& other) self id:" << m_id << " other id:" << other.m_id << std::endl;
        }
        Bar& operator=(const Bar& other){
            if (this != &other){
                m_data = other.m_data;
             }
            std::cout << "Bar::oeprator=(const Bar& other) self id:" << m_id << " other id:" << other.m_id << std::endl;
            return *this;
        }
        int getData() const{
            return m_data;
        }
        void setData(int data){
            m_data = data;
        }
        operator Foo() const{
            return Foo(m_data);
        }
        virtual ~Bar(){
            std::cout << "Bar::~Bar() self id:" << m_id << std::endl;
        }
    private:
        static int makeId(){
            return m_idCounter++;
        }
        int m_id;
        static int m_idCounter;
        int m_data;
    };
    
    int Bar::m_idCounter = 0;
    
    using namespace std;
    
    int main()
    {
        Foo f = 1;
        Bar b = 2;
        f = (Foo)b;
        std::cout << "before create vector" << std::endl;
        std::vector<Bar> bars;
        bars.reserve(5);
        std::cout << "before emplace back" << std::endl;
        for (int i = 0; i < 5; i++){
            bars.emplace_back((i+1)*10);
        }
        std::cout << "after emplace back" << std::endl;
        for (auto& item: bars){
            ((Bar)item).setData(100);
            std::cout << ((Bar)item).getData() << std::endl;
        }
        for (const auto& item: bars){
            ((Foo)item).setData(100);
            std::cout << ((Foo)item).getData() << std::endl;
        }
        for (auto& item: bars){
            ((Bar&)item).setData(100);
            std::cout << ((Bar&)item).getData() << std::endl;
        }
    
    
        return 0;
    }
    

    结论:

    Foo::Foo(int) self id:0
    Bar::Bar(int) self id:0
    Foo::Foo(int) self id:1
    Foo::operator=(const Foo& other) self id: 0  other id: 1
    Foo::~Foo() self id: 1
    before create vector
    before emplace back
    Bar::Bar(int) self id:1
    Bar::Bar(int) self id:2
    Bar::Bar(int) self id:3
    Bar::Bar(int) self id:4
    Bar::Bar(int) self id:5
    after emplace back
    Bar::Bar(const Bar& other) self id:6 other id:1
    Bar::~Bar() self id:6
    Bar::Bar(const Bar& other) self id:7 other id:1
    10
    Bar::~Bar() self id:7
    Bar::Bar(const Bar& other) self id:8 other id:2
    Bar::~Bar() self id:8
    Bar::Bar(const Bar& other) self id:9 other id:2
    20
    Bar::~Bar() self id:9
    Bar::Bar(const Bar& other) self id:10 other id:3
    Bar::~Bar() self id:10
    Bar::Bar(const Bar& other) self id:11 other id:3
    30
    Bar::~Bar() self id:11
    Bar::Bar(const Bar& other) self id:12 other id:4
    Bar::~Bar() self id:12
    Bar::Bar(const Bar& other) self id:13 other id:4
    40
    Bar::~Bar() self id:13
    Bar::Bar(const Bar& other) self id:14 other id:5
    Bar::~Bar() self id:14
    Bar::Bar(const Bar& other) self id:15 other id:5
    50
    Bar::~Bar() self id:15
    Foo::Foo(int) self id:2
    Foo::~Foo() self id: 2
    Foo::Foo(int) self id:3
    10
    Foo::~Foo() self id: 3
    Foo::Foo(int) self id:4
    Foo::~Foo() self id: 4
    Foo::Foo(int) self id:5
    20
    Foo::~Foo() self id: 5
    Foo::Foo(int) self id:6
    Foo::~Foo() self id: 6
    Foo::Foo(int) self id:7
    30
    Foo::~Foo() self id: 7
    Foo::Foo(int) self id:8
    Foo::~Foo() self id: 8
    Foo::Foo(int) self id:9
    40
    Foo::~Foo() self id: 9
    Foo::Foo(int) self id:10
    Foo::~Foo() self id: 10
    Foo::Foo(int) self id:11
    50
    Foo::~Foo() self id: 11
    100
    100
    100
    100
    100
    Bar::~Bar() self id:1
    Bar::~Bar() self id:2
    Bar::~Bar() self id:3
    Bar::~Bar() self id:4
    Bar::~Bar() self id:5
    Bar::~Bar() self id:0
    Foo::~Foo() self id: 0
    
    • 1

相关问题

Sidebar

Stats

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

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +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