Jump to content
  • Advertisement
Sign in to follow this  
dbzprogrammer

Quick Vector Question...

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

I'm looking at SGI's vector page, and I have an idea of what I want. Very simple, pre-allocate for speed, and then fill it in. But, when I pre-allocate space, can I fill it up using any of the store functions? (Insert, push, etc...) And how does the "InputIterator" work? I have a large ammount of data I want to move into the class rather quickly, and that seems like the best way of doing it. Link

Share this post


Link to post
Share on other sites
Advertisement
Yes, you will be able to use all of the standard modifier functions. Pre-allocation does just that, it does not change the way in which the vector works.

InputIterator in what context? They talk about input/output iterators later on in the the SGI reference.


jfl.

Share this post


Link to post
Share on other sites
'InputIterator' is a name for a templated type. So you can write code providing any type for the 'begin' and 'end' arguments (as long as they're the same type), and the compiler won't complain about your line of code - but if they're not appropriate types, it will *instead* complain about some code in the internals of the std::vector (or whatever else) implementation, because it tries to do something with your provided arguments that isn't supported by that type. (Phew - got that? :) )

The name is picked in order to communicate to you what the input arguments are supposed to be "capable of" - their 'concept' in standards parlance. So even though the compiler can't check it directly for you (although they want to change that with the next standardization/version of the language), you can look up in SGI's doc what is needed to make it work - here.

In particular you should note that when dealing with containers of foo, e.g. vector<foo>, you can get vector<foo>::iterators from various member functions. Because C++ template stuff likes to make it possible to do the same stuff with primitives that you do with object types, you can get the same effect from an array: if you have an array foo bar[count], then 'foo *' is a valid iterator type: in particular, 'bar' is a foo* equivalent to the beginning of the array, and 'bar + count' is a foo* equivalent to one-past-the-end - which is what you want to provide if you're copying in the whole array:


const int size = 1337;
MyType x[size];
vector<MyType> y(x, x + size);
// You can also use std::algorithms that expect an iterator range, directly
// on an array:
sort(x, x + size); // assuming MyType implements operator<(const Mytype&)


Of course, there are a few different kinds of iterators, which can be roughly organized in terms of how powerful they are. A "random access iterator" (such as an array pointer or a vector iterator) can do basically anything that any kind of iterator might want to do. By contrast, an "output iterator" can only be stepped forward, and only allows for writing elements (not reading them). You should probably also read SGI's Introduction to iterators for more detail..

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!