Sign in to follow this  

Vector q's

This topic is 4732 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
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
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

This topic is 4732 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this