C ++向量指针数组

时间:2010-07-14 01:29:39

标签: c++

struct_genestruct_units。 我将所有Struck_gene值读取到vector。 如何为struck_units.has_gene[i]指定指针以指向struct_gene向量中的某个值,这样我就不必编写stuckt_gene的完整副本 然后阅读它。

我试了一段时间,但没有设法找到工作解决方案

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

编辑 - 这是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;
}

4 个答案:

答案 0 :(得分:1)

t_struct_unit->has_genes[i] = &vector_gene

这会将指向基因列表的指针分配到单位的i位。

但要注意,因为vector_gene不是一个全局变量,所以它(以及它的所有内容)将在它返回的函数之后的某个时间被垃圾填充。因为它在main中,这对你来说可能不是问题,但你应该知道堆栈和堆分配之间的区别。

另外,您确定要让每个struct_unit保存struct_gene s的整个向量数组吗?难道你不希望每个人只拥有12个基因,而不是12个载体的基因吗?

答案 1 :(得分:0)

试试这个;

1)实例struct_unit

struct_unit aunit;

2)然后在循环内部将基因设置在所需的位置(SOME_INDEX由你定义)

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

答案 2 :(得分:0)

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;
}

答案 3 :(得分:0)

这是他的代码:

#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;
}