![](/img/trans.png)
[英]no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> > >
[英]reverse iterator error : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'
代碼A:
vector< int >::const_reverse_iterator rcit;
vector< int >::const_reverse_iterator tit=v.rend();
for(rcit = v.rbegin(); rcit != tit; ++rcit)
cout << *rcit << " ";
代碼B:
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
cout << *rcit << " ";
CODE A工作正常,但為什么CODE B通過錯誤:
DEV C ++ \\ vector_test.cpp 與'rcit!= std :: vector <_Tp,_Alloc> :: rend()中的'operator!='匹配_Tp = int,_Alloc = std :: allocator' 。
這是我試圖編寫的程序。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include <vector>
using std::vector;
template< typename T > void printVector( const vector< T > &v);
template< typename T >
void printVector( const vector< T > &v)
{
typename vector< T >::const_iterator cit;
for(cit = v.begin(); cit != v.end(); ++cit)
cout << *cit << " ";
}
int main()
{
int number;
vector< int > v;
cout << "Initial size of the vector : " << v.size()
<< " and capacity : " << v.capacity() << endl;
for(int i=0; i < 3; i++)
{
cout << "Enter number : ";
cin >> number;
v.push_back(number);
}
cout << "Now size of the vector : " << v.size()
<< "and capacity : " << v.capacity() << endl;
cout << "output vector using iterator notation " << endl;
printVector(v);
cout << "Reverse of output ";
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); v.rend() != rcit ; ++rcit)
cout << *rcit << " ";
cin.ignore(numeric_limits< streamsize >::max(), '\n');
cin.get();
return 0;
}
問題是rend
方法有兩種形式:
reverse_iterator rend();
const_reverse_iterator rend() const;
在進行比較時,似乎第一個被使用(雖然我不知道為什么),並且運算符!=
用於比較'const'和'non-const'迭代器沒有被定義。 但是,在分配給變量時,編譯器可以推斷出要調用的正確函數,並且一切正常。
在Xcode 4或codepad中編譯以下內容時,我沒有收到任何錯誤或警告:
#include <iostream>
#include <vector>
int main () {
using namespace std;
vector< int > v;
vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
cout << *rcit << " ";
}
您的計划是否以任何重要方式與此不同? 你用的是什么編譯器? ognian的建議有用嗎? 如果你用==
替換!=
怎么辦? (我知道它會導致運行時錯誤,但我很好奇編譯器對此的響應。)
嘗試交換!=的操作數,如下所示:
for(rcit = v.rbegin(); v.rend() != rcit; ++rcit)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.