set与multiset插入

von_king 2011-10-17 02:38:23
下面的程序用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;
}
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pdszcxhh 2011-10-17
  • 打赏
  • 举报
回复
学习了
von_king 2011-10-17
  • 打赏
  • 举报
回复
问题解决!谢谢各位。
[Quote=引用 6 楼 ljljlj 的回复:]

不同的编译器对类型的推导能力不一样。在devc++4.9.9.2里,只此一行添加上typename就可通过编译。
typename Container::const_iterator iElementLocator = stlContainer.begin ();
[/Quote]
ljhhh0123 2011-10-17
  • 打赏
  • 举报
回复
不同的编译器对类型的推导能力不一样。在devc++4.9.9.2里,只此一行添加上typename就可通过编译。
typename Container::const_iterator iElementLocator = stlContainer.begin ();
von_king 2011-10-17
  • 打赏
  • 举报
回复
那你知道为什么DEV C++不能编译通过么?他们的标准不同,如果是,麻烦说清楚点;或者是他们的严格程度不一样???因为以前学过,总还是感觉缺少点什么,这次想尽量搞通。麻烦大家了
[Quote=引用 4 楼 luciferisnotsatan 的回复:]

引用 2 楼 von_king 的回复:

2L解决了第二个问题,
现在是第一个问题,如果用模板的话,上面的代码报错了,不知道是什么原因,希望能得到解释,谢谢!

VS2005里,你贴出的代码能正常编译过。
[/Quote]
luciferisnotsatan 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 von_king 的回复:]

2L解决了第二个问题,
现在是第一个问题,如果用模板的话,上面的代码报错了,不知道是什么原因,希望能得到解释,谢谢!
[/Quote]
VS2005里,你贴出的代码能正常编译过。
von_king 2011-10-17
  • 打赏
  • 举报
回复
补充下:
报错在PrintContents函数体中 Container::const_iterator iElementLocator = stlContainer.begin (); 这句。
[Quote=引用 2 楼 von_king 的回复:]

2L解决了第二个问题,
现在是第一个问题,如果用模板的话,上面的代码报错了,不知道是什么原因,希望能得到解释,谢谢!
[/Quote]
von_king 2011-10-17
  • 打赏
  • 举报
回复
2L解决了第二个问题,
现在是第一个问题,如果用模板的话,上面的代码报错了,不知道是什么原因,希望能得到解释,谢谢!
luciferisnotsatan 2011-10-17
  • 打赏
  • 举报
回复
不想用模板,就要明确说明什么类型。你的代码就要有两个函数,一个是set<int>,一个是multiset<int>

void PrintContents (const set <int> & stlContainer)
{
set<int>::const_iterator iElementLocator = stlContainer.begin ();

while (iElementLocator != stlContainer.end ())
{
cout << *iElementLocator << endl;
++ iElementLocator;
}

cout << endl;
}

void PrintContents (const multiset <int> & stlContainer)
{
multiset<int> ::const_iterator iElementLocator = stlContainer.begin ();

while (iElementLocator != stlContainer.end ())
{
cout << *iElementLocator << endl;
++ iElementLocator;
}

cout << endl;
}

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧