如何在C++中打印对象向量的内容
How to Print the Contents of a Vector of Objects in C++
请我有一个叫做节点的人的向量。我从文件读取到节点向量中。当我尝试打印出矢量的内容时,我的代码可以编译但不打印任何内容。下面是我的代码。任何帮助将不胜感激。我还是新手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
的构造函数,并在person
的vector
中创建对象。
要打印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;
}
相关文章:
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 迭代时从向量和内存中删除对象
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 如何创建从同一类继承的不同对象的向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 从多个源构造一个对象,包括一个对象向量
- 在C 中,是否有可能在不兼容类型的std ::向量对象之间传输不同类型的缓冲区
- 将向量对象存储在共享指针投掷错误中
- E0312,C2664尝试将向量对象作为函数参数传递时错误
- 相对于向量对象的两个成员,找到两个向量的相交的有效方法
- 将数据从文件读取到向量对象
- C 向量对象访问
- 向量对象声明后的括号是什么意思
- 2向量对象指向相同的分配内存
- 如何返回向量对象
- 传递单个或向量对象时没有重载函数的实例
- 指向数组和向量对象的指针
- 向量对象库,可以存储其他对象类型的对象
- 如何从另一个向量/对象集构造新的向量/指针集