STL map容器

#include<iostream>
#include<map>
using namespace std;
void printData(map<int, int>&m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end();it++)
	{
		cout << "key:" << it->first << "value" << it->second << endl;
	}

}
void test01()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));//第一种
	m.insert(make_pair(2, 20));//第二种	
	m[3] = 30;
    printData(m);
}
int main()
{
	test01();
	system("pause");
}

STL map容器

STL map容器

 

 map容器中排序:默认是从小到大排序(要用仿函数)

#include<iostream>
#include<map>
using namespace std;
struct  mycompare
{
	bool operator()(int val1, int val2)
	{
		if (val1 > val2)
		{
			return true;
		}
		else
			return false;
	}
};
void printData(map<int, int, mycompare>&m)
{
	for (map<int, int, mycompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key:" << it->first << "value" << it->second << endl;
	}

}


void test01()
{
	map<int, int,mycompare>m;
	m.insert(pair<int, int>(1, 10));//第一种
	m.insert(make_pair(2, 20));//第二种	
	m[3] = 30;
	printData(m);

}
int main()
{
	test01();
	system("pause");
}

STL map容器

lower_bound  upper _bound,equal_range的用法!

#include<iostream>
#include<map>
using namespace std;
struct  mycompare
{
	bool operator()(int val1, int val2)
	{
		if(val1 > val2)
		{
			return true;
		}
		else
			return false;
	}
};
void printData(map<int, int, mycompare>&m)
{
	for (map<int, int, mycompare>::iterator it = m.begin(); it!= m.end(); it++)
	{
		cout << "key:" << it->first << "value" << it->second << endl;
	}

}


void test01()
{
	map<int, int,mycompare>m;

	m.insert(make_pair(2, 20));//第二种	
	
	m[4] = 40;
	m.insert(make_pair(5, 50));
	m.insert(pair<int, int>(1, 10));//第一种
	m.insert(make_pair(6, 60));
	//m.insert(make_pair(3, 30));
	printData(m);
	map<int, int, mycompare>::iterator pos = m.lower_bound(4);
	cout << pos->first << " " << pos->second << endl;//4, 40
	map<int, int, mycompare>::iterator pos1 = m.upper_bound(4);
	cout << pos1->first << " " << pos1->second << endl;//2, 20
	pair <map<int, int>::iterator,map<int, int>::iterator>ret=m.equal_range(4);
	cout << ret.first->first <<" "<< ret.first->second<<endl;
	cout << ret.second->first << " " << ret.second->second << endl;
}
int main()
{
	test01();
	system("pause");
}

STL map容器