set与multiset插入
下面的程序用Dev C++编译报错(因为系统是win7,VC6.0问题太多),错误(列出2条):
C:\Users\vonking\Desktop\programming\Sams Teach Yourself C++ in One Hour a Day, 6th Edition\Lesson 20\20.02.cpp In function `void PrintContents(const Container&)': 37
C:\Users\vonking\Desktop\programming\Sams Teach Yourself C++ in One Hour a Day, 6th Edition\Lesson 20\20.02.cpp `iElementLocator' undeclared (first use this function)
问下各位这是什么原因,这个是21天学通C++的源码,但是报错了,并且还有个问题:
就是为什么一定要template <typename Container>
void PrintContents (const Container & stlContainer);
第二句不能写为void PrintContents (set & stlContainer); 然后在函数里面Container::const_iterator写为set::const_iterator????
希望能在这里找到答案,谢谢各位。
#include <set>
#include <iostream>
using namespace std;
template <typename Container>
void PrintContents (const Container & stlContainer);
int main ()
{
set <int> setIntegers;
multiset <int> msetIntegers;
setIntegers.insert (60);
setIntegers.insert (-1);
setIntegers.insert (3000);
cout << "Writing the contents of the set to the screen" << endl;
PrintContents (setIntegers);
msetIntegers.insert (setIntegers.begin (), setIntegers.end ());
msetIntegers.insert (3000);
cout << "Writing the contents of the multiset to the screen" << endl;
PrintContents (msetIntegers);
cout << "Number of instances of '3000' in the multiset are: '";
cout << msetIntegers.count (3000) << "'" << endl;
return 0;
}
template <typename Container>
void PrintContents (const Container & stlContainer)
{
Container::const_iterator iElementLocator = stlContainer.begin ();
while (iElementLocator != stlContainer.end ())
{
cout << *iElementLocator << endl;
++ iElementLocator;
}
cout << endl;
}