Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector and push_back()

Reply
Thread Tools

std::vector and push_back()

 
 
mlt
Guest
Posts: n/a
 
      03-08-2009
What is the difference between using push_back or [] on a std::vector?

std::vector<int> temp(3);
for (int i=0; i<3; i++)
{
temp.push_back(i); // or
// temp[i] = i;
}


When I read from the vector later I get an error if I use push_back() above
but no error when I use [].

 
Reply With Quote
 
 
 
 
Annie Testes
Guest
Posts: n/a
 
      03-08-2009
mlt wrote:
> What is the difference between using push_back or [] on a std::vector?


push_back appends a value at the end of the vector.
[] replaces the element at the given index.


> std::vector<int> temp(3);


The above creates a vector containing 3 default constructed elements.
The value of a default constructed int is 0, so the vector
contains {0, 0, 0}.

> for (int i=0; i<3; i++)
> {
> temp.push_back(i); // or
> // temp[i] = i;
> }


Using push_back in the above loop appends 0, 1, 2, so the vector
contains {0, 0, 0, 0, 1, 3} after the loop.

Using [] replaces the first three elements, so the vector
contains {0, 1, 2} after the loop.
 
Reply With Quote
 
 
 
 
ZikO
Guest
Posts: n/a
 
      03-09-2009
mlt wrote:
> What is the difference between using push_back or [] on a std::vector?


It's a big difference. These are two completely different things.

operator[] table-like you can only use within the range of
0 - (size()-1) and by means of which you can read into or write from the
vector. However, if you go out of its range you will likely crash the
program.

push_back() method, on the other hand, adds new value at the end of the
vector increasing its size and you can't use it to read from the vector.

There's also at() method which works similarly to [] but throws
exception if you go out of range.

> std::vector<int> temp(3);
> for (int i=0; i<3; i++)
> {
> temp.push_back(i); // or
> // temp[i] = i;
> }
>
>
> When I read from the vector later I get an error if I use push_back()
> above but no error when I use [].


"i" is a local variable within the for-loop scope. Since you've left the
scope, i no longer exists. This is first thing which might cause error.
Second, if you somehow defined this variable at start and not
initialized it, it can contains anything like 0,2, 200, 465123 depending
of where and how you defined that. if it's let's say local variable it
might have 1425 then your code would look at that particular time like
below:

//...
temp.push_back(1425); // 1425 put at 4th index ( = 3, from 0); size()=4;
temp[1425] = 1425; // very wrong line! essentially you tries to refer
// to not existing index (1425), it's likely to
// crash !!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57