Multiple C ++ template collections

Multiple C ++ template collections

multiset (multiple sets) that the program storing a set of data. List with similar collections, multiple elements may be set as a data store and can be used as key data, however, it is different from the set type multi-class set can contain duplicate data.

Learn about set collection class templates ---- "" C ++ template collections

Create a method multiset multiset class template:

#include<set>		//调用set头文件
	...
	...
multiset<int>m;			//定义一个multiset
//multiset表示声明一个多重集合类模板,<type>表示集合的类型,m表示集合名。

multiset target key members of Function:

Just some common, there are many others.

function Explanation
begin Returns a pointer to the first element of the set iterator
end Returns a pointer to the last element of a collection iterator
size Returns the size of the collection
clear Delete all the elements in the collection
count(x) Returns the number of elements of the set value x
find(x) Returns a pointer to the iterator to x, if x does not exist, end return
erase(start,end) Delete vector iterator from start to end within the range of
erase(i) Delete iterator i-th element
insert(i,x) The value x i is inserted into the first position indicated by the iterator
insert(i,start,end) The iterator is inserted into the element from the start to the end of the range specified in the i-th position
insert(i,n,x) The n copies of x into the i-th position indicated by the iterator
swap(set) Swap the contents of two collections

It is worth noting that the collection can not be assigned and the input and output through the index.

Iterator

Access to the various elements of multiset collection, usually using an iterator.

Iterator definition method:

multiset<int>::iterator it=m.begin();

Iterator it defines a start pointer set to point m in the multiset.

Getting applications multiset set of iterator

Insert

	multiset<int>m;
	m.insert(1);m.insert(3);m.insert(5);
	m.insert(7);m.insert(9);m.insert(11);
	multiset<int>::iterator it;
	for(it=m.begin();it!=m.end();it++)
		cout<<*it<<" ";

To explain give you,
first define a set of type int multiset m, inserting operation is repeated six times.
Define an iterator it, it = m.begin () to initialize the head pointer m points.
Then traversing all elements of the output m by it ++.
The output is: 1,357,911

Note that, for the loop termination condition is it! = M.end (), when it = m.end (), it points to the next empty element m the last element, rather than the last element .

Deletion

Undertake insert operation continues to run

	m.erase(--it); 
	for(it=m.begin();it!=m.end();it++)
		cout<<*it<<" ";

By m.erase (- -it); remove the last element m,
the output is: 13579

Find operation

To revisit the search operation the Find:
the Find (x) returns an iterator pointing to x, if x does not exist, end is returned.

Undertake more insertion deletion:

	it=m.find(5);
	if(it!=m.end())
		cout<<"found"<<endl;
	else
		cout<<"not found"<<endl;
	it=m.find(13);
	if(it!=m.end())
		cout<<"found"<<endl;
	else
		cout<<"not found"<<endl;

The output is:
found
not found

Triple Source :

#include<iostream>
#include<set>
using namespace std; 
int main(){
	
	multiset<int>m;
	m.insert(1);m.insert(3);m.insert(5);
	m.insert(7);m.insert(9);m.insert(11);
	multiset<int>::iterator it;
	for(it=m.begin();it!=m.end();it++)
		cout<<*it<<" ";
	cout<<endl;
	
	m.erase(--it); 
	for(it=m.begin();it!=m.end();it++)
		cout<<*it<<" ";
	cout<<endl;
	
	it=m.find(5);
	if(it!=m.end())
		cout<<"found"<<endl;
	else
		cout<<"not found"<<endl;
	it=m.find(13);
	if(it!=m.end())
		cout<<"found"<<endl;
	else
		cout<<"not found"<<endl;
}

Results Figure:
multiset

Published 20 original articles · won praise 34 · views 3357

Guess you like

Origin blog.csdn.net/zhaizhaizhaiaaa/article/details/104102784