STL-------set与multiset
1.set(集)、multiset(多集)
2.红黑树
3.基本操作
insert
count和find
erase
注意:不能通过find进行修改
#include <iostream>
#include <set>
using namespace std;
template <typename Container>
void PrintContents(const Container& c)
{
//必须加typename 否则linux下编译是不通过的
typename Container::const_iterator i = c.begin();
while( i!=c.end() )
{
cout<<*i<<endl;
++i;
}
}
int main( int argc, char** argv )
{
set<int> a;
multiset<int> ma;
a.insert(60);
a.insert(-1);
a.insert(3000);
PrintContents(a);
/*
set<int>::const_iterator citr = a.begin();
for( citr; citr!=a.end(); ++citr )
{
cout<<*citr<<endl;
}*/
ma.insert(a.begin(), a.end());
ma.insert(3000);
ma.insert(3000);
cout<<"3000 --- total:"<<ma.count(3000)<<endl;
PrintContents(ma);
/*
multiset<int>::const_iterator itr = ma.begin();
for( ; itr!=ma.end(); ++itr )
{
cout<<*itr<<endl;
}*/
return 0;
}
请注意 typename Container::const_iterator i ;一定要加上typename。即使有些编译器让你通过。
set.cpp: In function `void PrintContents(const Container&)':
set.cpp:9: error: expected `;' before "i"
set.cpp:11: error: `i' undeclared (first use this function)
set.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears
.)
set.cpp: In function `void PrintContents(const Container&) [with Container = std::set<int, std::le
<int>, std::allocator<int> >]':
set.cpp:26: instantiated from here
set.cpp:9: error: dependent-name ` Container::const_iterator' is parsed as a non-type, but instant
tion yields a type
set.cpp:9: note: say `typename Container::const_iterator' if a type is meant
set.cpp: In function `void PrintContents(const Container&) [with Container = std::multiset<int, st
:less<int>, std::allocator<int> >]':
set.cpp:38: instantiated from here
set.cpp:9: error: dependent-name ` Container::const_iterator' is parsed as a non-type, but instant
tion yields a type
set.cpp:9: note: say `typename Container::const_iterator' if a type is meant
解释:It accurately doesn't treat map<T, A>::const_iterator as a type because it relies on template parameters, T and A. To make the compiler believe you, you need to use the typename keyword.

浙公网安备 33010602011771号