Why can't I access a const vector with iterator?

Posted by tsubasa on Stack Overflow See other posts from Stack Overflow or by tsubasa
Published on 2010-05-07T21:50:22Z Indexed on 2010/05/07 21:58 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

My example is as below. I found out the problem is with "const" in function void test's parameter. I don't know why the compiler does not allow. Could anybody tell me? Thanks.

vector<int> p;

void test(const vector<int> &blah)
{
   vector<int>::iterator it;
   for (it=blah.begin(); it!=blah.end(); it++)
   {
      cout<<*it<<" ";
   }
}

int main()
{
   p.push_back(1);
   p.push_back(2);
   p.push_back(3);
   test(p);

   return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl