Set
set (set) in accordance with a particular order is stored only element of the container.
In one set, the value of the element may also identify it (the value of type T itself Key), and each value must be unique.
set the value of the element can not be modified once the container (element always const), but it can be inserted into the container or removed from the container.
Internal element, is always set in accordance with its internal comparison (type Compare) instruction specific strict weak ordering criteria to sort.
ratio is generally set unordered_set container vessel (c ++ 11) key to access a single element by slower, but they allow direct sequence based on a subset of the subset iterative.
Set typically implemented as a binary search tree.
Container properties container properties
Associative joint
associated container elements in its key reference, rather than by its absolute position in the container reference.
Ordered order
orderly container element is always strictly abide by the order.
All the elements are inserted in this order dispensing position.
Set set
value of the element is a key for identifying it.
Unique keys unique key
vessel may have no equivalent to two key elements.
Allocator-aware sensing dispenser
container with a dispenser that stores an object needs a dynamic process.
Template parameters Template parameters
T
type elements.
Each container element is provided is also determined by the value of the unique identifier (the value for each key element itself).
Alias is a member of the type set::key_type
and set::value_type
.
Compare Compare
a binary predicate, it uses two parameters of the same element type, and returns a Boolean value.
If the comp is considered to be that type of object, and a and b are the key, then the expression comp(a,b)
if the strict definition of a weak function of the sequence that is located before a b, is returned true
.
set object uses this expression to determine the order of the elements in the container and follow two key elements are equivalent (by comparing the reflection: if !comp(a, b) && !comp(b, a)
, they are equivalent).
You can not have the collection container two elements equivalent.
This can be a pointer to a function or a function object (For example, see the constructor).
The default is less, and its return value is less than the operator application (a <b)
alias for member types set::key_compare
and set::value_compare
.
Alloc allocated
for the definition of the type of memory allocation allocator object model.
By default, the dispenser class template that defines the most simple memory allocation model, and regardless of the value.
Alias as a member type set::allocator_type
.
Member types member type
Member functions member functions
constructor Constructor
Construct set construction set
(1)空容器构造函数(默认构造函数)
构造一个没有元素的空容器。
(2)范围构造器
构造一个包含范围为[first,last)的元素的容器,
每个元素均由该范围内的相应元素构成。
(3)复制构造函数
构造一个容器,其中包含x中每个元素的副本。
// constructing sets
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> first; // empty set of ints
int myints[]= {10,20,30,40,50};
set<int> second (myints,myints+5); // range
set<int> third (second); // a copy of second
set<int> fourth (second.begin(), second.end()); // iterator ctor.
return 0;
}
operator = Operator =
Copy container content copy the contents of the container
// assignment operator with sets
#include <iostream>
#include <set>
int main ()
{
int myints[]={ 12,82,37,64,15 };
std::set<int> first (myints,myints+5); // set with 5 ints
std::set<int> second; // empty set
second = first; // now second contains the 5 ints
first = std::set<int>(); // and first is empty
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
return 0;
}
Iterators: iterator:
begin
Return iterator to beginning
end
Return iterator to end
// set::begin/end
#include <iostream>
#include <set>
int main ()
{
int myints[] = {75,23,65,42,13};
std::set<int> myset (myints,myints+5);
std::cout << "myset contains:";
for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
rbegin
Return reverse iterator to reverse beginning
makes
Return reverse iterator to reverse end
// set::rbegin/rend
#include <iostream>
#include <set>
int main ()
{
int myints[] = {21,64,17,78,49};
std::set<int> myset (myints,myints+5);
std::set<int>::reverse_iterator rit;
std::cout << "myset contains:";
for (rit=myset.rbegin(); rit != myset.rend(); ++rit)
std::cout << ' ' << *rit;
std::cout << '\n';
return 0;
}
Capacity: Capacity:
empty
Test whether container is empty
// set::empty
#include <iostream>
#include <set>
int main ()
{
std::set<int> myset;
myset.insert(20);
myset.insert(30);
myset.insert(10);
std::cout << "myset contains:";
while (!myset.empty())
{
std::cout << ' ' << *myset.begin();
myset.erase(myset.begin());
}
std::cout << '\n';
return 0;
}
myset contains: 10 20 30
size
Return container size
Returns the size of the container
// set::size
#include <iostream>
#include <set>
int main ()
{
std::set<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; ++i) myints.insert(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (100); //将整数 100 插入到set集合myints中
std::cout << "2. size: " << myints.size() << '\n';
myints.erase(5); //将整数 5 从set集合myints中移除
std::cout << "3. size: " << myints.size() << '\n';
return 0;
}
0. size: 0
1. size: 10
2. size: 11
3. size: 10
max_size
Return maximum size
Returns the maximum size of
the maximum number of elements provided to return a container can hold. Because of the known system or library implementation constraints,
it is possible to achieve the maximum potential container size, but it can not guarantee the size of the container can be achieved: before reaching this size, it still does not allocate storage at any time.
Return maximum size
返回最大尺寸
返回设置的容器可以容纳的最大元素数。由于已知的系统或库实现限制,
这是容器可以达到的最大潜在大小,但绝不能保证容器能够达到该大小:在达到该大小之前,它仍然无法在任何时候分配存储。
// set::max_size
#include <iostream>
#include <set>
int main ()
{
int i;
std::set<int> myset;
if (myset.max_size()>1000)
{
for (i=0; i<1000; i++) myset.insert(i);
std::cout << "The set contains 1000 elements.\n";
}
else std::cout << "The set could not hold 1000 elements.\n";
return 0;
}