Vectors of Structures in C++

View: New views
7 Messages — Rating Filter:   Alert me  

Vectors of Structures in C++

by Bill Denney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm wanting to make a vector of structures like I would by doing the
following on the command prompt

a(1).b = 3;
a(2).b = 4;

But, I want to do this in C++.  The only thing that I've found was
http://wiki.octave.org/wiki.pl?CodaStruct, but it only deals with reading
and creating from a struct scalar.  Can someone tell me how to make a
struct vector?

I'm wanting to return this struct vector from one subfunction to my main
DLD function that will be called from the octave command line.  What I've
found so far is putting the struct into an octave_value_list when returned
gives a cell with a struct in it.

Thanks,

Bill

--
"Many languages have something called a macro. But Lisp macros are
unique. And believe it or not, what they do is related to the
parentheses."
   -- Paul Graham, http://paulgraham.com/avg.html



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by Jordi GutiƩrrez Hermoso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 4/9/06, Bill Denney <denney@...> wrote:
> I'm wanting to make a vector of structures

I'm not sure if I'm understanding your question, but will a simple
answer like using the Standard Template Library vector class satisfy
your needs?

- JGH



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by Bill Denney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 10 Apr 2006, Jordi Gutierrez Hermoso wrote:

> On 4/9/06, Bill Denney <denney@...> wrote:
>> I'm wanting to make a vector of structures
>
> I'm not sure if I'm understanding your question, but will a simple
> answer like using the Standard Template Library vector class satisfy
> your needs?

I don't think so, but let me try to explain what I'm wanting a bit
better, and maybe you can tell me if it will work (and perhaps give an
example of how to do it):

I am currently doing something like

DLD_FUNCTION(TranslateSBML, argsIn, , "") {
   octave_value_list retval;

   Octave_map a;
   a.assign("foo", 123);
   a.assign("bar", GetSubstruct());

   return retval;
}

octave_value_list GetSubstruct() {
   Octave_map tmp;
   octave_value_list substruct;

   for (int i=0; i < 10; i++) {
     tmp.assign("a", i);
     substruct(i) = tmp;
   }

   return substruct;
}

I want substruct to be a struct vector instead of a cell vector.  How do I
do that?

Bill

--
"She often gave herself very good advice (though she very seldom
followed it)."
   -- Lewis Carroll



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10-Apr-2006, Bill Denney wrote:

| On Mon, 10 Apr 2006, Jordi Gutierrez Hermoso wrote:
|
| > On 4/9/06, Bill Denney <denney@...> wrote:
| >> I'm wanting to make a vector of structures
| >
| > I'm not sure if I'm understanding your question, but will a simple
| > answer like using the Standard Template Library vector class satisfy
| > your needs?
|
| I don't think so, but let me try to explain what I'm wanting a bit
| better, and maybe you can tell me if it will work (and perhaps give an
| example of how to do it):
|
| I am currently doing something like
|
| DLD_FUNCTION(TranslateSBML, argsIn, , "") {
|    octave_value_list retval;
|
|    Octave_map a;
|    a.assign("foo", 123);
|    a.assign("bar", GetSubstruct());
|
|    return retval;
| }
|
| octave_value_list GetSubstruct() {
|    Octave_map tmp;
|    octave_value_list substruct;
|
|    for (int i=0; i < 10; i++) {
|      tmp.assign("a", i);
|      substruct(i) = tmp;
|    }
|
|    return substruct;
| }
|
| I want substruct to be a struct vector instead of a cell vector.  How do I
| do that?

It's not clear to me exactly what data structure you are trying to
create here.  Can you post some Octave code that creates a small
structure with the key elements that you would like to construct
using C++?

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by Bill Denney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 10 Apr 2006, John W. Eaton wrote:

> It's not clear to me exactly what data structure you are trying to
> create here.  Can you post some Octave code that creates a small
> structure with the key elements that you would like to construct
> using C++?

function retval = TranslateSBML()
   retval.b = "B";
   retval.c = makechildstruct();
endfunction

function retval = GetSubstruct()
   for i = 1:10
     retval(i).d = i;
   endfor
endfunction

The jist of what I'm trying to do is that I want to be able to end up with
an index like

a.c(1).d

instead of

a.c{1}.d

as I currently get.  I will also be creating sub-...-sub-structures, but I
assume that will be a straight-up extension of whatever you show me here.
I will also be needing (in a different function) to read out these
sub-...-sub-structures.  Is there a good source of documentation on how to
use the Octave C++ types and classes (the header files seem bare of
comments)?

I realize that my example C++ code is probably very wrong in a best coding
practices sense (I'm learning C++ at the same time that I'm trying to
write this).

Bill

--
"We'll give people IIS (It Isn't Secure) just so the script kiddies can
demonstrate the ability of Windows to do distributed computing in the form
of DDoS attacks."
   -- RAMMS+EIN



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10-Apr-2006, Bill Denney wrote:

| function retval = TranslateSBML()
|    retval.b = "B";
|    retval.c = makechildstruct();
| endfunction
|
| function retval = GetSubstruct()
|    for i = 1:10
|      retval(i).d = i;
|    endfor
| endfunction
|
| The jist of what I'm trying to do is that I want to be able to end up with
| an index like
|
| a.c(1).d
|
| instead of
|
| a.c{1}.d
|
| as I currently get.

Try this:

  #include <octave/oct.h>
  #include <octave/Cell.h>
  #include <octave/oct-map.h>

  octave_value
  GetSubstruct (void)
  {
     Octave_map m;

     Cell tmp (dim_vector (10, 1));

     for (int i = 0; i < 10; i++)
       tmp(i) = i+1;

     m.assign ("d", tmp);

     return m;
  }

  DEFUN_DLD (TranslateSBML, , ,
    "TranslateSBML")
  {
     Octave_map m;

     m.assign  ("b", "B");
     m.assign ("c", GetSubstruct ());

     return octave_value (m);
  }

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Re: Vectors of Structures in C++

by Bill Denney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That worked just like I wanted, thanks.

Bill

On Mon, 10 Apr 2006, John W. Eaton wrote:

> Try this:
>
>  #include <octave/oct.h>
>  #include <octave/Cell.h>
>  #include <octave/oct-map.h>
>
>  octave_value
>  GetSubstruct (void)
>  {
>     Octave_map m;
>
>     Cell tmp (dim_vector (10, 1));
>
>     for (int i = 0; i < 10; i++)
>       tmp(i) = i+1;
>
>     m.assign ("d", tmp);
>
>     return m;
>  }
>
>  DEFUN_DLD (TranslateSBML, , ,
>    "TranslateSBML")
>  {
>     Octave_map m;
>
>     m.assign  ("b", "B");
>     m.assign ("c", GetSubstruct ());
>
>     return octave_value (m);
>  }
>
> jwe
>

--
"Don't ever take a city for granted.
"After all, it is bigger than you are; it is older; and it has learned
how to wait..."
   -- Neil Gaiman



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------