Resizable Arrays

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

    Resizable Arrays

    Hi,

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

    Thanks

    David Sharp

  • Peter van Merkerk

    #2
    Re: Resizable Arrays

    > Is there another, more friendly way to create resizable arrays without[color=blue]
    > using the realloc function?[/color]

    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.merke rk(at)dse.nl


    Comment

    • dms

      #3
      Re: Resizable Arrays

      Hi,

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

      Thanks

      David Sharp

      Peter van Merkerk wrote:[color=blue][color=green]
      >>Is there another, more friendly way to create resizable arrays without
      >>using the realloc function?[/color]
      >
      >
      > 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.merke rk(at)dse.nl
      >
      >[/color]

      Comment

      • Peter van Merkerk

        #4
        Re: Resizable Arrays

        > 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[i] << endl;
        }
        return 0;
        }


        HTH

        --
        Peter van Merkerk
        peter.van.merke rk(at)dse.nl



        Comment

        • dms

          #5
          Re: Resizable Arrays

          That's great! Thanks.

          Peter van Merkerk wrote:[color=blue][color=green]
          >>Thanks for the quick reply. Can you please give me an example of[/color]
          >
          > 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[i] << endl;
          > }
          > return 0;
          > }
          >
          >
          > HTH
          >
          > --
          > Peter van Merkerk
          > peter.van.merke rk(at)dse.nl
          >
          >
          >[/color]

          Comment

          Working...