Поэтому у меня есть код ниже. Я панелирую изображение матрицы/или pgm. По панелированию я имею в виду повторение в заданном числе столбцов и строк. Он будет выглядеть как одно из этих окон, все панели разделяют каждую секцию стекла.
пример: http://1.bp.blogspot.com/_OLskT-GO5VE/TGqgrSX_o_I/AAAAAAAAA-4/vcCdn6hA3fI/s320/2007_12_warhol_cambell_soup.jpg это 4x8 матрица суп-банок (которые сами по себе являются гигантской матрицей пикселей,
Я считаю, что сегментная ошибка происходит где-то с индексом = k, но я не могу найти ничего плохого. Я переопределяю матрицу так, чтобы ее не было.
edit: фиксированный пример.
void panel(vector <VecofInts> &p, int inputRow, int inputColumn)
{
int i, j, v = 1, g = 1, k = 0, row, col;
row = p.size();//obtaining the original rows
col = p[0].size();//obtaining the original columns
p.resize((r * row)); //sets up the new matrix so I can add new elements.
/* This is my first test loop for the columns; I know I can create a single loop
for rows and columns but this will help me find problems more easily */
while(v < c){
...
}
/* this is the loop I'm having trouble with */
v=1;
while(v < c){
k = row;
while(g < r){
for(i = 0; i < row; i++){
k = k + i;
for(j = 0; j < col; j++){
p[k].push_back(p[i][j]);
}
}
g++;
k++; //this allows the g<r loop to continue and k not repeat itself
//in the first i loop again.
}
v++;
}
}
Из вашего примера не слишком ясно, каковы ваши намерения.
Кстати, я предлагаю этот общий подход:
typedef std::vector<int> VecofInts;
typedef std::vector<VecofInts> Matrix;
typedef std::vector<int>::iterator RowIterator;
void panel(const Matrix& m, int inputRow, int inputColumn) {
const int column = m.size(); /* Number of column, each colum is a vec of int */
RowIterator it;
for(int i=0; i != column; i++) {
it = m[i].begin();
/* m[i] represent a row on your matrix */
for(; it != m[i].end; it++) {
...
}
}
}
когда вы используете k = k + i
в цикле for, вы получите k = 0,1,3,6,10,15...
Я думаю, что ваша цель k++
?
РЕДАКТИРОВАТЬ:
Пожалуйста, см. Комментарий для k = k + i
:
while(v < c){
k = row;
while(g < r){
for(i = 0; i < row; i++){
k = k + i; // ####### k = (row), (row+1), (row+3), (row+6), (row+10),(row+15)....
for(j = 0; j < col; j++){
p[k].push_back(p[i][j]);
}
}
g++;
k++; //this allows the g<r loop to continue and k not repeat itself
//in the first i loop again.
}
p[k]
? и на самом деле вы получили серию p [row + 0] .push, p [row + 1] .push, p [row + 3] .push, p [row + 6] .push, p [row + 10] .push в вашем коде