set与multiset插入

von_king von_king 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面的程序用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;
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
von_king
C语言 70,040 成员 243,237 内容