在C++语言中,我们可以使用vector实现二维数组。具体方法为:使用vector
include
include
include
using namespace std;
int main() {
int N = 2, M = 3;
vector > Matrix(N, vector (M, 0));
ostream_iterator os(cout, " ");
Matrix[0][2] = 4;
//交换矩阵的两行
Matrix[0].swap(Matrix[1]);
//交换矩阵第二行的两个元素
swap(Matrix[1][0], Matrix[1][2]);
for (uint i = 0; i < Matrix.size(); i++) {
copy(Matrix[i].begin(), Matrix[i].end(), os);
cout << endl;
}
return EXIT_SUCCESS;
}
暂无评论