Ok hello, I went to my references and then the compiler and tried some stuff out.
I came up with some interesting conclusions, but something in my new from scratch code doesn't work right.
I guess my original question has been answered, but now after trying various things with the compiler, I have a quandary, and this one seems simple yet it doesn't work.
skipper, you said the following in a recent reply.
Dreamverv3:
And is [i] arbitrary? so could I just as easily use [x] [y] [z]?
Skipper:
Absolutely arbitrary. Doesn't even need to be a single char variable. Give it a name like 'forCntr' if that's your preference.
I tried variables like: short container = 0;
And then tried to do: Weight[container];
I got a segmentation fault when executing that build.
Well I tried all sorts of single character alpha variables and they all would let me populate the vector as shown below:
Code:
vector <short> Weight(10);
short i = 0;
short x = 0;
short c = 0;
for ( i = 0, i < 10, i++)
{
Weight[x] = i;
c = Weight[x];
cout << "The current number is: " << c << endl;
}
This would output, the quote plus the numbers 0-9. Thats great, but then I tried to access an individual element of the vector and cout that value and it would only give me a zero.
This is what I tried:
Code:
for ( i = 0, i < 10, i++)
{
Weight[x] = i;
c = Weight[x];
cout << "The current number is: " << c << endl;
}
cout << "Here is the number 4: " << Weight[5] << endl;
What I got on execution was:
Here is the number 4: 0
I tried storing the Weight[5] in other variables like g,y,q, but they all return 0. I am initialiazing everything upon declaration to 0 to avoid garbage. But the assignments come afterwards. So that can't be doing it.
I like vectors, because they are dynamic, I'm going to try single dimensional arrays next but this should work shouldn't it?
I mean am I missing something here?
Can't I just assign like this:
c = Weight[5];
Aren't accessing elements the whole point of the "[]"
I didn't want to start up a new thread since this was already here and is vaguely related to the subject line.
Thanks, and sorry if this sounds like I'm not treading water, you're answers really haelped with the previous questions.
And about learning the STL, won't that happen as I progress through my c++ books? Is there a separate STL book or internet resource aside from the c++ references.
The way it was worded made it seem like STL is a separate thing to learn.