如何在C ++中打印对象向量的内容

请给我一个称为节点的人的向量。我从一个文件读到节点向量。当我尝试打印矢量的内容时,我的代码可以编译,但不打印任何内容。下面是我的代码。任何帮助将不胜感激。我还是C ++新手

vector <string*> edges;
vector <person> nodes;

void insert_node(person& p, string name, int age, float spread_probability) {
p.name = name;
p.age = age;
p.spread_probability = spread_probability;
nodes.push_back(p);

}


void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
    cout<< i <<":"; 
}

}

// This is the main function 


int main() {

ifstream inf("smallpopulation.dat");

// If we couldn't open the output file stream for reading
if (!inf) {
    // Print an error and exit
    cerr << "Uh oh, population.dat could not be opened for reading!" << endl;
    return 1;
}

// While there's still stuff left to read
while (inf) {

    string n;
    int a;
    double s;

    inf >> n >> a >> s;
    insert_node(nodes[0], n, a, s);
}

print_node(nodes[0]);
}
评论
HORSE
HORSE
void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
    cout<< i <<":"; 
}
}

问题就在那里。您实际上不告诉代码打印任何内容。您可以尝试以下方法:

cout << i << nodes[i].age;
点赞
评论