troubleshooting Question

Manipulating a FORTRAN data structure using STL iterators?

Avatar of jdpipe
jdpipeFlag for Australia asked on
Programming Languages-OtherC++Editors IDEs
16 Comments1 Solution979 ViewsLast Modified:
Hi there

I've got a bit of Fortran code that I'm using and I want to wrap in elegantly with C++.
The code in question is here: http://www.netlib.org/toms/529

The Fortran routine that I want to use requires a datastructure (IP, ICN, LENR) describing a boolean matrix. I want to create a C++ class for this boolean matrix which stores data in memory in a way that is fortran compatible, but I wanted to be able to make use of STL programming techniques to implement that class.

Let me give a simple example. A 3x3 bool matrix {1 1 0; 0 1 1; 0 0 0} would be represented in the fortran structure as
IP = 1 1 4 4
ICN = 1 2 2 3
LENR = 2 2 0

When I want to insert a new item into this matrix, lets say at row 2, column 1, I might want to be able to code something like

class MatBool{
    MatBool(const unsigned &nr, const unsigned &nc) : nr(nr), nc(nc), ip(size_t(nr+1),0), lenr(size_t(0),0) {}
    unsigned nr,nc;
    vector<unsigned> ip;
    vector<unsigned>icn;
    vector<usigned> lenr;
    ...
    void set(const unsigned &r, const unsigned &c){
        vector<unsigned>::iterator i,first,last;
        first = icn.begin() + ip[r];
        last = icn.end() + ip[r+1]
        for(i=first; i<last && *i<c ; ++i);
        icn.insert(i,c);
        for(unsigned j=r; j<nr; ++j)ip[j]++;
    }
}

I'm finding that I have problems with the icn.begin() + ip[r] stuff. Is there a way that I can obtain an iterator for a given location (index number) within my vector<unsigned> icn ?

Normally I would fix this problem by making the vector<unsigned> ip into a vector<vector<unsigned>::iterator> but I can't do that here because I want to maintain the datastructure that is compatible with the Fortran routine.

Any suggestions?

Cheers
JP
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 16 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros