简体   繁体   English

如何在C ++中构建和使用vector(map(pair(struct)))?

[英]How can I build and use a vector(map(pair(struct))) in C++?

I want to build a variable like vector(map(pair(struct))) and use it to store information in C++, I try to use following code: 我想建立一个像vector(map(pair(struct())))这样的变量,并用它在C ++中存储信息,我尝试使用以下代码:

struct st_Base
{
    char Type[2];
    double Price;
    queue<double> Samples;
};

vector< map< string, pair< st_Base, st_Base >* >* > gv_combo;

string str_source = "test123";

gv_combo.push_back(new map<str_source, new pair<st_Base, st_Base>>);

But when I run the program, it always show me lots of errors. 但是,当我运行该程序时,它总是向我显示很多错误。 Can anyone told me the right way to build it, place data in it, and read it? 谁能告诉我正确的方法来构建它,将数据放入其中并读取它?

Consider not using dynamic allocation via new keyword (Manual memory management is prone to errors). 考虑不通过new关键字使用动态分配(手动内存管理容易出错)。 If your memory needs to be allocated dynamically use unique pointer std::unique_ptr . 如果需要动态分配内存,请使用唯一指针std::unique_ptr

What you are esentially creating is a container holding a pointer to container that is holding a pair of values (string (key), and pointer to pair of structs (value)). 本质上,您将创建一个容器,该容器持有一个指向持有一对值(字符串(键),另一个指向一对结构(值))的容器的指针。

#include <vector>
#include <map>
#include <utility>
#include <memory>
#include <iostream>



struct st_Base { int foo; };

int main()
{
    typedef std::pair< st_Base, st_Base> weird_pair;
    std::vector<std::map<std::string, weird_pair>> gv_combo;

    string str_source = "test123";
    weird_pair pair = make_pair(st_Base{ 10 }, st_Base{ 11 });
    gv_combo.push_back(std::map<std::string, weird_pair>()); 

    gv_combo.at(0).insert(std::pair<std::string, weird_pair>(str_source, pair));

    std::cout << gv_combo.at(0).at("test123").second.foo;

    return 1;

}

But this example is extremly unreadable (at least for me). 但是这个例子极不可读(至少对我而言)。 Access to members of structs is not straightforwarwd (needs to call at() to localize element in map, then use first/second to access apropriate st_Base which results in ever increasing chain of calls. Adding unique_ptr would result in even longer chain that would put my brain on a verge of scrapping the entire code after working with it for any period of time. 对结构成员的访问不是直接的(需要调用at()来本地化地图中的元素,然后使用first / second访问适当的st_Base,这会导致调用链不断增加。添加unique_ptr会导致更长的链接在经过一段时间后,我的大脑濒临报废整个代码。

Notes to OP: OP的注意事项:
-read documentation carefully, it's your friend -仔细阅读文档,是您的朋友
-allocate with keyword new only when you really have to (eg. obscure framework pre c++11) -仅在确实需要时才使用关键字new分配(例如,c ++ 11之前的晦涩框架)
-typedefs save lifes -typedefs挽救生命
-pointers can get out of hand quickly if you don't wrap them into nice structure 如果您不将指针包装成漂亮的结构,指针会很快失去控制
-objects can use initializer lists {} to give them data during construction of the object. -objects可以使用初始化列表{}在构造对象的过程中为它们提供数据。 it's worth noting that C and C++ versions {} are not exchangable ( st_Base{.foo=10} is legal in C, but illegal in c++ ) 值得注意的是C和C ++版本{}不可st_Base{.foo=10}st_Base{.foo=10}在C中是合法的,但在c ++中是非法的)

This seems to be what you are trying to achieve: 这似乎是您要实现的目标:

struct st_Base {
    char Type[2];
    double Price;
    std::queue<double> Samples;
};

std::vector<std::map<std::string, std::pair<st_Base, st_Base>>> gv_combo;

string str_source = "test123";
std::map<std::string, std::pair<st_Base, st_Base>> my_map;
my_map[str_source] = std::make_pair(st_Base(...), st_Base(...)); // instert pair of objects here

gv_combo.push_back(my_map);

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

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