Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
UPDATED:
sorry,i think my question wasnt clear enough so i updated my question to this:

i have a vector and it has 5 elements

now how can i use for example second element of it,i mean in array we can use it by [] for example : myarray[1] but in vector i cant use [],what should i do?
thanks in advance
Posted
Updated 27-Jul-11 21:23pm
v4

Use std::list or std:vector ( a vector is an array ). The standard template library is a subject best covered in articles, not forum replies.

STL 101 Part A - Vector[^]
 
Share this answer
 
If you're using this within a for() loop, then odds are you do know the size. Remember that you should only use a dynamic container when you need it, otherwise it will tend to make your program quite a bit slower (depending on which container you choose, the amount will vary) since dynamic memory allocations are quite costly in time.
 
Share this answer
 
Comments
arashmobileboy 27-Jul-11 11:32am    
you said correct,my example isnt completely correct,its just a simple example,please see my updated question and say me is it correct or not,thanks so much
Albert Holguin 27-Jul-11 14:11pm    
Somewhat, the concept is correct, but you have to use an iterator to get the values stored and your evaluation order of the math wouldn't give you the results you expect. If you're ever not sure of evaluation order of math, its best to use parentheses.

Here's a reference for std::list with examples:
http://en.wikipedia.org/wiki/List_%28C%2B%2B%29[^]
Use "at" function of of vector class
 
Share this answer
 
Comments
arashmobileboy 28-Jul-11 4:16am    
solved,thanks so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900