STL-------set与multiset
2015-03-03 12:41
323 查看
1.set(集)、multiset(多集)
2.红黑树
3.基本操作
insert
count和find
erase
注意:不能通过find进行修改
请注意 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
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_iteratoras a type because it relies on template parameters, T and A. To make the compiler believe you, you need to use the typename keyword.
相关文章推荐
- C++之STL(5)之 set 与 multiset
- STL学习笔记-set/multiset容器(集合)
- C++的STL模板set,multiset,map,multimap,deque,list,stack,queue和priority_queue
- STL学习——hash_set/hash_map/hash_multiset/hash_multimap篇
- STL set multiset map multimap unordered_set unordered_map example
- STL - 常用关联容器代码 - set & multiset
- C++ STL中set和multiset的使用方法
- STL之set和multiset
- STL(2) 关联容器之Set和Multiset几个要点
- STL_set&multiset
- STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方法
- (STL之set与multiset)SPOJ - Ada and Field(易)
- (STL之set与multiset)SPOJ - Ada and Field
- STL容器:set,multiset
- 16 STL集合和映射容器set/multiset_map/multimap
- STL:set/multiset用法详解
- STL容器 set、 multiset、map、multimap
- STL---set和multiset
- 2018.2.1【 UVA - 156 】解题报告(STL,set,multiset,make_pair)
- stl之List 、priority_queue、set:仿函数,pair查返回值、multiset