简体   繁体   English

如何在C ++中打印出2D对象矢量?

[英]How to print out a 2D vector of objects in C++?

I'm writing a program which includes a 2D vector of objects: 我正在编写一个包含2D矢量对象的程序:

class Obj{
public:
    int x;
    string y;
};

vector<Obj> a(100);

vector< vector<Obj> > b(10);

I've stored some values of vector a in vector b. 我在向量b中存储了一些向量a的值。

I get an error when I try to print it out like this: 当我尝试将其打印出来时出现错误:

for(int i=0; i<b.size(); i++){
    for(int j=0; j<b[i].size(); j++)
      cout << b[i][j];
    cout << endl;
}

error info: 错误信息:

D:\\main.cpp:91: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and '__gnu_cxx::__alloc_traits >::value_type {aka Obj}') cout << b[i][j]; D:\\ main.cpp:91:错误:'operator <<'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'__gnu_cxx :: __ alloc_traits> :: value_type {aka Obj} ')cout << b [i] [j]; ^ ^

Your problem is not related to vectors, it is related to sending object(s) of some user defined type Obj to the standard output. 您的问题与向量无关,它与将某些用户定义类型Obj发送到标准输出有关。 When you send an object to the output stream using the operator<< as you do with: 使用运算符<<将对象发送到输出流时,如下所示:

cout << b[i][j];

the stream does not know what to do with it as none of the 12 overloads accepts a user defined type Obj . 流不知道如何处理它,因为12个重载中没有一个接受用户定义的类型Obj You need to overload the operator<< for your class Obj : 您需要为您的类Obj重载operator<<

std::ostream& operator<<(std::ostream& os, const Obj& obj) {
    os << obj.x  << ' ' << obj.y;
    return os;
}

or even a vector of Obj s: 甚至是Obj的矢量:

std::ostream& operator<<(std::ostream& os, const std::vector<Obj>& vec) {
    for (auto& el : vec) {
        os << el.x << ' ' << el.y << "  ";
    }
    return os;
}

For more info on the subject check out this SO post: 有关该主题的更多信息,请查看此SO帖子:
What are the basic rules and idioms for operator overloading? 运算符重载的基本规则和习惯用法是什么?

This has nothing to do with your vectors. 这与你的载体无关。

You are trying to print an Obj , but you didn't tell your computer how you would like it to do that. 您正在尝试打印Obj ,但您没有告诉您的计算机您希望它如何执行此操作。

Either print b[i][j].x and b[i][j].y individually, or overload operator<< for Obj . 单独打印b[i][j].xb[i][j].y ,或者为Obj重载operator<<

There is no cout::operator<< that takes a class Obj as a right hand side. 没有cout::operator<<class Obj作为右手边。 You could define one. 你可以定义一个。 Simplest solution is to send x and y to cout separately. 最简单的解决方案是分别将xy发送到cout But use range-based for-loops! 但是使用基于范围的for循环!

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Obj {
public:
    int x;
    string y;
};

vector<vector<Obj>> b(10);

void store_some_values_in_b() {
    for (auto& row : b) {
        row.resize(10);
        for (auto& val : row) {
            val.x = 42; val.y = " (Ans.) ";
        }
    }
}

int main() { 

    store_some_values_in_b();

    for (auto row: b) {
        for (auto val : row) {
            cout << val.x << " " << val.y;
        }
        cout << endl;
    }
}

Maybe like below 也许就像下面

for(int i=0; i<b.size(); i++){
    for(int j=0; j<b[i].size(); j++)
        cout << "(" << b[i][j].x << ", " << b[i][j].y << ") ";
    cout << endl;
}

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

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