简体   繁体   English

C ++向量指针数组

[英]C++ vector pointer array

have struct_gene and struct_units . 具有struct_genestruct_units i read all Struck_gene values to vector . 我将所有Struck_gene值读取为vector How do i assign pointer for struck_units.has_gene[i] to point to some value in struct_gene vector so i dont have to write full copy of stuckt_gene And after that read it. 我如何为struck_units.has_gene[i]分配指针以指向struct_gene向量中的某个值,所以我不必写完全值的stickt_gene,然后读取它。

I tried for some time but didn't manage to get working solution 我试了一段时间,但没有找到有效的解决方案

http://a.imageshack.us/img195/8607/001tfa.jpg http://a.imageshack.us/img195/8607/001tfa.jpg

Edit - Here's the code as retrieved by Jakobud: 编辑-这是Jakobud检索的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}

t_struct_unit->has_genes[i] = &vector_gene

This will assign a pointer to the your gene list into the unit's i th slot. 这会将指向您的基因列表的指针分配到该单元的第i个插槽中。

But beware, since vector_gene is not a global variable, it (and all its contents) will be filled with garbage some time after the function it is in returns. 但是要注意,由于vector_gene不是全局变量,因此它(及其所有内容)将在返回函数后的一段时间内被垃圾填充。 Since it's in main , that's probably a non-issue for you, but you should know about the difference between stack and heap allocations. 由于它位于main ,对您来说可能不是问题,但是您应该了解堆栈和堆分配之间的区别。

Also, are you sure you want each struct_unit to hold a whole array of vectors of struct_gene s? 另外,您确定要每个struct_unit都容纳struct_gene向量的整个数组吗? Don't you want each one to hold just twelve genes, rather than 12 vectors of genes? 您不希望每个人都只拥有12个基因,而不是12个基因载体吗?

Try this; 尝试这个;

1) instance struct_unit 1)实例struct_unit

struct_unit aunit;

2) Then inside the loop set the gene in the desired position ( SOME_INDEX to be defined by you) 2)然后在循环内部将基因设置在所需位置( SOME_INDEX由您定义)

//inside loop
vector_gene.push_back(*t_struct_gene);
aunit.has_genes[SOME_INDEX] = *t_struct_gene;
//

http://codepad.org/TZpNS59a : http://codepad.org/TZpNS59a

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}

Here is his code: 这是他的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}

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

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