0

I'm looking for a way to initialize only first values in std::map and then initialize the second ones according to the keys. here is my code:

#pragma once
#include <string>
#include <map>

class Student
{
public:

    Student(
        double Score_Maths,
        double Score_Eng,
        double Score_Chem,
        double Score_Bio,
        double Score_His
        );
    ~Student();


private:

    std::string Surname;
    std::map<std::string, double> Subject_Scores = { {"Maths"}, {"English"}, {"Chemistry"}, {"Biology"}, {"History"} };

};

What I'm trying to do is, to have those keys in class already and then initialize the values using constructor, but of course it shows error when initializing map like that, any help?

17
  • 4
    You should be able to give defaults for each value: std::map<std::string, double> Subject_Scores = { {"Maths", 0.0}, {"English", 0.0}, {"Chemistry", 0.0}, {"Biology", 0.0}, {"History", 0.0} }; Commented Apr 5, 2018 at 13:23
  • 5
    @UlrichEckhardt while this code could technically be slightly MORE minimal, I would say this is a pretty good MCVE already. Commented Apr 5, 2018 at 13:24
  • 1
    so it's impossible without using some pre-assigned values? Commented Apr 5, 2018 at 13:24
  • 1
    @Nick yes. You can not assign a key without a value or vice-versa Commented Apr 5, 2018 at 13:26
  • 5
    Technically if you make the value an optional<double> it can actually not have a value, rather than just having a magic "invalid" value. But this seems like a lot of complexity without much payoff. Commented Apr 5, 2018 at 13:28

2 Answers 2

5

initialize the values using constructor

You can do both in the constructor directly:

https://godbolt.org/g/hWhcxu

class Student
{
public:

    Student(
        double Score_Maths,
        double Score_Eng,
        double Score_Chem,
        double Score_Bio,
        double Score_His
        )
        :
        Subject_Scores({ {"Maths", Score_Maths},
                         {"English", Score_Eng},
                         {"Chemistry", Score_Chem},
                         {"Biology", Score_Bio},
                         {"History", Score_His} })
    {
    }
    ~Student();

private:
    std::map<std::string, double> Subject_Scores;
}

This still ensures that your map is valid and initialized for the entire class lifetime.

Sign up to request clarification or add additional context in comments.

3 Comments

That's probably what I will do, because c++ doesn't seem to have what I was searching for.
I am thinking how about passing a map itself to the constructor for scalability. What if the set of subjects change? Will we go on and keep on changing the class. Design?
@SauravSahu Design-wise I think this is appropriate. You can even add more constructors with different curricula without issues without having to change e.g. a simple printout function. It's not great in terms of scalability, but neither is passing a fixed set of scores to the constructor.
0

You can write a (static) function to do that:

 std::map<std::string, double> Student::createMap( const std::vector<std::string> &v )
 {
     std::map<std::string, double> r;
     for( const auto &key : v ) r[ key ];
     return r;
 }

then in your class:

     std::map<std::string, double> Subject_Scores = createMap( { "Maths", "English", "Chemistry", "Biology", "History" } );

9 Comments

+1 for the syntax look-alikeness to what I wanted to do, but still, It assigns 0.0 to the values, which I don't want to happen. But it seems my votes aren't counted.
@Nick why dont you want that to happen? The thing is: If you want a container that contains only keys and no values then it isnt a map.
@user463035818 I already wrote about that in my other comments
To hear what? When you use map and do not want any value - do not insert that key, it is that simple. Looks like you want something you do not understand.
smells like a xy problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.