• C++之map如何实例化类对象(一百九十九)


    简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

    优质专栏:Audio工程师进阶系列原创干货持续更新中……】🚀

    人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

    更多原创,欢迎关注:Android系统攻城狮

    欢迎关注Android系统攻城狮

    1.前言

    本篇目的:理解std::map如何实例化ComponentLoader对象。

    2.应用实例

    v1.0 实例化map类

    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    struct ComponentModule {
    public:
      void init(std::string libPath){
        printf("%s(), line = %d, libpath = %s\n",__FUNCTION__,__LINE__,libPath.c_str());
      }
    };
    
    struct ComponentLoader {
    public:
      ComponentLoader(std::string libPath) : mLibPath(libPath) {
        printf("%s(), line = %d, libpath = %s\n",__FUNCTION__,__LINE__,mLibPath.c_str());
      }
    
      void fetchModule() {
        std::shared_ptr<ComponentModule> localModule = std::make_shared<ComponentModule>();
        localModule->init(mLibPath);
      }
    
      std::string mLibPath;
    };
    
    int main(){
      //map容器,第一个参数string,第二个是ComponentLoader类.
      std::map<string, ComponentLoader> mComponents;
    
      //v1.0
      //auto emplace = [&](const char *libPath) {
    
      //v2.0 function定义
      function<void (const char*)> emplace = [&](const char *libPath) {
        //构造函数ComponentLoader实例化,emplace插入键值对
        mComponents.emplace(libPath, libPath);
      };
    
      emplace("libcodec2_soft_aacdec.so");
      emplace("libcodec2_soft_aacenc.so");
      emplace("libcodec2_soft_amrnbenc.so");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    注释:
    mComponents.emplace(libPath, libPath)插入key-value键值对,其实际形式可以写为:map
    第一个字段就是string类型
    第二个参数传入ComponentLoader类构造函数,它的构造函数实现

    ComponentLoader(std::string libPath) : mLibPath(libPath) {
        printf("%s(), line = %d, libpath = %s\n",__FUNCTION__,__LINE__,mLibPath.c_str());
      }
    
    • 1
    • 2
    • 3

    ComponentLoader构造函数初始化列表,将传入的libPath赋值给mLibPath成员函数。在可以调用fetchModule函数直接使用。

    v2.0 实例化map类 + 遍历

    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    struct ComponentModule {
    public:
      void init(std::string libPath){
        printf("%s(), line = %d, libpath = %s\n",__FUNCTION__,__LINE__,libPath.c_str());
      }
    };
    
    struct ComponentLoader {
    public:
      ComponentLoader(std::string libPath) : mLibPath(libPath) {
        printf("%s(), line = %d, libpath = %s\n",__FUNCTION__,__LINE__,mLibPath.c_str());
      }
    
      void fetchModule() {
        std::shared_ptr<ComponentModule> localModule = std::make_shared<ComponentModule>();
        localModule->init(mLibPath);
      }
    
      std::string mLibPath;
    };
    
    int main(){
      //map容器,第一个参数string,第二个是ComponentLoader类.
      std::map<string, ComponentLoader> mComponents;
    
      //v1.0
      //auto emplace = [&](const char *libPath) {
    
      //v2.0 function定义
      function<void (const char*)> emplace = [&](const char *libPath) {
        //构造函数ComponentLoader实例化,emplace插入键值对
        mComponents.emplace(libPath, libPath);
      };
    
      emplace("libcodec2_soft_aacdec.so");
      emplace("libcodec2_soft_aacenc.so");
      emplace("libcodec2_soft_amrnbenc.so");
    
    #if 0
      printf("\n");
      //v1.0使用auto遍历map
      for (auto &pathAndLoader : mComponents) {
        const string &path = pathAndLoader.first;
        ComponentLoader &loader = pathAndLoader.second;
        loader.fetchModule();
      }
      printf("\n");
    
      //v2.0使用pair遍历map
      for (std::pair<const string, ComponentLoader> &pathAndLoader : mComponents) {
        const string &path = pathAndLoader.first;
        ComponentLoader &loader = pathAndLoader.second;
    
        loader.fetchModule();
      }
    
      printf("\n");
      //v3.0使用迭代器遍历map
      std::map<std::string, ComponentLoader>::iterator it;
     for (it = mComponents.begin(); it != mComponents.end(); it++) {
       //std::string key = it->first;
       //ComponentLoader &value = it->second;
    
       printf("key = %s\n",it->first.c_str());
       it->second.fetchModule();
     }
    #endif
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    【Linux】 linux | docker | 安装nacos
    02.01 nginx简介、选择理由、安装和使用
    斯坦福计算机视觉cs231n-assignment3总结
    springcloud 项目扫描不到mybatics的xml文件;
    KaiwuDB 助力能源企业实现 4 大价值提升
    TensorFlow学习:使用官方模型进行图像分类、使用自己的数据对模型进行微调
    Linux下MMDetection环境配置
    Java中如何在两个线程间共享数据
    【Paraview教程】第一章安装与基础介绍
    一步步教你在 Windows 上构建 dotnet 系应用的 UOS 软件安装包
  • 原文地址:https://blog.csdn.net/u010164190/article/details/132838603