【STL】set map的基本用法

栏目: 编程工具 · 发布时间: 8年前

内容简介:【STL】set map的基本用法

set

public member function

set ::begin

iterator begin ();
const_iterator begin () const;

Return iterator to beginning

Returns an iterator referring to the first element in the set container.

Internally, set containers keep their elements ordered from lower to higher, therefore begin returns the element with the lowest key value in the set .

Parameters

none

Return Value

An iterator to the first element in the container.

Both iterator and const_iterator are member types. In the set class template, these are bidirectional iterators.

Example

// set::begin/end
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {75,23,65,42,13};
  set<int> myset (myints,myints+5);

  set<int>::iterator it;

  cout << "myset contains:";
  for ( it=myset.begin() ; it != myset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

Output:

myset contains: 13 23 42 65 75

public member function

set ::end

iterator end ();
const_iterator end () const;

Return iterator to end

Returns an iterator referring to the past-the-end element in the set container.

Parameters

none

Return Value

An iterator to the element past the end of the container.

Both iterator and const_iterator are member types. In the set class template, these are bidirectional iterators.

Example

// set::begin/end
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {75,23,65,42,13};
  set<int> myset (myints,myints+5);

  set<int>::iterator it;

  cout << "myset contains:";
  for ( it=myset.begin() ; it != myset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

Output:

myset contains: 13 23 42 65 75

代码实现

void testSet()
{
	set<int> st;
	st.insert(1);
	st.insert(2);
	st.insert(2);
	st.insert(3);
	st.insert(4);
	st.insert(5);
	
	set<int>::iterator it=st.begin();
	while (it!=st.end())
	{
		cout<<*it<<endl;
		it++;
	}
	cout<<endl;
	//cout<<st.count(2)<<endl;

}

multiset

public member function

multiset ::begin

iterator begin ();
const_iterator begin () const;

Return iterator to beginning

Returns an iterator referring to the first element in the multiset container.

Internally, multiset containers keep their elements ordered from lower to higher, therefore begin returns the element with the lowest key value in the multiset .

Parameters

none

Return Value

An iterator to the first element in the container.

Both iterator and const_iterator are member types. In the multiset class template, these are bidirectional iterators.

Example

// multiset::begin/end
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {42,71,71,71,12};
  multiset<int> mymultiset (myints,myints+5);

  multiset<int>::iterator it;

  cout << "mymultiset contains:";
  for ( it=mymultiset.begin() ; it != mymultiset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

Output:

mymultiset contains: 12 42 71 71 71

public member function

multiset ::end

iterator end ();
const_iterator end () const;

Return iterator to end

Returns an iterator referring to the past-the-end element in the multiset container.

Parameters

none

Return Value

An iterator to the element past the end of the container.

Both iterator and const_iterator are member types. In the multiset class template, these are bidirectional iterators.

Example

// multiset::begin/end
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {15,98,77,77,39};
  multiset<int> mymultiset (myints,myints+5);

  multiset<int>::iterator it;

  cout << "mymultiset contains:";
  for ( it=mymultiset.begin() ; it != mymultiset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

Output:

mymultiset contains: 15 39 77 77 98

代码实现

void testMULTISET()
{
	multiset<int> mst;
	mst.insert(12);
	mst.insert(12);
	mst.insert(13);
	mst.insert(14);
	mst.insert(16);
	mst.insert(17);

	multiset<int>::iterator it=mst.begin();
	while (it!=mst.end())
	{
		cout<<*it<<endl;
		it++;
	}
	cout<<endl;
	//cout<<mst.count(12)<<endl;
}

map

public member function

map ::begin

iterator begin ();
const_iterator begin () const;

Return iterator to beginning

Returns an iterator referring to the first element in the map container.

Internally, map containers keep their elements ordered by their keys from lower to higher , therefore begin returns the element with the lowest key value in the map .

Parameters

none

Return Value

An iterator to the first element in the container.

Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.

Dereferencing this iterator accesses the element's value, which is of type pair <const Key,T> .

Example

// map::begin/end
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<char,int> mymap;
  map<char,int>::iterator it;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for ( it=mymap.begin() ; it != mymap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}

Output:

a => 200
b => 100
c => 300

public member function

map ::end

iterator end ();
const_iterator end () const;

Return iterator to end

Returns an iterator referring to the past-the-end element in the map container.

Parameters

none

Return Value

An iterator to the element past the end of the container.

Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.

Dereferencing this iterator accesses the element's value, which is of type pair <const Key,T> .

Example

// map::begin/end
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<char,int> mymap;
  map<char,int>::iterator it;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for ( it=mymap.begin() ; it != mymap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}

Output:

a => 200
b => 100
c => 300

代码实现

void testCOUNTmap()
{
	map<string,int> CountMap;
	CountMap.insert(pair<string,int>("小白",3));
	CountMap.insert(pair<string,int>("小黑",2));
	CountMap.insert(pair<string,int>("小黄",1));
	CountMap.insert(pair<string,int>("小红",3));
	map<string,int>::iterator it=CountMap.begin();
	while (it!=CountMap.end())
	{
		cout<<" count:"<<CountMap.count((*it).first)<<" 名称:"<<(*it).first<<" 编号:" <<(*it).second<<endl;
		it++;
	}
}

求map中前K多的内容

struct CMPbyVALUE
{
	bool operator()(const pair<string,int>&left,const pair<string,int>&right)
	{
		return left.second>right.second;
	}
};
void GetTopKMap1(const int k)
{
	vector<pair<string,int>> v;
	map<string,int> mp;
	map<string,int>::iterator it;
	for (int i=0;i<10;i++)
	{
		mp["苹果"]++;
	}
	for (int i=0;i<9;i++)
	{
		mp["梨"]++;
	}
	for (int i=0;i<8;i++)
	{
		mp["桃子"]++;
	}
	for (int i=0;i<7;i++)
	{
		mp["哈密瓜"]++;
	}
	for(it=mp.begin();it!=mp.end();it++)
	{
		v.push_back(make_pair(it->first,it->second));
	}
	sort(v.begin(),v.end(),CMPbyVALUE());
	for(int j=0;j<k;j++)
		cout<<v[j].first<<" "<<v[j].second<<endl;
	cout<<endl;
}

所有代码

#include <iostream>
#include<algorithm>
#include <string>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
using namespace std;
void testSet()
{
	set<int> st;
	st.insert(1);
	st.insert(2);
	st.insert(2);
	st.insert(3);
	st.insert(4);
	st.insert(5);
	
	set<int>::iterator it=st.begin();
	while (it!=st.end())
	{
		cout<<*it<<endl;
		it++;
	}
	cout<<endl;
	//cout<<st.count(2)<<endl;

}
void testMULTISET()
{
	multiset<int> mst;
	mst.insert(12);
	mst.insert(12);
	mst.insert(13);
	mst.insert(14);
	mst.insert(16);
	mst.insert(17);

	multiset<int>::iterator it=mst.begin();
	while (it!=mst.end())
	{
		cout<<*it<<endl;
		it++;
	}
	cout<<endl;
	//cout<<mst.count(12)<<endl;
}
void testMap()
{
	map<string,int> numNameMap;
	pair<map<string,int>::iterator,bool> ret;
	numNameMap.insert(pair<string,int>("小白",3));
	numNameMap.insert(pair<string,int>("小黑",2));
	numNameMap.insert(pair<string,int>("小黄",1));
	numNameMap.insert(pair<string,int>("小红",3));
	map<string,int>::iterator it;
	for(it=numNameMap.begin();it!=numNameMap.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	cout<<endl;
	ret=numNameMap.insert(pair<string,int>("小红",3));
	if (ret.second==false)
	{
		cout<<ret.first->first<<" "<<ret.first->second<<endl;
	}


}
//void TopCountMap(vector<string>& v)
//{
//	map<string,int> CountMap;
//	CountMap.insert(pair<string,int>("苹果",1));
//	CountMap.insert(pair<string,int>("苹果",1));
//
//	CountMap.insert(pair<string,int>("苹果",1));
//
//	CountMap.insert(pair<string,int>("苹果",1));
//
//	CountMap.insert(pair<string,int>("苹果",1));
//
//	CountMap.insert(pair<string,int>("苹果",1));
//	map<string,int>::iterator i=CountMap.begin();
//	
//	for(;i!=CountMap.end();i++)
//	{
//		pair<map<string,int>::iterator,bool> ret
//			=CountMap.insert(pair<string,int>(v[i],1));
//	}
//	for(int i=0;i!=CountMap.size();i++)
//	{
//	pair<map<string,int>::iterator,bool> ret=CountMap.insert(pair<string,int>(v[i],1));
//	cout<ret.first<<" "<<ret.second <<endl;
//	}
//	
//
//  }
	
void testCOUNTmap()
{
	map<string,int> CountMap;
	CountMap.insert(pair<string,int>("小白",3));
	CountMap.insert(pair<string,int>("小黑",2));
	CountMap.insert(pair<string,int>("小黄",1));
	CountMap.insert(pair<string,int>("小红",3));
	map<string,int>::iterator it=CountMap.begin();
	while (it!=CountMap.end())
	{
		cout<<" count:"<<CountMap.count((*it).first)<<" 名称:"<<(*it).first<<" 编号:" <<(*it).second<<endl;
		it++;
	}
}
struct CMPbyVALUE
{
	bool operator()(const pair<string,int>&left,const pair<string,int>&right)
	{
		return left.second>right.second;
	}
};
void GetTopKMap1(const int k)
{
	vector<pair<string,int>> v;
	map<string,int> mp;
	map<string,int>::iterator it;
	for (int i=0;i<10;i++)
	{
		mp["苹果"]++;
	}
	for (int i=0;i<9;i++)
	{
		mp["梨"]++;
	}
	for (int i=0;i<8;i++)
	{
		mp["桃子"]++;
	}
	for (int i=0;i<7;i++)
	{
		mp["哈密瓜"]++;
	}
	for(it=mp.begin();it!=mp.end();it++)
	{
		v.push_back(make_pair(it->first,it->second));
	}
	sort(v.begin(),v.end(),CMPbyVALUE());
	for(int j=0;j<k;j++)
		cout<<v[j].first<<" "<<v[j].second<<endl;
	cout<<endl;
}
//void GetTopKMap2(const int k)
//{
//	vector<pair<string,int>> v;
//	map<string,int> mp;
//	map<string,int>::iterator it;
//	for (int i=0;i<10;i++)
//	{
//		mp["苹果"]++;
//	}
//	for (int i=0;i<9;i++)
//	{
//		mp["梨"]++;
//	}
//	for (int i=0;i<8;i++)
//	{
//		mp["桃子"]++;
//	}
//	for (int i=0;i<7;i++)
//	{
//		mp["哈密瓜"]++;
//	}
//	for(it=mp.begin();it!=mp.end();it++)
//	{
//		v.push_back(make_pair(it->first,it->second));
//	}
//     v.reserve(v.size());
//	sort(v.begin(),v.end(),CMPbyVALUE());
//	
//	for(int j=0;j<k;j++)
//		cout<<v[j].first<<" "<<v[j].second<<endl;
//	cout<<endl;
//}
int main()
{
	vector<string> v;
	testSet();
	testMULTISET();
	testMap();
	testCOUNTmap();
	GetTopKMap1(3);
	GetTopKMap1(4);
	system("pause");
	return 0;
}

结果

【STL】set map的基本用法


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数文明

数文明

涂子沛 / 中信出版集团 / 2018-9 / 88.00元

从量数、据数、普适记录、人脸识别、以图搜车,到雾计算、城市大脑、单粒度治理、无匿名社会、量子思维……作为中国研究大数据的权威专家,作者在《数文明》一书中,以大数据为核心元素,抽丝剥茧,深入地阐述了这个大数据时代的文明社会——一个全新的数文明时代。 将大数据与人类文明融合在一起,这本书提供给我们的不仅是一种全新的叙事结构,它还将突破你的认知边界和思维极限,给你提供一个应对这个世界的全新的认知方......一起来看看 《数文明》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具