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

C++ 如何在C+中打印对象向量的内容+;,c++,vector,C++,Vector,请给我一个叫做节点的向量。我从一个文件读入节点向量。当我尝试打印出向量的内容时,我的代码会编译,但不会打印任何内容。下面是我的代码。任何帮助都将不胜感激。我对C还是新手++ class person { public: //int person_id; string name; int age; float spread_probability; person(){ } person (string pname, int pag

请给我一个叫做节点的向量。我从一个文件读入节点向量。当我尝试打印出向量的内容时,我的代码会编译,但不会打印任何内容。下面是我的代码。任何帮助都将不胜感激。我对C还是新手++

class person {

public:
    //int person_id;
    string name;
    int age;
    float spread_probability;


    person(){

     }

    person (string pname, int page, double spread){
        //person_id = p_id;
        name = pname;
        age = page;
        spread_probability = spread;
    }
};
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){
    cout << p.name << " " << p.age << " " << p.spread_probability << endl;

    for(int i=0; i<nodes.size(); i++){
        cout<< nodes[i].name <<":"; 
    }
}

// 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]);
}
班级人员{
公众:
//国际个人识别码;
字符串名;
智力年龄;
浮动扩散概率;
人(){
}
人物(字符串pname,整型页,双排列){
//person_id=p_id;
name=pname;
年龄=页数;
扩散概率=扩散;
}
};
矢量边;
向量节点;
无效插入节点(人员和p、字符串名称、整数、浮动价差概率)
{
p、 名称=名称;
p、 年龄=年龄;
p、 扩散概率=扩散概率;
节点。推回(p);
}
作废打印节点(人员和p){
cout
void print\u节点(个人和个人){
对于(int i=0;i
void print_)节点(person&p){
对于(int i=0;i此函数:

void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
  cout<< i <<":"; 
}
int main() 
{
    ifstream inf("smallpopulation.dat");
    vector<person> nodes;

    // 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;
        nodes.emplace_back(n, a, s);
    }

    // Goes through all instances of `nodes`
    for (const person& p : nodes) {
        cout << p << '\n'; // calls the `std::ostream& operator<<` defined above...
    }
    return 0;
}
然后打印它们的向量:

for(int i=0; i<nodes.size(); i++){
  print_node(nodes[i]);
}
用于(int i=0;i此函数:

void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
  cout<< i <<":"; 
}
int main() 
{
    ifstream inf("smallpopulation.dat");
    vector<person> nodes;

    // 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;
        nodes.emplace_back(n, a, s);
    }

    // Goes through all instances of `nodes`
    for (const person& p : nodes) {
        cout << p << '\n'; // calls the `std::ostream& operator<<` defined above...
    }
    return 0;
}
然后打印它们的向量:

for(int i=0; i<nodes.size(); i++){
  print_node(nodes[i]);
}

对于(int i=0;i您可能会重载运算符您可能会重载运算符问题是您没有打印来自
person
类的任何信息。
但是除了打印之外,您的代码还有更多的问题。首先,您的
insert
函数效率低下且难以阅读。实际上,您甚至不需要
insert
函数。您可以使用
std::vector::emplace\u back()
函数,例如:

nodes.emplace_back("name", 38, 1.4f);
这将调用
person
的构造函数,并在
person
s的
vector
中创建对象

要打印
向量
,您可以创建一个函数来打印一个
人物
对象实例:

void printPerson(const person& p) {
    std::cout << "Name: " << p.name << 
        "\nAge: " << p.age <<
        "\nSpread probability: " << p.spread_probability;
}
然后,您只需要为每个要打印的对象调用此函数


总而言之,这将是整个主要功能:

void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
  cout<< i <<":"; 
}
int main() 
{
    ifstream inf("smallpopulation.dat");
    vector<person> nodes;

    // 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;
        nodes.emplace_back(n, a, s);
    }

    // Goes through all instances of `nodes`
    for (const person& p : nodes) {
        cout << p << '\n'; // calls the `std::ostream& operator<<` defined above...
    }
    return 0;
}
intmain()
{
ifstream inf(“smallpopulation.dat”);
向量节点;
//如果我们无法打开输出文件流进行读取
如果(!inf){
//打印错误并退出

cerr问题是您没有打印来自
人员
类的任何信息。
但是除了打印之外,您的代码还有更多的问题。首先,您的
insert
函数效率低下且难以阅读。实际上,您甚至不需要
insert
函数。您可以使用
std::vector::emplace\u back()
函数,例如:

nodes.emplace_back("name", 38, 1.4f);
这将调用
person
的构造函数,并在
person
s的
vector
中创建对象

要打印
向量
,您可以创建一个函数来打印一个
人物
对象实例:

void printPerson(const person& p) {
    std::cout << "Name: " << p.name << 
        "\nAge: " << p.age <<
        "\nSpread probability: " << p.spread_probability;
}
然后,您只需要为每个要打印的对象调用此函数


总而言之,这将是整个主要功能:

void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
  cout<< i <<":"; 
}
int main() 
{
    ifstream inf("smallpopulation.dat");
    vector<person> nodes;

    // 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;
        nodes.emplace_back(n, a, s);
    }

    // Goes through all instances of `nodes`
    for (const person& p : nodes) {
        cout << p << '\n'; // calls the `std::ostream& operator<<` defined above...
    }
    return 0;
}
intmain()
{
ifstream inf(“smallpopulation.dat”);
向量节点;
//如果我们无法打开输出文件流进行读取
如果(!inf){
//打印错误并退出


cerr噢,谢谢。让我试试。但是我怎么能一次打印出所有内容呢?我也试过了。它仍然没有打印出任何内容。一种方法是用
列出属性。调试代码的一种有用方法是在
cout
行上放置一个断点。这样你可以看到向量中的内容,并验证问题是否存在从以前执行的代码中删除。我也会尝试,谢谢。让我试试。但是我怎么能一次打印出所有内容呢?我也尝试过。它仍然没有打印出任何内容。一种方法是用
列出属性调试代码的一种有用方法是在
cout
行上放置一个断点。这样你就可以看到我所做的C++中的S,并验证这个问题是否来自先前执行的代码。我也会尝试这么多。让我试试,这样做对代码