iterator on a matrix

This is a discussion on iterator on a matrix within the C++ Programming forums, part of the General Programming Boards category; Hello, I need a matrix int matrix[x][y]; as you seen I don't know x and y and I opted for ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    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.
    Last edited by mickey0; 05-29-2008 at 12:06 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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) {
                //...
            }
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    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. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    your vectors have to be either rows or columns. they can't be both.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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...)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's also Boost.MultiArray, which essentially wraps Bubba's way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    Quote Originally Posted by Bubba View Post
    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 subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21