Resizable Arrays

Discussion in 'C++' started by dms, Aug 29, 2003.

  1. dms

    dms Guest

    Hi,

    Is there another, more friendly way to create resizable arrays without
    using the realloc function?

    Thanks

    David Sharp
    dms, Aug 29, 2003
    #1
    1. Advertising

  2. > Is there another, more friendly way to create resizable arrays without
    > using the realloc function?


    Take a look at the std::vector class in the standard library. This class
    is an excellent replacement for C-style arrays in most cases.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 29, 2003
    #2
    1. Advertising

  3. dms

    dms Guest

    Hi,

    Thanks for the quick reply. Can you please give me an example of usage?

    Thanks

    David Sharp

    Peter van Merkerk wrote:
    >>Is there another, more friendly way to create resizable arrays without
    >>using the realloc function?

    >
    >
    > Take a look at the std::vector class in the standard library. This class
    > is an excellent replacement for C-style arrays in most cases.
    >
    > --
    > Peter van Merkerk
    > peter.van.merkerk(at)dse.nl
    >
    >
    dms, Aug 29, 2003
    #3
  4. > Thanks for the quick reply. Can you please give me an example of
    usage?

    #include <iostream>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<int> v(3); // Create vector with 3 default constructed
    elements.

    v[0] = 100;
    v[1] = 200;
    v[2] = v[1] + v[0];

    v.push_back(123); // Add one element to the v, v now has 4 elements.
    v.push_back(321); // Add one element to the v, v now has 5 elements.

    v.resize(10); // Resize v to 10 elements
    v[9] = 12345; // Set value of last element of v

    // Dump contents of v
    for(int i=0; i < v.size(); ++i)
    {
    cout << "v[" << i << "] = " << v << endl;
    }
    return 0;
    }


    HTH

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 29, 2003
    #4
  5. dms

    dms Guest

    That's great! Thanks.

    Peter van Merkerk wrote:
    >>Thanks for the quick reply. Can you please give me an example of

    >
    > usage?
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    >
    > int main()
    > {
    > vector<int> v(3); // Create vector with 3 default constructed
    > elements.
    >
    > v[0] = 100;
    > v[1] = 200;
    > v[2] = v[1] + v[0];
    >
    > v.push_back(123); // Add one element to the v, v now has 4 elements.
    > v.push_back(321); // Add one element to the v, v now has 5 elements.
    >
    > v.resize(10); // Resize v to 10 elements
    > v[9] = 12345; // Set value of last element of v
    >
    > // Dump contents of v
    > for(int i=0; i < v.size(); ++i)
    > {
    > cout << "v[" << i << "] = " << v << endl;
    > }
    > return 0;
    > }
    >
    >
    > HTH
    >
    > --
    > Peter van Merkerk
    > peter.van.merkerk(at)dse.nl
    >
    >
    >
    dms, Aug 29, 2003
    #5
    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:
    731
    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:
    337
    =?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:
    373
    Graeme
    Sep 5, 2004
  4. Philipp
    Replies:
    21
    Views:
    1,102
    Philipp
    Jan 20, 2009
  5. fir

    fully fast resizable arrays in c (c2)

    fir, Mar 18, 2013, in forum: C Programming
    Replies:
    21
    Views:
    373
Loading...

Share This Page