[問題] STL的iterator使用template的問題
想請問各位高手們..
今天有兩個vector容器,型別分別是int及double,
我利用template的方式把iterator指向該二容器,
程式碼如下,但是跑不過去....
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template <class T>
void PrintOut(T& container)
{
if (container.empty())
cout << "容器為空...";
else
{
vector<T>::iterator ptr; //宣告指位器
cout << "容器內資料為:";
for(ptr=container.begin();ptr!=container.end();ptr++)
cout << *ptr << " ";
cout << "\n\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> container(2);
vector<double> box(4);
container[0]=1;
container[1]=2;
PrintOut(container);
box[0]=9.3;
box[1]=8.8;
PrintOut(box);
system("PAUSE");
return 0;
}
錯誤訊息:
error C2679: 二元運算子 '=' : 找不到使用右方運算元型別
'std::_Vector_iterator<_Ty,_Alloc>' 的運算子 (或是沒有可接受的轉換)
不過今天如果我把double或int其中一個先拿掉,
再將PrintOut函式中 vector<T>::iterator ptr 改成 vector<int>::iterator ptr
或是 vector<double>::iterator ptr
就可以跑得過去了.....
想請問一下我在樣板的部份撰寫上有什麼錯誤嘛?
感謝各位~~~~~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.170.116.2
→
01/03 15:53, , 1F
01/03 15:53, 1F
→
01/03 16:49, , 2F
01/03 16:49, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
11
38