Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array High Bound Proplem 1

Status
Not open for further replies.

ca4928

Programmer
Jan 4, 2005
13
GB
I am new to C++ and am trying to translate a program from VB6 to C++. In this program i have an Array that slowly grows in size as the program runs. I need to find out how to "Redim Preserve myArray(Ubound(myArray)+1)" in C++.

For those who don't use VB, this function extends the length of myArray so that it has one more Value in it, but preserves the values that are already there
 
Standard C(++) arrays are fixed size, which gives fantastic speed and simplicity, but lacks flexibility and security. To use a standard array, you simply define the size of the array to be the maximum size that could possibly be required.

If that's not practical, then you have to use a different approach. The C++ Standard Template Library provides several container types, many of which can be used like arrays. Each has its own speed/flexibility trade-offs, but a good general-purpose container is "vector":
[tt]
#include <vector>
using namespace std;

vector<int>v; // create a vector of integers called v
v.push_back(123); // add 123 to the end of the vector
v.resize(10); // resize the vector to 10 elements
a=v[5]; // access the 6th element of v
v.clear(); // resize the vector to 0 elements

for(vector<int>::iterator i=v.begin();i!=v.end();i++)
{printf("%d\n",*i);} // list all the elements of the vector
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top