简体   繁体   English

结构向量内的结构向量

[英]Vector of structs inside of vector of structs

Imagine something like account database of social network in C++. 想象一下像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? 我该如何指出呢?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM