C++语法基础--关联容器--set
2013-07-10 23:17
453 查看
1.set(#include<set>)
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> > // set::allocator_type
> class set
2.构造函数
原型:
empty:
explicit set (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
range:
template <class InputIterator>
set (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
copy:
set (const set& x)
Examples:
set<int> first; // empty set of ints
int myints[]= {10,20,30};
set<int> second (myints,myints+3); // range
set<int> third (second); // a copy of second
set<int> fourth (second.begin(), second.end());
// iterator ctor.
2.添加元素,insert()
*与map容器的操作一样,带一个参数的insert版本返回pair类型的对象,包含一个迭代器和一个bool值
原型:
single element :
pair<iterator,bool> insert (const value_type& val);
with hint:
iterator insert (iterator position, const value_type& val);
range :
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
Examples:
int main ()
{
set<int> myset;
set<int>::iterator it;
pair<set<int>::iterator,bool> ret;
// set some initial values:
for (int i=1; i<=3; ++i)
{
myset.insert(i*10); // set: 10 20 30
}
ret = myset.insert(20); // no new element inserted
if (!ret.second)
{
it=ret.first; // "it" now points to element 20
}
myset.insert (it,15);
int myints[]= {5,10,15}; // 10,15 already in set, not inserted
myset.insert (myints,myints+3);
cout << "myset contains:"<<endl;
for (it=myset.begin(); it!=myset.end(); ++it)
{
cout << ' ' << *it; //5 10 15 20 30
}
return 0;
}
3.从set中获取元素find()
*set容器不提供下标操作符
*正如不能修改map中元素的键一样,在获得指向set中某元素的迭代器后,只能对其做读操作。
原型:
iterator find (const value_type& val) const;
Examples:
int main ()
{
set<int> myset;
set<int>::iterator it;
// set some initial values:
for (int i=1; i<=3; i++)
{
myset.insert(i*10); // set: 10 20 30
}
it=myset.find(20);
myset.erase (it);
cout << "myset contains:"<<endl;
for (it=myset.begin(); it!=myset.end(); ++it)
{
std::cout << ' ' << *it; //10 30
}
return 0;
}
4.简单判断某个元素是否存在count().
*与map容器一样,count的返回值只能是1或0
原型:
size_type count (const value_type& val) const;
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> > // set::allocator_type
> class set
2.构造函数
原型:
empty:
explicit set (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
range:
template <class InputIterator>
set (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
copy:
set (const set& x)
Examples:
set<int> first; // empty set of ints
int myints[]= {10,20,30};
set<int> second (myints,myints+3); // range
set<int> third (second); // a copy of second
set<int> fourth (second.begin(), second.end());
// iterator ctor.
2.添加元素,insert()
*与map容器的操作一样,带一个参数的insert版本返回pair类型的对象,包含一个迭代器和一个bool值
原型:
single element :
pair<iterator,bool> insert (const value_type& val);
with hint:
iterator insert (iterator position, const value_type& val);
range :
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
Examples:
int main ()
{
set<int> myset;
set<int>::iterator it;
pair<set<int>::iterator,bool> ret;
// set some initial values:
for (int i=1; i<=3; ++i)
{
myset.insert(i*10); // set: 10 20 30
}
ret = myset.insert(20); // no new element inserted
if (!ret.second)
{
it=ret.first; // "it" now points to element 20
}
myset.insert (it,15);
int myints[]= {5,10,15}; // 10,15 already in set, not inserted
myset.insert (myints,myints+3);
cout << "myset contains:"<<endl;
for (it=myset.begin(); it!=myset.end(); ++it)
{
cout << ' ' << *it; //5 10 15 20 30
}
return 0;
}
3.从set中获取元素find()
*set容器不提供下标操作符
*正如不能修改map中元素的键一样,在获得指向set中某元素的迭代器后,只能对其做读操作。
原型:
iterator find (const value_type& val) const;
Examples:
int main ()
{
set<int> myset;
set<int>::iterator it;
// set some initial values:
for (int i=1; i<=3; i++)
{
myset.insert(i*10); // set: 10 20 30
}
it=myset.find(20);
myset.erase (it);
cout << "myset contains:"<<endl;
for (it=myset.begin(); it!=myset.end(); ++it)
{
std::cout << ' ' << *it; //10 30
}
return 0;
}
4.简单判断某个元素是否存在count().
*与map容器一样,count的返回值只能是1或0
原型:
size_type count (const value_type& val) const;
相关文章推荐
- C++语法基础--关联容器--multimap,multiset
- C++语法基础--关联容器--map(二)--查找并读取map中的元素count(),find()与 从map对象中删除元素erase()
- C++语法基础--关联容器--map(一)--constructor,operator[],insert
- C++中的关联容器set用法
- C++ 关联容器, map,set
- C++ 11 特性:关联容器map、set的使用
- C++语法基础--顺序容器(二)--容器常用操作,迭代器失效总结
- C++:关联容器(pair、map、set)
- C++学习笔记23——关联容器之set
- C++学习_关联容器(map,set)
- C++ STL关联容器 set和map学习笔记
- C++ STL标准库的关联容器set与map
- C++关联容器(unordered_map,vector,unordered_set)
- C++ 10.4 关联容器-----set 类型
- C++语法基础--泛型算法(generic algorithm)--写入容器元素的算法fill(),fill_n,replace()
- 【C/C++】关联容器pair map set
- C++ Primer Chapter 10 关联容器之set & mutimap
- C++学习基础四——顺序容器和关联容器
- C++中关联容器map和set使用说明
- C++语法基础--顺序容器(三)--关系操作符,容器大小的操作,访问元素,删除元素,查找元素