This question already has answers here:
Print a vector of vectors

(2个答案)


3个月前关闭。




我正在尝试动态分配和打印vector<vector<int>>,但是我不知道该怎么做,我被卡在这里是我的程序
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class A {
public:
    vector<int> getVector(int s) {

        vector <int> A(s);
        for (int j = 0; j < s; j++) {
            A.push_back(j);
        }
        return A;
    }
};
int main() {
    A obj;
    int n = 5;
    vector<vector<int>> A;

    A.push_back(obj.getVector(n));     // pushes a vector on vector A
    A.push_back(obj.getVector(n - 1));

    vector<vector<int>> ::iterator it;
    it = A.begin();
    for (it; it != A.end(); it++) {
        cout << *it;
    }


    return 0;
}

最佳答案

vector<vector<int>> ::iterator it;
    it = A.begin();
    for (it; it != A.end(); it++) {
        for(vector<int>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
            cout << *it2;
    }

07-28 00:34