0

I want to create a file where i store all the students from a class(each as unique) and to also be able so save the subject that they are taking and all the grades that they have on that specific subject.

I tried doing this by implementing some sort of a 3d map with a string for unique names and a struct that holds the name of each subject and a vector inside it with all the grades the the student has.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
using namespace std;

struct subject
{           
    string subject;
    vector<int> grade;
};


int main()
{
        vector<subject> test;
    map<string, vector<subject> > ourClass; 
}
ourClass["John Mayer"] = test[0].note.push_back(10);

Tried it like this and it doesn't work. Do i have to overload the operator first or is there another way to access the specific elements?

How do i properly call the map for a specific name to add as many grades as i want in a specific subject? Or is there any more efficient way to do this?

Vlad
  • 23
  • 5
  • Have you looked into using `std::multimap` instead of a map of vector ? Also, the operator `=` will usually perform a copy. Be careful when copying containers or objects. – Clonk Nov 08 '19 at 09:29
  • 1
    This would work: [Sample on coliru](http://coliru.stacked-crooked.com/a/8e37c3d1eb847143) – Scheff's Cat Nov 08 '19 at 09:30
  • I'm very uncertain about the RHS of `ourClass["John Mayer"] = test[0].note.push_back(10);`. (But, I'm certain it won't work.) However, the `std::map::operator=()` expects a RHS type of the `map` value (in your case `vector`). [`std::vector::push_back()`](https://de.cppreference.com/w/cpp/container/vector/push_back) returns `void`. Hence, your assignment cannot compile. (I didn't quite understand what you actually intended to do.) – Scheff's Cat Nov 08 '19 at 09:34
  • [Avoid ```using namespace std```](https://stackoverflow.com/q/5849457/8769985) in headers. – francesco Nov 08 '19 at 10:02

0 Answers0