如何聲明未知STL容器的迭代器?例如,我想寫收到容器中的功能和打印全部採用迭代:如何聲明未知容器的迭代器變量
template <class Container> void print(Container c) {
// how to declare iterator???????
my_iterator = c.begin();
while(my_iterator!=c.end()) {
cout << *my_iterator << endl;
my_iterator++;
}
}
C++ 11:'auto it = c.begin();' – hmjd
它不是「未知」 - 知道模板類型參數與知道類型一樣好:) – dasblinkenlight
請注意,在這種特殊情況下,您應該通過'const'參考,以避免造成不必要的複製。 – juanchopanza