Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const and STL containers

The following std::vector code is giving errors

int main()
{
    std::vector<const double> VectDouble;
    VectDouble.push_back(2.34);
    VectDouble.push_back(2.33);
    VectDouble.push_back(2.32);

    for(std::vector<const double> VectDouble::iterator i=VectDouble.begin();i!=VectDouble.end();++i)
       std::cout<<*i;

}
like image 957
McGrath Avatar asked Sep 16 '10 03:09

McGrath


2 Answers

Your STL container elements should be assignable and copy-constructible.

const prevents it from being assignable. Remove const and try compiling your code again.

Also change std::vector<double> VectDouble::iterator to

std::vector<double>::iterator

like image 84
Prasoon Saurav Avatar answered Sep 28 '22 23:09

Prasoon Saurav


VectDouble is a variable name.

change

for(std::vector<const double> VectDouble::iterator i=VectDouble.begin();i!=VectDouble.end();++i)

to

for(std::vector<const double>::iterator i=VectDouble.begin();i!=VectDouble.end();++i)

or

typedef  std::vector<const double> vector_t;
for(vector_t::iterator i=VectDouble.begin();i!=VectDouble.end();++i)
like image 33
skimobear Avatar answered Sep 28 '22 23:09

skimobear