# Thread: iterator on a matrix

1. ## iterator on a matrix

Hello,

I need a matrix int matrix[x][y]; as you seen I don't know x and y and I opted for a vector of vector; problem: times I need to perform some operation on one "Line" of the matrix" eg. compute the min_element of the line and other times I need compute min on one column; I hoped to use STL algorithms as "min element"; I hope this will be clear!

Can anyone help me, please?

EDIT:
my actual situation is this:
Code:
`vector<Pattern*> = _patternSet->begin();`
PatternSet class contains a vector of Pattern*; Pattern class contains a vector of Value; Value class has a double as member.

2. begin does not return a vector, it returns an iterator. If you dereference it, it may be a vector.

Code:
```int main()
{
typedef std::vector<int> IntLine;
typedef std::vector<IntLine> IntTable;

IntTable table;
//...

for (IntTable::iterator table_it = table.begin(); table_it != table.end(); ++table_it) {
for (IntLine::iterator line_it = table_it->begin(); line_it != table_it->end(); ++line_it) {
//...
}
}
}```

3. hi,

the question isn't it; I'm looking a structure as "matrix" that permits me to call stl_algorithm on coulumn and on lines to perform some calcolus........

4. your vectors have to be either rows or columns. they can't be both.

5. Interesting problem.

It is easy to do this for rows. For columns it is harder. Someone might have a better solution, but one possibility is to use custom predicates:

Code:
```#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>

struct compare_nth
{
const unsigned n;
compare_nth(unsigned num): n(num) {}

/*
note: it is programmers responsibility to ensure than first[n] and second[n]
are not out of range
*/
template <class Container>
bool operator() (const Container& first, const Container& second)
{
return first[n] < second[n];
}
};

int main()
{
typedef std::vector<int> IntLine;
typedef std::vector<IntLine> IntTable;
IntTable table;
for (int i = 0; i != 10; ++i) {
IntLine line;
for (int j = 0; j != 6; ++j) {
line.push_back(rand());
std::cout << std::setw(7) << line.back();
}
table.push_back(line);
std::cout << '\n';
}
//simple to find in rows:
std::cout << "\nmin in rows:";
for (IntTable::iterator line_it = table.begin(); line_it != table.end(); ++line_it) {
std::cout << ' ' << *std::min_element(line_it->begin(), line_it->end());
}

//harder to find in columns
//NB! assumes all rows have same length
std::cout << "\nmin in columns:";
for (unsigned i = 0; i != table[0].size(); ++i) {
std::cout << ' ' << (*std::min_element(table.begin(), table.end(), compare_nth(i)))[i];
}
}```
However, if boost::lambda is available to you, you won't need compare_nth and could search columns like this:

Code:
```
std::cout << ' ' << (*std::min_element(table.begin(), table.end(), _1[i] < _2[i]))[i];```
Basically, you'll need to keep your head clear as to what dereferences to what.

6. Why use a vector? You have essentially forced yourself into a data type that your debugger can't even display.

You can just as easily use a dynamically allocated array. For this situation I fail to see what functionality a vector brings to the table to warrant using it over a dynamic array.

I think this code is much easier:

Code:
```matrix[row * width + column] = some_value;
some_value = matrix[row * width  + column]```
Than any of the other snippets.

7. But how would that help you if you want std::algorithms to iterate over columns? (Not that vector<vector> is the best solution for things...)

8. There's also Boost.MultiArray, which essentially wraps Bubba's way.

9. Originally Posted by Bubba
Why use a vector? You have essentially forced yourself into a data type that your debugger can't even display.
I think this code is much easier:
Code:
```matrix[row * width + column] = some_value;
some_value = matrix[row * width  + column]```
Moreover, I don't know the "line" and "column" values at begin.....

Popular pages Recent additions