C++ Nested data structure utilizing struct

So I'm trying to come up with a simple data structure that would have shop chains, their shop locations and the products & their prices.

The solution I wanted to try for it was following, but because I forgot that C++ does not support dynamic naming it did not work. Can I somehow alter this logic into something that C++ can handle?

Example vector containing the data I want to save to my data structure would be something like

{"chain_nameX", "location_nameX", "product_nameX", product_priceX}

where the first 3 entries would be strings and the last one would be either an int or a string (if the product was out of stock it would not have a price value, but a string).

I have to use this struct for my data structure, but because of not being able to use dynamic naming I'm a little bit lost as to what would be a natural solution.

struct Product {
    string productname;
    double price;
};

This is the struct I created to contain a shop location and the product data from above struct in a vector, but I wanted to utilize dynamic naming so I'm not sure if this is actually a good way to approach it

struct Shop {
    string shop_location;
    vector <Product> shop_locations;
};

So, I wanted to start storing the data by using logic like this:

    Shop word_segments[1];

but because dynamic naming is not actually a thing in C++, it will not work. Are there any smooth solutions to formulating data structures like this?

Also, I'm mandated to use the pre-determined struct Product , but isn't the Product as a struct kind of redundant? If I call it by the shop location, then the limit would be only one product and if I call it by the product name, then why does it contain a productname string within it?

Edit: If I just name the struct data structures as set names and save them to vectors/other data structures, will they remain unchanged when I rename their elements for other parts of the data structures or will their value change globally? (let me know if this sounds too confusing and I'll put up an example as to what I mean)

Edit2: Did according to suggestion

Product product;
    product.productname = word_segments[2];
    product.price = price;

    Shop shop;
    shop.products.push_back(product);
    shop.shop_location = word_segments[1];

    ShopChain shopchain;
    shopchain.name = word_segments[0];
    shopchain.shops.push_back(shop);

    chains[word_segments[0]].push_back(shopchain);

    all_possible_products.push_back(word_segments[2]);
    chain_names.push_back(word_segments[0]);

"chains" is defined as map<string, vector<ShopChain>> chains;


I fail to understand what you'd need dynamic naming for. You seem to be missing some basic knowledge about data structures, so let me show you one approach:

On the top level, we will have this:

std::vector<ShopChain> shop_chains;

where ShopChain is:

struct ShopChain {
     std::string name;              // shop chain has-a name
     std::vector<Shop> shops;       // and shops
     std::vector<Product> products; // and products (common for all shops)
};

where Product is defined in your question and Shop is:

struct Shop {
    std::string location; // shop has-a location
};

vector <Product> shop_locations; as a member was making very little sense to me.


You might need to move products under Shop , if you think their merchandise will not always be the same. Maybe you'd also not want to store duplicates of Product , so you'd need to store them in a separate data structure and only keep references to them.

链接地址: http://www.djcxy.com/p/87910.html

上一篇: 如何在Android中延迟后调用方法

下一篇: C ++使用struct的嵌套数据结构