How do vector choose const or non-const?

N

Nephi Immortal

I have a question. If I declare either const or non-const iterator
object, how do vector constructor know to recognize const or non-
const?

For example:

vector< int > v;
vector< int >::iterator I_B = v.begin();
vector< int >::iterator I_E = v.end();

const vector< int > cv;
vector< int >::const_iterator cI_B = cv.begin();
vector< int >::const_iterator cI_E = cv.end();

Look at the function declaration below.

template<class InputIterator>
vector(
InputIterator _First,
InputIterator _Last
);

I write another vector constructor.

vector< int > v2( I_B, I_E );
const vector< int > cv2( cI_B, cI_E );

How do vector constructor recognize? It sounds like it must have
first version of const constructor function and second version of non-
const constructor function.
 
N

Nephi Immortal

There's just one constructor. There's no such thing as a const contructoror  
a non-const constructor. In both cases, you are constructing a  
std::vector<int>. The only difference here is that after your vector is  
constructed the resulting object is either mutable or constant.

This particular constructor is actually a template. It'll take any pair of  
iterators, as long as the values they iterate over are convertible to a  
std::int.

 application_pgp-signature_part
< 1KViewDownload

I understand, but I am trying to write vector and iterator from
scratch in order to have better understanding how they behave. Can
you please demonstrate your simple basic code here and tell me how one
constructor is called with either mutable or constant.

I suspect that iterator class must have two versions. First one is
const_iterator class and second one is iterator class. vector is only
one version class to call either const_iterator class or iterator
class.

If iterator has two versions, it means that I don’t need to write
“const iterator< … >” and “iterator< … >” is used. It sounds like
that each member functions don’t need const after parameters.
 
J

James Kanze

There's just one constructor.

You mean that there's just one constructor template. There are
an infinite number of constructors, since there are an infinite
number of possible instantiations of the template. (Not to
mention the other constructors.)
There's no such thing as a const contructor or
a non-const constructor. In both cases, you are constructing a
std::vector<int>. The only difference here is that after your vector is
constructed the resulting object is either mutable or constant.
This particular constructor is actually a template. It'll take any pair of
iterators, as long as the values they iterate over are convertible to a
std::int.

And that both iterators have the same type. What doesn't work
is something like:

std::const_iterator start = v.begin();
std::vector<int> v2( start, v.end() );
 
J

James Kanze

Nephi Immortal writes:
[...]
Well, yes, const_iterator and iterator are two separate classes. However,
there's one template constructor that takes a beginning and an ending
iterator. Roughly speaking, it would look like this:
template<typename iter_type>
vector(iter_type beg_iter, iter_type end_iter)
{
while (beg_iter != end_iter)
push_back(*beg_iter++);
}
Of course, std::vector also has an allocator object, I'm leaving out a lot
of detail here. With the above constructor, you can give it either an
iterator or a const_iterator. Doesn't matter. Works with either one.

The real problem is making it give the desired results for
something like "std::vector<int> v(10, 1);". This isn't an
exact match for the std::vector<int>(size_t, int), and is an
exact match for the template constructor above. And of course,
there are also the complexity constraints when the iterator is a
random_access_iterator.

Implementing this constructor correctly is a non-trivial task,
and really requires a very experiences C++ programmer. (On the
other hand, something like what you've shown is a very good
learning experiment, provided one does understand the
restrictions.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top