简体   繁体   English

打印对象内对象的向量

[英]print vector of objects within an object

I'm trying to print an object Order (actually a vector of Order s). 我正在尝试打印对象Order (实际上是Order的向量)。 Order has some data members, including a vector with other objects, Purchase . Order具有一些数据成员,包括带有其他对象的向量Purchase

I can print the vector<Purchase> to cout on its own, and I can print vector<Objects> if I ignore the vector<Purchase> member. 我可以打印vector<Purchase>cout自身,我可以打印vector<Objects> ,如果我忽略vector<Purchase>构件。 But the tricky part is to print vector<Objects> with vector<Purchase> included. 但是棘手的部分是打印包含vector<Purchase> vector<Objects>

Here is my code: 这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

struct Purchase {
    string name;
    double unit_price;
    int count;
};

struct Order {
    string name;
    string adress;
    double data;
    vector<Purchase> vp;
};

template<typename Iter>   //this is my general print-vector function
ostream& print(Iter it1, Iter it2, ostream& os, string s) {
    while (it1 != it2) {
        os << *it1 << s;
        ++it1;
    }
    return os << "\n";
}

ostream& operator<<(ostream& os, Purchase p) {
    return os << "(" << p.name << ", " << p.unit_price << ", " << p.count << ")";
}

ostream& operator<<(ostream& os, Order o) {
    vector<Purchase> vpo = o.vp;
    ostringstream oss;
    oss << print(vpo.begin(), vpo.end(), oss, ", "); //This is what I would like to do, but the compiler doesn't like this conversion (from ostream& to ostringstream)

    os << o.name << "\n" << o.adress << "\n" << o.data << "\n"
        << oss << "\n";
    return os;
}

int main() {
    ifstream infile("infile.txt");
    vector<Order> vo;
    read_order(infile, vo);  //a function that reads a txt-file into my vector vo
    print(vo.begin(), vo.end(), cout, "");
    return 0;
}

As you can see, I had the idea to use ostringstreams as a temporary variable, that I would store the vector<Purchase> before I pass it on to the ostream& os . 如您所见,我想到了将ostringstreams用作临时变量的想法,即在将vector<Purchase>传递给ostream& os之前,先将其存储。 But this is a no go. 但这是不行的。 What would be a good solution to this problem? 什么是解决这个问题的好方法?

I am fairly new to C++ and are just learning the different uses of streams, so please bear with me if this is a stupid question. 我对C ++相当陌生,只是在学习流的不同用法,所以如果这是一个愚蠢的问题,请多多包涵。

Looks like you have two minor typos. 看来您有两个小错别字。

First, remove the indicated portion: 首先,删除指示的部分:

   oss << print(vpo.begin(), vpo.end(), oss, ", ")
// ↑↑↑↑↑↑↑

Then, later in that same function, you cannot stream a stringstream , but you can stream the string serving as its underlying buffer, so use std::stringstream::str() : 然后,后来在同样的功能,你不能流一stringstream ,但你可以流的字符串作为其潜在的缓冲区,所以使用std::stringstream::str()

os << o.name << "\n" << o.adress << "\n" << o.data << "\n"
    << oss.str() << "\n";
//        ↑↑↑↑↑↑

With those fixes in place, and the missing read_order function abstracted away, your program compiles . 修复了这些错误,并read_order了缺少的read_order函数之后, 您的程序就会编译

最简单的方法是编写operator<<的重载,该重载对std::vector<Purchase>进行const引用,然后将向量流式传输到ostream

std::ostream& operator<<(std::ostream& os, const std::vector<Purchase>& v);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM