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
3 1611 Banfa 9,065
Recognized Expert Moderator Expert
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. - 0, 1, 2, 3, 4,
-
5, 6, 7, 8, 9,
-
10, 11, 12, 13, 14
-
15, 16, 17, 18, 19
-
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.
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. - 0, 1, 2, 3, 4,
-
5, 6, 7, 8, 9,
-
10, 11, 12, 13, 14
-
15, 16, 17, 18, 19
-
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 -
std::vector<double> test(10);
-
would be an array with 10 elements, indexed from 0-9
so if one were to perform the action
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
Banfa 9,065
Recognized Expert Moderator Expert
please correct me if i am wrong
I can't because you are completely right :D
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dave Williams |
last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and
feel fairly comfortable at throwing screens up. However, I want to start
writing forms that revolve around an access database.
From what I have read to date it looks like VB can only display 1 record at
a time. I suspect this is not the case. How easy is it to display 10
records at a time (in a table format) that can be scrolled up and down ?
When the current...
|
by: Matt Hollingsworth |
last post by:
Hello,
Very new to python, so a noob question. When I've written stuff in
JavaScript or MEL in the past, I've always adopted the variable naming
convention of using a $ as the first character (no, I don't use perl,
never have). Not possible in python. What are some good options that
people commonly use so that they can easily and quickly identify
variable with just a glance? When I was writing stuff in SoftImage
XSI/JScript, many of...
|
by: davestrike |
last post by:
I am a noob to sql and asp programming. I am working on a db for the
gaming squad I am a member of. One of the pages I created is a roster
list of all the squad members. Part of this roster is listing each
member's email address. What several people have asked of me is to
make it so the email addresses can be clicked on to open their email
programs, just as html allows the mailto function to work.
Here is a copy of the coding I am...
|
by: We need more power captain |
last post by:
Hi,
I know less than a noob, I've been asked to do some compiles in VC++ 6
without knowing too much at all. (I'm a COBOL program normally so
this is all too much for me)
I open VC++6, open the workspace and then:
Build>Batch Build
|
by: Ivan Shevanski |
last post by:
Alright heres another noob question for everyone. Alright, say I have a
menu like this.
print "1. . .Start"
print "2. . .End"
choice1 = raw_input("> ")
and then I had this to determine what option.
| |
by: Dan McCollick |
last post by:
Hi All,
Noob question that I can not seem to figure out:
I am trying to implement a screenscraper to pull data from two seperate
websites, here is some test code so far:
public static void Main(string args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
|
by: AndyW |
last post by:
Hey folks.
I am trying to get a soap wsdl service working and have a bit of a
noob php programming question for it. I'm using PHP 5.x btw.
I have written a soap server that contains a single method in the
form:
SayHelloWorld( $name ) { return "Hello " .$name; };
|
by: Lang Murphy |
last post by:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted. Raw
noob when it comes to .Net and C#.
So I'm going to be more specific... All I need to do at the moment is figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now,...
|
by: SCRIPT KEEPER |
last post by:
Hello,
I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args from the batch, so from the batch I can use the name of the compiled csharp app without having to include the "call" command.
Something like "Compiled Csharp app.exe" "PowerShell Args" etc.....
Thank You for any help.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |