Is there a class for a dynamicly resizable array?

Discussion in 'C++' started by Jonathan Wilson, Dec 4, 2003.

  1. 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.
    Jonathan Wilson, Dec 4, 2003
    #1
    1. Advertising

  2. Jonathan Wilson

    Jon Bell Guest

    In article <3fceb1b0$>,
    Jonathan Wilson <> wrote:
    >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.


    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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Dec 4, 2003
    #2
    1. Advertising

  3. "Jonathan Wilson" <> wrote...
    > 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.


    vector<pair<string,yourdatastructure> >

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

    Victor
    Victor Bazarov, Dec 4, 2003
    #3
  4. > I am not sure what you mean by "I need to be able to store the elements
    > in this array somehow".

    I meant sort not store.
    Can one easliy sort the elements in a vector somehow?
    Jonathan Wilson, Dec 4, 2003
    #4
  5. Jonathan Wilson

    David Fisher Guest

    "Jonathan Wilson" <> wrote:

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

    > I meant sort not store.
    > Can one easliy sort the elements in a vector somehow?


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

    David F
    David Fisher, Dec 4, 2003
    #5
  6. Jonathan Wilson

    Jon Bell Guest

    In article <Ttzzb.3887$>,
    David Fisher <> wrote:
    >"Jonathan Wilson" <> wrote:
    >>
    >> Can one easliy sort the elements in a vector somehow?

    >
    >std::vector vec;
    >... fill the vector with values ...
    >std::sort(vec.begin(), vec.end());


    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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Dec 4, 2003
    #6
  7. Jonathan Wilson

    Flaviu Matan Guest

    Jonathan Wilson <> wrote in message news:<3fceb1b0$>...
    > 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.


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

    , or a std::map<string, my_structure>
    Flaviu Matan, Dec 4, 2003
    #7
  8. Jonathan Wilson

    Dan W. Guest

    On Thu, 04 Dec 2003 13:04:29 +0800, Jonathan Wilson
    <> wrote:

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

    >I meant sort not store.
    >Can one easliy sort the elements in a vector somehow?


    Depending on how you use your "array", you might want to use
    std::vector<my_pair> or std::list<my_pair>:

    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!
    Dan W., Dec 4, 2003
    #8
  9. Jonathan Wilson wrote:
    >
    > 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.


    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
    Karl Heinz Buchegger, Dec 4, 2003
    #9
  10. Jonathan Wilson

    jb Guest

    Jonathan Wilson <> wrote in message news:<3fceb1b0$>...
    > 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.


    vector...perhaps???
    jb, Dec 4, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thiago Almeida

    Resizable columns on a web datagrid

    Thiago Almeida, Oct 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    728
    Thiago Almeida
    Oct 15, 2003
  2. =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?=

    RE: Are datagrids in Windows Application resizable?

    =?Utf-8?B?TWF0dCBIYW1pbHRvbg==?=, Sep 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    334
    =?Utf-8?B?Tm9lbCBMYXdhcw==?=
    Sep 2, 2004
  3. =?Utf-8?B?Tm9lbCBMYXdhcw==?=

    RE: Are datagrids in Windows Application resizable?

    =?Utf-8?B?Tm9lbCBMYXdhcw==?=, Sep 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    Graeme
    Sep 5, 2004
  4. Mark
    Replies:
    19
    Views:
    611
  5. Adam
    Replies:
    2
    Views:
    427
    dontlikenicknames
    Sep 2, 2008
Loading...

Share This Page