您的位置:首页 > 其它

STL-------set与multiset

2015-03-03 12:41 323 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: