vector/array advice please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csx

    vector/array advice please

    Hi all,
    I have the following code, which at the moment is a vector of arrays. But
    unfortunately, it doesnt do what I want.
    Basically, here is an example in Java that I want to work in C++.

    var Levels = new Array()

    Levels[0] = new makearray(1,1,' b',1);
    Levels[1] = new makearray(2,2,' g',1);
    Levels[2] = new makearray(3,5,' h',1);
    Levels[3] = new makearray(4,5,' w',1);
    Levels[4] = new makearray(5,3,' p',1);

    Can use levels.length() // to return the size of the outer array - i.e. 5



    This is really easy in Java, its like an array of arrays but with the
    levels.length() feature of a vector, and I want the same in C++. Also, you
    dont have to specify the size of the array beforehand.

    I want to be able to specify an array by its corresponding array index. So,
    if i wanted array (4, 5, 'w', 1) this would be accessed like
    nodes temp = Levels[3].
    But I also want to be easily able to call levels.length() so that I can get
    the size of the array. In this case, 5.

    I implemented mine as a vector of arrays, so that I could use
    "nodes.size ()", but now I cant refer to them via index. i.e. node[4].
    If I had implemented it as an array of arrays, the index would work,
    node[4]. but I wouldn't be able to call a method like nodes.size().



    Can someone please provide some advice
    Thanks in advance!!












    #include "stdafx.h"
    #include <vector>
    #include <iostream> // we have to use STD::cout with this
    #include <string>
    using namespace std; // Now don't need to specify the namespace, i.e.
    STD::COUT

    struct node
    {
    int key, row, parent;
    char description;
    node( int k=0, int r=0, char d='temp', int p=0 )
    : key(k), row(r), description(d), parent(p) {}
    };



    int _tmain(int argc, _TCHAR* argv[])
    {
    vector<node> nodes;
    nodes.push_back ( node(1,4,'G',4) ); // 0
    nodes.push_back ( node(2,2,'H',2) ); // 1
    nodes.push_back ( node(3,6,'J',2) ); // 2
    return 0;
    }








  • Julián Albo

    #2
    Re: vector/array advice please

    csx escribió:
    [color=blue]
    > I implemented mine as a vector of arrays, so that I could use
    > "nodes.size ()", but now I cant refer to them via index. i.e. node[4].
    > If I had implemented it as an array of arrays, the index would work,
    > node[4]. but I wouldn't be able to call a method like nodes.size().[/color]

    Use a vector of vectors.

    Regards.

    Comment

    • Ron Natalie

      #3
      Re: vector/array advice please


      "csx" <csx@ms.com> wrote in message news:bkq23n$54u $1@hercules.bti nternet.com...
      [color=blue]
      > Levels[0] = new makearray(1,1,' b',1)[/color]
      Pretend we're not javaheads here and explain what makearray does.
      [color=blue]
      > I implemented mine as a vector of arrays, so that I could use
      > "nodes.size ()", but now I cant refer to them via index. i.e. node[4].
      > If I had implemented it as an array of arrays, the index would work,
      > node[4]. but I wouldn't be able to call a method like nodes.size().[/color]

      Can't refer to what by index. Sure you can refer to nodes[4] (it will
      even work if there were that many items in the array). The only thing
      that won't work is accessing them by index if they do not exist
      (perhaps java arrays auto extend, but C++ vectors do not).

      vector<node> nodes;
      node[0] = node(2,3,'d',3) ; // illegal, nodes has zero size.

      nodes.push_back ( node(1,4,'G',4) ); // 0
      node[0] = node(5,6,'7',8) ; // legal the push_back increased nodes.size() to 1.

      nodes.resize(5) ;
      node[2] = node(7,8,'9',10 ); // legal, resize increased the size.


      Comment

      • csx

        #4
        Re: vector/array advice please

        hi thanks
        but, how would this be coded so i could index a vector via
        vector[4] as in the code posted earlier?

        "Julián Albo" <JULIANALBO@ter ra.es> wrote in message
        news:3F708C8B.3 0FECA44@terra.e s...
        csx escribió:
        [color=blue]
        > I implemented mine as a vector of arrays, so that I could use
        > "nodes.size ()", but now I cant refer to them via index. i.e. node[4].
        > If I had implemented it as an array of arrays, the index would work,
        > node[4]. but I wouldn't be able to call a method like nodes.size().[/color]

        Use a vector of vectors.

        Regards.


        Comment

        • Kevin Saff

          #5
          Re: vector/array advice please

          "csx" <csx@ms.com> wrote in message
          news:bkq504$a7a $1@hercules.bti nternet.com...[color=blue]
          > hi thanks
          > but, how would this be coded so i could index a vector via
          > vector[4] as in the code posted earlier?[/color]

          First resist top-posting.

          Second, you usually construct a vector using push_back.

          std::vector<std ::string> v;
          v.push_back( "a " );
          v.push_back( "example " );
          v.push_back( "simple " );

          and then you use the data using operator[].

          example:
          std::cout << v[0] << v[2] << v[1] << std::endl;
          // prints "a simple example "

          Alternatively, use std::map.

          std::map<int, std::string> v;
          v[0] = "a ";
          v[0] = "a ";



          Comment

          Working...