By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,374 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,374 IT Pros & Developers. It's quick & easy.

real noob question

P: 3
hi am new at c++ and am trying my best to learn but am having a little trouble understand one aspect that has been repeated in many text books all with very vague explanations.

i am hoping someone can clear some of this up for me.

my question is in regards to arrays and vectors.

i understand that c++ counts from 0+ and not the natural numbers 1+

so an array of test(23) would be in the range 0-22?

this part i understand, however the text book's i have makes use of terms like test.size() -1 to retrieve the last element of an array, which i am having trouble understand.

anyone able to do a simple example to make it clearer?

thank you in advance
Sep 23 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
If you have an array with 20 elements

int test[20];

Then (as you say) the indexes for those elements are 0 - 19 inclusive, and you will find that there are 20 numbers in that range.

Expand|Select|Wrap|Line Numbers
  1.  0,  1,  2,  3,  4,  
  2.  5,  6,  7,  8,  9,
  3. 10, 11, 12, 13, 14
  4. 15, 16, 17, 18, 19
  5.  
So the array has 20 entries and the index of it's top entry is 19.

If you get the number of elements in the array by using test.size() if it's a class or (sizeof test/sizeof test[0]) if it's a simple array then you will always be able to access the last element of the array using size - 1.

This is true for any size array, an array of 1000 entries will have indexes numbered 0 - 999 inclusive, the last index = size - 1.
Sep 23 '06 #2

P: 3
If you have an array with 20 elements

int test[20];

Then (as you say) the indexes for those elements are 0 - 19 inclusive, and you will find that there are 20 numbers in that range.

Expand|Select|Wrap|Line Numbers
  1.  0,  1,  2,  3,  4,  
  2.  5,  6,  7,  8,  9,
  3. 10, 11, 12, 13, 14
  4. 15, 16, 17, 18, 19
  5.  
So the array has 20 entries and the index of it's top entry is 19.

If you get the number of elements in the array by using test.size() if it's a class or (sizeof test/sizeof test[0]) if it's a simple array then you will always be able to access the last element of the array using size - 1.

This is true for any size array, an array of 1000 entries will have indexes numbered 0 - 999 inclusive, the last index = size - 1.
i see i think i am getting a clearer picture now

Expand|Select|Wrap|Line Numbers
  1. std::vector<double> test(10);
  2.  
would be an array with 10 elements, indexed from 0-9

so if one were to perform the action
Expand|Select|Wrap|Line Numbers
  1.  test.size() 
it would return an integer value of the size of the array which in the above example would be 10

we can therefore use the size() -1 to access the last index?

please correct me if i am wrong
Sep 23 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
please correct me if i am wrong
I can't because you are completely right :D
Sep 23 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.