Jump to content
  • Advertisement
Sign in to follow this  
fooman_69

Vector q's

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am currently trying to learn about using vectors, but I have a question about initialization. Do you have to code:
vector<int> numbers(numOfElements);
or can you write:
vector<int> numbers;
I am using a struct as the datatype, not an int. Does this matter during the creation of the variable I am using as the vector? While I am here I may as well ask again cause nobody on the .NET forum seems to know why the intellisense doesn't always work... When I use my struct for the vector and attempt to access the members inside (ex. myVector[2].weight), .NET's intellisense fails. Anyone know why?

Share this post


Link to post
Share on other sites
Advertisement
std::vector<int> numbers; is fine, you dont need to let it know how many elements at the start (defaults to 100 i believe). It'll allocate more in anycase if you add too many.

you can create vectors of any type, struct or class.

about intellisense failing no idea, have had it happen to me but just ignore it when it doesn't show up. Some cases it doesn't like c type code in cpp files, others it doesn't like me using the same name for a class even if under two different namespaces.

HTH

Share this post


Link to post
Share on other sites
note if you do it this way

vector <int> numbers;

the [] operator wont work unless you call resize so you have to call push back when you want to add something to the vector

Share this post


Link to post
Share on other sites
Quote:
Original post by nts
std::vector<int> numbers; is fine, you dont need to let it know how many elements at the start (defaults to 100 i believe).


It defaults to an empty vector.

Quote:
While I am here I may as well ask again cause nobody on the .NET forum seems to know why the intellisense doesn't always work...


Its static code analysis is not perfect. Invest in Visual Assist.

Share this post


Link to post
Share on other sites
Quote:
Original post by fooman_69
Hello,
I am currently trying to learn about using vectors, but I have a question about initialization. Do you have to code:
vector<int> numbers(numOfElements);

or can you write:
vector<int> numbers;


If you know how many you want, or have a good guess, then you may as well put it in. It's not needed, though. std::vector provides a no-argument constructor as well, which is invoked by the second form.

Of course, you could have answered the actual question for yourself by just trying it :s

Quote:
I am using a struct as the datatype, not an int. Does this matter during the creation of the variable I am using as the vector?


If you want a vector of foo, then you need to request "vector<foo>". For the STL container classes, read the angle brackets as "of".

Share this post


Link to post
Share on other sites
Thanks for the replies.
Everyone seems to think Visual Assist is a pretty good product to have. I'll take a look into it. The reason I asked the initial vector question was because I initialized a vector without specifying the size and had an "out of bounds" exception right off the bat (it was a simple test program for quicksort). However, when I initialized it to whatever number it worked alright.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!