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


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

查看所有标签

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

科学计算导论

科学计算导论

希思 / 清华大学出版社 / 2005-10 / 48.00元

本书全面地介绍了科学计算中解各种主要问题的数值方法,包括线性和非线性方程、最小二乘法、特征值、最优化、插值、积分、常微分方程和偏微分方程、快速傅里叶变换和随机数生成。 本书的特点是: 以使用算法的读者为对象,重点讲授算法背后的思想和原理,而不是算法的详细分析。 强调敏感性和病态性等概念,对同一问题的不同算法进行比较和评价,提高读者对算法的鉴赏能力。 对每类......一起来看看 《科学计算导论》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换