Array and Vector

Hi all, from my understanding I understand that I can use array in this manner.

struct test
{
   int a;
   int b;
   int c;
};
 
test testing[10]; //creating an array with the structer type
 
testing[1].a=1;
testing[2].b=2;
testing[3].c=3;

If I'm not wrong we can use array in this manner, however is it possible to do the same using "vector"?

Yes, you can use both arrays and vectors in this manner. A vector acts just like an array with extras.

Be careful never to access elements that don't exist, remembering that arrays in C and C++ count from zero:

int n[4];

n[3]=5; // Last valid element
n[4]=5; // THIS MAY CRASH OR DO WEIRD THINGS

vector<int> v;
v.resize(4);
v[3]=5; // Last valid element
v[4]=5; // THIS MAY CRASH OR DO WEIRD THINGS

Thanks corona! For the codes below, if I want to use vector instead of array. How should I do it to achieve the same result?

struct test
{
   int a;
   int b;
   int c;
};
 
test testing[10]; //creating an array with the structer type
 
testing[1].a=1;
testing[2].b=2;
testing[3].c=3;

to make a vector of int, do vector<int>
to make a vector of test, do vector<test>
to make a vector of slartibartfast, do vector<slartibartfast>

To create vector change the type from array to array of pointers to struct test or an "Iliffe Vector"...

struct test *testing[10];

Which helps how exactly? it can't be used as an ordinary array anymore, and each element must be allocated individually...

That is left as an exercise for the op...i provided an example of a vector not its implementation.

That's a very bizzare way to make a vector, unless you wanted an array of whatever * instead of an array of whatever...

I am not sure as to what part of an array of pointers to struct type dont you get...its vectorized as each can be located anywhere in memory and not restricted to being sequential and have memory allocated throgh malloc calls.

I get it. I just don't understand what makes you think it's anything like a vector.

C++ vectors are stored contiguously in memory, and allocated together, not individually. You may be thinking of sparse memory, like a C++ map<>?

Except this is a poor way to do sparse memory, too. You'd do that with a list of some sort, so you don't even store NULL pointers for empty elements.

What you've got could be used to build a hash table, perhaps.

It is a vector in the simplest of sense...direction and magnitude.

No i wasnt thinking of sparse memory but more on the lines of distributed memory...whose allocation may not be sequential but spread over pages that are currently in the processes address space without interleaving nulls.

This type of data structure has quite a few applications with hash table being one of them...others include scatter gather I/O and storing variable length strings like the lines of a file for sorting later...and if your doubt persists lookup the manpages of the readv and writev system calls.

Pretty sure the user was talking about C++ vectors, not the physics kind, not the writev kind.

That could very well be so as i said before...leave it upto the op to decide.