Re: [問題] STL的iterator使用template的問題
看板C_and_CPP (C/C++)作者littleshan (我要加入劍道社!)時間15年前 (2011/01/03 15:50)推噓1(1推 0噓 0→)留言1則, 1人參與討論串2/2 (看更多)
※ 引述《LikaiHo ( )》之銘言:
: 想請問各位高手們..
: 今天有兩個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())
^^^^^^^^^^^^^^^^^
既然你這樣用
表示 T 是 vector<int> 或 vector<double>
: cout << "容器為空...";
: else
: {
: vector<T>::iterator ptr; //宣告指位器
所以把 T 代換進去
這樣就變成
vector< vector<int> >::iterator ptr
這應該不是你要的
: cout << "容器內資料為:";
: for(ptr=container.begin();ptr!=container.end();ptr++)
: cout << *ptr << " ";
: cout << "\n\n";
: }
: }
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.15.163
推
01/03 16:49, , 1F
01/03 16:49, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
11
38