3

This is the function for computing maximum sum of a subarray

int Solution::maxSubArray(const vector<int> &A) {
vector<int>::iterator i;


int max_so_far = *A.begin();
int current_max = *A.begin();


for(i = A.begin(); i != A.end(); ++i)
{   
    current_max = max(*i,*i+current_max);
    max_so_far = max(max_so_far,current_max)
}
return max_so_far;

}

This is the error , I'm getting this for C++11 and not in previous versions. Help me solve this

solution.cpp: In member function 'int Solution::maxSubArray(const      std::vector<int>&)':
solution.cpp:9:8: error: no match for 'operator=' (operand types are     'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*,     std::vector<int> >}' and 'std::vector<int>::const_iterator {aka    __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}')
  for(i = A.begin(); i != A.end(); ++i) 
    ^
solution.cpp:9:8: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
             from /usr/include/c++/4.8/bits/char_traits.h:39,
             from /usr/include/c++/4.8/ios:40,
             from /usr/include/c++/4.8/ostream:38,
             from /usr/include/c++/4.8/iostream:39,
             from solution.h:7,
             from solution.cpp:-3:
/usr/include/c++/4.8/bits/stl_iterator.h:708:11: note:    __gnu_cxx::__normal_iterator<int*, std::vector<int> >&    __gnu_cxx::__normal_iterator<int*, std::vector<int> >::operator=(const   __gnu_cxx::__normal_iterator<int*, std::vector<int> >&)
     class __normal_iterator
            ^
  /usr/include/c++/4.8/bits/stl_iterator.h:708:11: note:   no known      conversion for argument 1 from 'std::vector<int>::const_iterator {aka   __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' to 'const   __gnu_cxx::__normal_iterator<int*, std::vector<int> >&'
 /usr/include/c++/4.8/bits/stl_iterator.h:708:11: note:   __gnu_cxx::__normal_iterator<int*, std::vector<int> >& __gnu_cxx::__normal_iterator<int*, std::vector<int> >::operator=    (__gnu_cxx::__normal_iterator<int*, std::vector<int> >&&)
  /usr/include/c++/4.8/bits/stl_iterator.h:708:11: note:   no known      conversion for argument 1 f
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
hashcoding
  • 109
  • 3
  • 7
  • Aside: `current_max = max(*i,*i+current_max);` is incredibly suspicious, don't you mean `current_sum`? You are summing `A`, and `max_so_far` will only be different if some of the values are negative – Caleth Oct 20 '17 at 10:57

4 Answers4

11

You need to use vector<int>::const_iterator instead of vector<int>::iterator.

If you are working with a compiler that supports C++11 or later version, you can use auto type.

auto i = A.begin();
R Sahu
  • 204,454
  • 14
  • 159
  • 270
3

You can't use a regular iterator to go through const vector.

vector<int>::iterator i must be ::const_iterator i.

See this question What is the difference between const_iterator and non-const iterator in the C++ STL?

Community
  • 1
  • 1
Pavel
  • 5,374
  • 4
  • 30
  • 55
1

Since you are passing the vector as a const, a normal iterator won't do the job. Instead you need a const_iterator for a const vector.

int Solution::maxSubArray(const vector<int> &A) {
vector<int>::const_iterator i;
for(i = A.begin(); i != A.end(); ++i)
{   
 ....
}

That should fix it for you!

digi0ps
  • 11
  • 8
0

The iterator needs to be a const_iterator as the parameter is const vector<int>


Change the line

vector<int>::iterator i;

to

vector<int>::const_iterator i;

Or use auto to get the type

Andreas DM
  • 10,685
  • 6
  • 35
  • 62