constant element in std::vector

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

constant element in std::vector

 
0
  #1
Jul 2nd, 2008
Hello,

Can anybody explain why I got an awful error message when try following code:
  1. #include <vector>
  2.  
  3. int main()
  4. {
  5. std::vector<const int> v;
  6. return 0;
  7. }
Thanks in advance,
brain.
Last edited by Narue; Jul 2nd, 2008 at 11:32 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,934
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: constant element in std::vector

 
0
  #2
Jul 2nd, 2008
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?
Last edited by niek_e; Jul 2nd, 2008 at 11:16 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

Re: constant element in std::vector

 
0
  #3
Jul 2nd, 2008
Originally Posted by niek_e View Post
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?
I use gcc version 4.2.2 20070909 (prerelease) (4.2.2-0.RC.1mdv2008.0).

I just want to keep the vector from constant integers.

The error message is the following:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h: In instantiation of ‘__gnu_cxx::new_allocator<const int>’:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/allocator.h:86: instantiated from ‘std::allocator<const int>’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:79: instantiated from ‘std::_Vector_base<const int, std::allocator<const int> >’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:160: instantiated from ‘std::vector<const int, std::allocator<const int> >’
test.cpp:18: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:81: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = const int]’ cannot be overloaded
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:78: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const int]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::deallocate(_Tp*, size_t) [with _Tp = const int]’:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:134: instantiated from ‘void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(_Tp*, size_t) [with _Tp = const int, _Alloc = std::allocator<const int>]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:120: instantiated from ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = const int, _Alloc = std::allocator<const int>]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_vector.h:199: instantiated from ‘std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = const int, _Alloc = std::allocator<const int>]’
test.cpp:18: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:97: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/ext/new_allocator.h:97: error: initializing argument 1 of ‘void operator delete(void*)’
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: constant element in std::vector

 
0
  #4
Jul 3rd, 2008
Try this,
  1. #include <vector>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<const int*> v;
  7. return 0;
  8. }
Last edited by RenjithVR; Jul 3rd, 2008 at 6:59 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: constant element in std::vector

 
0
  #5
Jul 3rd, 2008
try
  1. #include <vector>
  2.  
  3. int main() {
  4. std::vector<const int> v();
  5. return 0;
  6. }
Last edited by ivailosp; Jul 3rd, 2008 at 7:51 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

Re: constant element in std::vector

 
0
  #6
Jul 4th, 2008
Originally Posted by ivailosp View Post
try
  1. #include <vector>
  2.  
  3. int main() {
  4. std::vector<const int> v();
  5. return 0;
  6. }

Thanks. it helps.

But I cannot add any element to it.
v.push_back(5);

causes error message.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: constant element in std::vector

 
0
  #7
Jul 4th, 2008
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 not Assignable and cannot be the value_type in a standard container.
Last edited by vijayan121; Jul 4th, 2008 at 8:31 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

Re: constant element in std::vector

 
0
  #8
Jul 4th, 2008
Originally Posted by vijayan121 View Post
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 not Assignable and cannot be the value_type in a standard container.
Ok, thank you, but I was confused because Visual C++ 2005 compiler lets the following code:

#include <vector>

int main()
{
std::vector<const int> v;
v.push_back(5);
v[0] = 2;
return 0;
}

I also would like to understand if following code cannot be compiled:

#include <vector>
#include <iostream>

void f(std::vector<int>::const_iterator& i)
{
std::cout << *i << std::endl;
}
int main()
{
std::vector<int> v;
for (std::vector<int>::iterator b = v.begin(); b != v.end(); ++b)
f(b);
return 0;
}

but if the prototype of the function is the following everything is OK.

void f(const std::vector<int>::const_iterator& i); or
void f(std::vector<int>::const_iterator i);

Regards,
brain.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: constant element in std::vector

 
1
  #9
Jul 5th, 2008
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.

  1. template< typename CNTR > // CNTR is a standard container
  2. void foo( CNTR& cntr, const CNTR& const_cntr )
  3. {
  4. typename CNTR::iterator iter = cntr.begin() ;
  5. typename CNTR::const_iterator const_iter = const_cntr.begin() ;
  6. typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ;
  7. typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ;
  8. }

this code will not compile:
  1. #include <vector>
  2.  
  3. void f( std::vector<int>::const_iterator& i )
  4. { std::cout << *i << std::endl; }
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
but this will:
  1. template< typename ITERATOR > void f( ITERATOR i )
  2. { std::cout << *i << std::endl; }
  3.  
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
c++09: containers also have:
  1. // ...
  2. const_iterator cbegin() const;
  3. const_iterator cend () const;
  4. const_reverse_iterator crbegin() const;
  5. const_reverse_iterator crend () const;
  6. // ...
http://www.open-std.org/jtc1/sc22/wg...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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: brain is an unknown quantity at this point 
Solved Threads: 0
brain brain is offline Offline
Newbie Poster

Re: constant element in std::vector

 
0
  #10
Jul 10th, 2008
Originally Posted by vijayan121 View Post
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.

  1. template< typename CNTR > // CNTR is a standard container
  2. void foo( CNTR& cntr, const CNTR& const_cntr )
  3. {
  4. typename CNTR::iterator iter = cntr.begin() ;
  5. typename CNTR::const_iterator const_iter = const_cntr.begin() ;
  6. typename CNTR::reverse_iterator rev_iter = cntr.rbegin() ;
  7. typename CNTR::const_reverse_iterator const_rev_iter = const_cntr.rbegin() ;
  8. }

this code will not compile:
  1. #include <vector>
  2.  
  3. void f( std::vector<int>::const_iterator& i )
  4. { std::cout << *i << std::endl; }
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
but this will:
  1. template< typename ITERATOR > void f( ITERATOR i )
  2. { std::cout << *i << std::endl; }
  3.  
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8. std::vector<int> v;
  9. std::vector<int>::iterator b = v.begin() ;
  10. f(b);
  11. }
c++09: containers also have:
  1. // ...
  2. const_iterator cbegin() const;
  3. const_iterator cend () const;
  4. const_reverse_iterator crbegin() const;
  5. const_reverse_iterator crend () const;
  6. // ...
http://www.open-std.org/jtc1/sc22/wg...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.
Thanks everybody for discussion.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC