马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> printMatrix(vector<vector<int>>matrix)
{
vector<int>res;
res.clear();
int row = matrix.size();
int column = matrix[0].size();
int circle = ((row < column ? row : column - 1) / 2 + 1); //圈数
for (int i = 0; i < circle; i++)
{
//从左向右
for (int j = i; j < column - i; j++)
res.push_back(matrix[i][j]);
//从上向下
for (int k = i + 1; k < column - i; k++)
res.push_back(matrix[k][column - i - 1]);
//从右向左(需检查是否重复打印)
for (int m = column - (i + 1) - 1; (m >= i) && (row - i - 1 != i); m--)
res.push_back(matrix[row - i - 1][m]);
//从下向上(需检查是否重复打印)
for (int n = row - (i + 1) - 1; (n > i) && (column - i - 1 != i); n--)
res.push_back(matrix[n][i]);
cout << res.size() << endl;
}
return res;
}
int main()
{
vector<vector<int>>matrix;
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
vector<int> b;
b.push_back(5);
b.push_back(6);
b.push_back(7);
b.push_back(8);
matrix.push_back(a);
matrix.push_back(b);
vector<int>output = printMatrix(matrix);
cout << output.size() << endl;
//vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
//for (it = output.begin(); it != output.end(); it++)
//{
// cout << *it << " ";
//}
for (size_t i = 0; i <= output.size() - 1; i++)
{
if (i >= output.size() || i < 0) { cout << "vetcor下标越界" << endl; break; }
cout << output[i] << endl;
}
return 0;
}
出现vector subscript out of range的错误
大神们帮帮我,我真的太菜了
|