[英]Why is it showing no operator= for my vector iterator?
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. 这是错误,我得到的是C ++ 11而不是以前的版本。 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
. 您需要使用
vector<int>::const_iterator
而不是vector<int>::iterator
。
If you are working with a compiler that supports C++11 or later version, you can use auto
type. 如果您使用的是支持C ++ 11或更高版本的编译器,则可以使用
auto
type。
auto i = A.begin();
You can't use a regular iterator to go through const vector. 你不能使用常规迭代器来通过const向量。
vector<int>::iterator i
must be ::const_iterator i
. vector<int>::iterator i
必须是::const_iterator i
。
See this question What is the difference between const_iterator and non-const iterator in the C++ STL? 看到这个问题C ++ STL中const_iterator和非const迭代器有什么区别?
The iterator needs to be a const_iterator
as the parameter is const vector<int>
迭代器需要是一个
const_iterator
因为参数是const vector<int>
Change the line 改变线
vector<int>::iterator i;
to 至
vector<int>::const_iterator i;
Or use auto
to get the type 或者使用
auto
来获取类型
Since you are passing the vector as a const , a normal iterator won't do the job. 由于您将向量作为const传递,因此普通迭代器不会执行此任务。 Instead you need a const_iterator for a const vector .
相反,你需要const_iterator作为const向量 。
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! 那应该为你解决!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.