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

How to Print the Contents of a Vector of Objects in C++

本文关键字:向量 对象 打印 C++      更新时间:2023-10-16

请我有一个叫做节点的人的向量。我从文件读取到节点向量中。当我尝试打印出矢量的内容时,我的代码可以编译但不打印任何内容。下面是我的代码。任何帮助将不胜感激。我还是新手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]);
}

此函数:

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

除了打印i什么都不做,i只是一个int

在我们打印一个person向量之前,我们必须能够打印一个person。你还没有向我们展示person的定义,但该类似乎包含(至少(三个成员变量。所以我们可以有一个这样的函数:

void print_node(person& p){
cout << p.name << " " << p.age << " " << p.spread_probability << endl;
}

然后打印它们的矢量:

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

或:

for(vector<person>::iterator itr=nodes.begin(); itr!=nodes.end(); ++itr){
print_node(*itr);
}

一旦你有了这么多的工作,许多改进是可能的。

你可以重载运算符<<如下所示:

#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
class Node {
public:
Node():x(0), s("string"){}
Node(int _x, std::string _s):x(_x),s(_s){}
friend std::ostream & operator<<(std::ostream & o, Node n);
private:
int x;
std::string s;
};
std::ostream & operator<<(std::ostream & o, Node n)
{
o<<"("<<n.x<<","<<n.s<<") ";
return o;
}
int main(int argc, char** argv)
{
Node n1 = Node();
Node n2 = Node(12,"apple");
Node n3 = Node(100, "rat");
std::cout<<n1<<" "<<n2<<std::endl;
//this print gives the following output:
//(0,string)  (12,apple)
//here the vector of Node objects is created:
std::vector<Node> vec;
vec.push_back(n1);
vec.push_back(n2);
vec.push_back(n3);
//size of the vector of Node objects:
std::cout<<"size="<<vec.size()<<std::endl;
//here the vector of Node objects is printed:
for(int i=0; i<vec.size(); ++i) std::cout<<vec.at(i)<<"  ";
std::cout<<std::endl;   
return 0;
} 
void print_node(person& p){
for(int i=0; i<nodes.size(); i++){
cout<< i <<":"; 
}
}

问题就在那里。你实际上没有告诉代码打印任何东西。U 可以尝试如下内容:

cout << i << nodes[i].age;

问题是您没有打印来自person类的任何信息。
但是除了打印之外,您的代码中还有更多问题。首先,您的insert函数效率低下且难以读取。实际上,在您的情况下,您甚至根本不需要insert函数。您可以使用std::vector::emplace_back()函数,例如:

nodes.emplace_back("name", 38, 1.4f);

这将调用person的构造函数,并在personvector中创建对象。

要打印vector<person>,您可以创建一个函数来打印一个person对象实例:

void printPerson(const person& p) {
std::cout << "Name: " << p.name << 
"nAge: " << p.age <<
"nSpread probability: " << p.spread_probability;
}

这已经建议并且肯定会起作用,但是有一个更好的选择,那就是 重载operator<<std::ostream,这就是std::cout派生自:

std::ostream& operator<<(std::ostream& out, const person& p)
{
out << "Name: " << p.name << 
"nAge: " << p.age <<
"nSpread probability: " << p.spread_probability;
return out;
}

然后,您只需为要打印的每个对象调用此函数即可。


总结一下,这将是整个主要功能:
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;
}