【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的基本用法


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

查看所有标签

猜你喜欢:

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

Unity Shader入门精要

Unity Shader入门精要

冯乐乐 / 人民邮电出版社 / 2016-5-1 / CNY 69.00

本书不仅要教会读者如何使用Unity Shader,更重要的是要帮助读者学习Unity中的一些渲染机制以及如何使用Unity Shader实现各种自定义的渲染效果,希望这本书可以为读者打开一扇新的大门,让读者离制作心目中杰出游戏的心愿更近一步。 本书的主要内容为:第1章讲解了学习Unity Shader应该从哪里着手;第2章讲解了现代GPU是如何实现整个渲染流水线的,这对理解Shader的工......一起来看看 《Unity Shader入门精要》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具