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;
}
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;
}
...全文
相关推荐
C++set函数学习
在编程实践中,set和multiset经常被用于解决各种算法问题。比如在数据去重、查找与排序结合的场景中,使用set或multiset可以大大简化代码。
C++ STL关联容器指南[代码]
特别地,文章强调了set/map与multiset/multimap的区别,尤其是在插入重复值时的不同行为。
C++模板(vector、map、multimap、set、multiset)
在C++标准库中,模板被广泛应用于STL(Standard Template Library,标准模板库),其中包括了各种容器如vector、map、multimap、set和multiset。
C++ STL入门教程(7) multimap、multiset的使用
(多元集合)multiset是STL容器中的一种关联容器,它和set的使用方法相同,但是multiset允许元素重复。
C++ STL 中这四个非常重要的关联容器:map, set, multimap, 和 multiset 1
Multiset与set相似,但是允许多个相同的元素存在。它维护一个元素有序集合,并且可以包含重复的元素。


