#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<int>> test;
    vector<int> v;
    int n,temp;

    cin >> n;
    test.clear();

    //输入
    for (int i = 0; i<n; i++) {
        v.clear(); //每次记得clear:)
        for (int j = 0; j < n; j++)
        {
            cin >> temp;
            v.push_back(temp);
        }
        test.push_back(v);
    }

    //输出
    for(int i = 0; i < n; i++)
    {
        for(int j = 0;j < n; j++)
        {
            cout << test[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

二维向量的输入问题:
不像二维数组那样,可以直接对arr[i][j]进行循环赋值。在vector<vector<int>>中,因为vector是一个容器,最外层的vector容器中放着更小的vector,而里层的vector里面放的是int型的数字。所以我们首先要对里层的vector容器赋值,然后再把里层的vector作为元素插入到外层的vector中。代码如上,转载自http://blog.csdn.net/u013068755/article/details/70198924

posted on 2017-11-14 00:55  任我主宰  阅读(958)  评论(0编辑  收藏  举报