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
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();
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?
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
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.