質問

First post here, and I'm working on some code to try to read from multiples CSV files, combine them into a "Master" CSV file. (CODING in: C++ Visual Studio 2013 by the way)

Anyways, that's not the trouble I'm having. I'm having difficulty with setting up my initial container I would like to store these values into. Here's my set-up so far:

#include <vector>
#include <map>
using namespace std;
    struct GradeCategories{
        string category;
        int grade;
    }; GradeCategories grade_cat;

    struct NameGrades{
        string name;
        vector<GradeCategories> v;
    }; NameGrades NG;

    typedef map<int, vector<NameGrades>> TheBook;

So how would I go about even accessing/inputting data into this set up?

I attempted:

TheBook[12345017][NG.name = "Bob Jones"].push_back((grade_cat.category = "Exam 1")(grade_cat.grade = 95));

Other than that, I don't really see how I would access the internal components of this. Extremely new to maps, and this deep of an "inception" of code is kind of throwing me.

So any help with that would be very appreciated! Thanks :)

役に立ちましたか?

解決

Well, TheBook, maps int onto a vector of NameGrades, which means:

TheBook[int] = vector<NameGrades>;

Start with one GradeCatagories object: grade_cat

grade_cat.category = "Exam";
grade_cat.grade = 95;

Now place it in a vector:

vector<GradeCategories> vgc;
vgc.push_back(grade_cat);

Now we can move on to NG:

NG.name = "Bob Jones";
NG.v = vgc;

Now we can insert it into a vector:

vector<nameGrades> vng;
vng.push_back(NG);

Finally, we can insert it into the map:

TheBook tb;
tb[12345017] = vng;

他のヒント

After fixing your code (#include <string>) and the TheBook typedef (put a space between >>), you can access it like this:

TheBook aBook;
aBook[1]     // Access map<int, THIS> > where int==1
[2]          // Access vector<NameGrades>[THIS] where THIS==2
.v[3]        // Access NameGrades.v[3]
.grade = 5;

If you want to add something to a book, do it like this:

GradeCategories a,b,c,d;
NameGrades v1,v2;

v1.v.push_back(a); v1.v.push_back(b);
v2.v.push_back(c); v2.v.push_back(d);

vector<NameGrades> outerVector;
outerVector.push_back(v1); outerVector.push_back(v2);

TheBook aBook;
aBook[1] = outerVector;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top