简体   繁体   English

C ++差异向量 <struct> 和一个向量 <string> ?

[英]C++ Diff vector<struct> and a vector<string>?

I track my child processes and their 'title' in my parent process using: 我使用以下命令在父进程中跟踪子进程及其“标题”:

struct c_process {
    string device;
    pid_t pid;
};

vector<c_process> children;

I terminate and fork these children based on if 'folders' exist when I SIGHUP the parent and reload its 'config'. 我在挂载父级并重新加载其“配置”时基于是否存在“文件夹”来终止并分叉这些子级。

Parent running
Folders /a1 /a3
Reads config and spawn processes (list of folders)
vector<c_process> = {['a1', 111], ['a3', 222]}
Something changes folders
Folders /a1 /a4 /a5
SIGHUP parent
Reads config and spawn processes
vector<c_process> = {['a1', 111], ['a4', 1121], ['a5', 452]}

My problem occurs when I read the list of folders and need to decide 'what needs to be added/fork' and 'what needs to be 'deleted/terminated'. 当我阅读文件夹列表并需要确定“需要添加/分叉的内容”和“需要删除/终止的内容”时,会出现我的问题。

vector<string> config;
vector<c_process> c_process;

So with the above structures how can I do this? 那么使用上述结构该如何做呢?

Be aware that if std::vector grows and has not enoough space reserved it may allocate and construct new elements via copy constructor from the old ones and afterwards destroy the old ones. 请注意,如果std :: vector增长并且没有足够的保留空间,它可能会通过复制构造函数从旧元素分配和构造新元素,然后销毁旧元素。 If pid_t is some high-level object this may hurt. 如果pid_t是一些高级对象,则可能会受伤。 An alternative is to keep only pointers (may be counting smart pointers) within the vector and therefore avoid the destruction of the refered objects. 一种替代方法是仅在矢量内保留指针(可能正在对智能指针进行计数),从而避免破坏所引用的对象。

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

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