Yur*_* G. 1 c++ templates iterator stl typename
我在简单的事情上需要帮助
我试图创建课程
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T> class merge_sort
{
protected:
vector<T> merge(const vector<T> &a, const vector<T> &b)
{
vector<T> v;
typename vector<T>::iterator A;
A= a.begin();
typename vector<T>::iterator B;
B= b.begin();
...
Run Code Online (Sandbox Code Playgroud)
但编译器给我下一个错误:
no match for ‘operator=’ in ‘A = ((const std::vector<int, std::allocator<int> >*)a)->std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]()’ merge.cpp /merge_sort line 23 C/C++ Problem
Run Code Online (Sandbox Code Playgroud)
使用
typename vector<T>::const_iterator A = a.begin();
typename vector<T>::const_iterator B = b.begin();
Run Code Online (Sandbox Code Playgroud)
因为a并且b是const引用,所以begin调用const版本,它返回一个const_iterator,而不是一个iterator.您不能将const_iterators 分配给iterators,就像您不能将指针指定给const一样.