Click here to Skip to main content
15,886,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I am checking the usage of std::map and I would like to share you if there is a better approach to cover the version to link to different map part of my code segment I post bellow.

Could somebody explain me the best approach for such a map usage ?

Thank you very much in advance.

C++
<pre>#include <iostream>
#include <cstdlib>
#include <memory>
#include <map>

using namespace std;

class InputScale
{
public:
    float offset;
    float factor;
};

int main()
{
    bool inputSelected = true;
    map<int, unique_ptr<InputScale>> inputScales;
    map<int, unique_ptr<InputScale>> tachoScales;   
    
    // version linked to one single map.
    auto& inputScale = inputScales[1];
    inputScale = make_unique<InputScale>();
    
    inputScale->offset = 1.f;
    std::cout << inputScale->offset << std::endl;
    
    // version to link to different map
    map<int, unique_ptr<InputScale>>::mapped_type* scale{ nullptr };
    if (inputSelected)
        scale = &inputScales[2];
    else
        scale = &tachoScales[2];
    
    *scale = make_unique<InputScale>();
    (*scale)->offset = 2.f;
    
    std::cout << (*scale)->offset << std::endl;
}



What I have tried:

Currently I am using a pointer but I would like to get rid of it.
Posted
Updated 17-Aug-19 6:59am
Comments
Shao Voon Wong 19-Aug-19 6:01am    
You can remove unique_ptr

map<int, inputscale> inputScales;

1 solution

 
Share this answer
 
Comments
SuperMiQi 18-Aug-19 6:04am    
Thank you very much Mohibur,
I checked the link you posted me but I could not find the answer to my question.
If you have more inputs about it, this would really be appreaciated.
Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900