1

Imagine something like account database of social network in C++. Each account has it's username, level (admin, etc.), list of users who follow this account and list of users who messaged this account.

Problem is, I wanna count number of messages received by each separate user, so name and count in inner struct gotta be linked together.

Is this good idea of implementation?

    struct User {
        string name;
        int level;

        vector<string> followedBy;
        struct MessagedBy {
            string name;
            int count;
        };
    };

vector<User> users;
//@TODO vector of MessagedBy as an instance of User

How do I create vector of structs inside of vector of structs? How do I point at it?

6
  • Is there any reason you are using structs and not classes? Commented Mar 21, 2013 at 16:34
  • 1
    @wittybear structs are classes. Commented Mar 21, 2013 at 16:36
  • It doesn't really matter if I use structs or classes, both are okay with me. Commented Mar 21, 2013 at 16:39
  • As an aside, you might want to permit people to change their names. How about using an additional int id to identify a user and to refer to them in MessagedBy and followedBy? Commented Mar 21, 2013 at 16:41
  • Yes, that is a great tip, Christian. This software is actually supposed to translate user entry to GraphViz syntax, so I guess I won't need that this time. ;) Commented Mar 21, 2013 at 16:44

1 Answer 1

2

So, you would probably want something like this:

struct User {
        string name;
        int level;

    vector<string> followedBy;
    struct MessagedBy {
        string name;
        int count;
    };
    vector<MessageBy> messages;
};

You can then use:

cout << "Messages from: " << users[index].messages[otherindex].name << ":" << users[index].messages[otherindex].count;
Sign up to request clarification or add additional context in comments.

1 Comment

I did include that in my answer... vector<MessageBy> messages;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.