What is the error-message? What compiler are you using?
This compiles fine on VS2008 with warninglevel 4.
And why do you want to use const ints?
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
concept: the value_type of a standard container (the type of the object stored in the container) is required to be Assignable http://www.sgi.com/tech/stl/Assignable.html
a const T ( or for that matter a T& ) is notAssignable and cannot be the value_type in a standard container.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
for a standard container, begin() and end() are overloaded on the const specifier.
on a modifiable container, begin() and end() return container::iterator.
on a const container, begin() and end() returns container::const_iterator.
template< typename CNTR > // CNTR is a standard container
void foo( CNTR& cntr, const CNTR& const_cntr )
{
typename CNTR::iterator iter = cntr.begin() ;
typename CNTR::const_iterator const_iter = const_cntr.begin() ;
typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ;
typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ;
}
this code will not compile:
#include <vector>
void f( std::vector<int>::const_iterator& i )
{ std::cout << *i << std::endl; }
int main()
{
std::vector<int> v;
std::vector<int>::iterator b = v.begin() ;
f(b);
}
but this will:
template< typename ITERATOR > void f( ITERATOR i )
{ std::cout << *i << std::endl; }
#include <vector>
int main()
{
std::vector<int> v;
std::vector<int>::iterator b = v.begin() ;
f(b);
}
c++09: containers also have:
// ...
const_iterator cbegin() const;
const_iterator cend () const;
const_reverse_iterator crbegin() const;
const_reverse_iterator crend () const;
// ...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1674.pdf
so things become a bit more convenient.
note: prefer passing an iterator by value (rather than by modifiable reference);
it is more flexible for iterations to be non-intrusive.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287