Is there a class for a dynamicly resizable array?

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

    Is there a class for a dynamicly resizable array?

    Basicly, I need to store a string plus a data structure for each entry in
    this array. It needs to be able to get bigger (but I wont be deleting from
    it). Also, I need to be able to store the elements in this array somehow.

  • Jon Bell

    #2
    Re: Is there a class for a dynamicly resizable array?

    In article <3fceb1b0$1@dne ws.tpgi.com.au> ,
    Jonathan Wilson <jonwil@tpgi.co m.au> wrote:[color=blue]
    >Basicly, I need to store a string plus a data structure for each entry in
    >this array. It needs to be able to get bigger (but I wont be deleting from
    >it). Also, I need to be able to store the elements in this array somehow.[/color]

    The 'vector' class in the C++ standard library should fill your needs.
    Here's a simple example that uses a vector of strings. You can
    generalize it by defining a class or struct to hold whatever you want, and
    making a vector of that.

    #include <vector>
    #include <string>
    #include <iostream>

    using namespace std;

    int main ()
    {
    vector<string> foo2; // start with a zero-length vector
    foo2.push_back( "Hello,"); // vector expands automatically
    foo2.push_back( "my");
    foo2.push_back( "name");
    foo2.push_back( "is");
    foo2.push_back( "Munich.");

    // find out how big the vector is by using the size() member function.

    for (int k = 0; k < foo2.size(); ++k)
    cout << foo2[k] << " ";
    cout << endl;

    return 0;
    }

    --
    Jon Bell <jtbellap8@pres by.edu> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA

    Comment

    • Victor Bazarov

      #3
      Re: Is there a class for a dynamicly resizable array?

      "Jonathan Wilson" <jonwil@tpgi.co m.au> wrote...[color=blue]
      > Basicly, I need to store a string plus a data structure for each entry in
      > this array. It needs to be able to get bigger (but I wont be deleting from
      > it). Also, I need to be able to store the elements in this array somehow.[/color]

      vector<pair<str ing,yourdatastr ucture> >

      I am not sure what you mean by "I need to be able to store the elements
      in this array somehow".

      Victor


      Comment

      • Jonathan Wilson

        #4
        Re: Is there a class for a dynamicly resizable array?

        > I am not sure what you mean by "I need to be able to store the elements[color=blue]
        > in this array somehow".[/color]
        I meant sort not store.
        Can one easliy sort the elements in a vector somehow?

        Comment

        • David Fisher

          #5
          Re: Is there a class for a dynamicly resizable array?

          "Jonathan Wilson" <jonwil@tpgi.co m.au> wrote:
          [color=blue][color=green]
          > > I am not sure what you mean by "I need to be able to store the elements
          > > in this array somehow".[/color]
          > I meant sort not store.
          > Can one easliy sort the elements in a vector somehow?[/color]

          std::vector vec;
          .... fill the vector with values ...
          std::sort(vec.b egin(), vec.end());

          David F


          Comment

          • Jon Bell

            #6
            Re: Is there a class for a dynamicly resizable array?

            In article <Ttzzb.3887$xm. 135747@nasal.pa cific.net.au>,
            David Fisher <nospam@nospam. nospam.nospam> wrote:[color=blue]
            >"Jonathan Wilson" <jonwil@tpgi.co m.au> wrote:[color=green]
            >>
            >> Can one easliy sort the elements in a vector somehow?[/color]
            >
            >std::vector vec;
            >... fill the vector with values ...
            >std::sort(vec. begin(), vec.end());[/color]

            If the vector is filled with a user-defined data type, you need to define
            operator<() for that data type, in order to do the comparisons, or else
            pass a comparison function as a third argument to std::sort().

            --
            Jon Bell <jtbellap8@pres by.edu> Presbyterian College
            Dept. of Physics and Computer Science Clinton, South Carolina USA

            Comment

            • Flaviu Matan

              #7
              Re: Is there a class for a dynamicly resizable array?

              Jonathan Wilson <jonwil@tpgi.co m.au> wrote in message news:<3fceb1b0$ 1@dnews.tpgi.co m.au>...[color=blue]
              > Basicly, I need to store a string plus a data structure for each entry in
              > this array. It needs to be able to get bigger (but I wont be deleting from
              > it). Also, I need to be able to store the elements in this array somehow.[/color]

              You could use the standard collections: a std::vector of std::pair
              Ex:
              typedef std::pair<strin g, my_structure> MyPair;
              std::vector<MyP air> vect;

              , or a std::map<string , my_structure>

              Comment

              • Dan W.

                #8
                Re: Is there a class for a dynamicly resizable array?

                On Thu, 04 Dec 2003 13:04:29 +0800, Jonathan Wilson
                <jonwil@tpgi.co m.au> wrote:
                [color=blue][color=green]
                >> I am not sure what you mean by "I need to be able to store the elements
                >> in this array somehow".[/color]
                >I meant sort not store.
                >Can one easliy sort the elements in a vector somehow?[/color]

                Depending on how you use your "array", you might want to use
                std::vector<my_ pair> or std::list<my_pa ir>:

                When you sort a vector, you are actually moving its elements around,
                which could be slow if your vector is large. Also, if you have
                pointers to elements of the vector, or iterators, and then sort it,
                your pointers and iterators become invalid.

                The list container, on the other hand, is not contiguous: Each element
                could be anywhere in memory, and each element is chained to previous
                and next via pointers. It is usually faster to sort, add and delete.

                With std::list nothing is moved, as sorting, adding and deleting only
                change pointers to previous and next. To use it you need to,

                #include <list>

                And to make sure that the STL is in your path.

                Cheers!

                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: Is there a class for a dynamicly resizable array?

                  Jonathan Wilson wrote:[color=blue]
                  >
                  > Basicly, I need to store a string plus a data structure for each entry in
                  > this array. It needs to be able to get bigger (but I wont be deleting from
                  > it). Also, I need to be able to store the elements in this array somehow.[/color]

                  After lots of discussion about std::vector:

                  Depending on your exact neees a simple std::map or a std::multimap
                  could be simpler.

                  --
                  Karl Heinz Buchegger
                  kbuchegg@gascad .at

                  Comment

                  • jb

                    #10
                    Re: Is there a class for a dynamicly resizable array?

                    Jonathan Wilson <jonwil@tpgi.co m.au> wrote in message news:<3fceb1b0$ 1@dnews.tpgi.co m.au>...[color=blue]
                    > Basicly, I need to store a string plus a data structure for each entry in
                    > this array. It needs to be able to get bigger (but I wont be deleting from
                    > it). Also, I need to be able to store the elements in this array somehow.[/color]

                    vector...perhap s???

                    Comment

                    Working...