STL map multimap 一些基本操作

21C++ STL map multimap 一些基本操作

#include<map>
#include<string>
using namespace std;

int main(){
	map <int, string> a1;
	multimap <int, string> a2;
	
	map <int, string> b1( a1);
	multimap <int, string> b2( a2);
	
	map <int, string> c1( a1.begin(), a1.end());
	multimap <int, string> c2( a2.begin(), a2.end());
	
}
#include<map>
#include<string>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << i->first<< " -> " << i->second << endl;
	cout << endl;
}

// 定义 降序排序 
template <typename T>
struct SortDescending{
	bool operator() (const T& a, const T& b){
		return ( a > b);
	}
};

int main(){
	//降序的map 
	map <int, string, SortDescending<int>> a1;// or map<int,string,greater<int>> a1;
	
	// 四种插入方法 
	a1.insert( map <int, string> ::value_type ( 3, "Three"));
	a1.insert( make_pair( 1, "One"));
	a1.insert( pair <int, string> ( 2, "Two"));
	a1[4] = "Four";
	
	cout << "a1.size() is " << a1.size() << endl;
	show( a1);
	
	multimap <int, string> a2( a1.begin(), a1.end());
	a2.insert( make_pair( 4, "four"));
	cout << "a2.size() is " << a2.size() << endl;
	show( a2);
	
	//a2.count( key) 返回值为 key 的键值对 对数 
	cout << "the number of pairs with the key 4 is "<< a2.count( 4) << endl; 
}
#include<map>
#include<iostream>
#include<string>
using namespace std;

template <typename T>
void show( const T& a){
	for( auto p = a.begin(); p != a.end(); p ++)
		cout << p -> first << " -> " << p -> second << endl;
	cout << endl;
}
int main(){
	map <int, string> a;
	a.insert( make_pair( 1, "One"));
	a.insert( pair<int, string> ( 3, "Three"));
	a.insert( map <int, string>:: value_type ( 2, "Two"));
	a[4] = "Four";
	
	show( a);
	
	int x;
	cout << "enter the key you want to search" << endl;
	cin >> x;
	
	// a.find() returns a iterator
	auto i = a.find( x);
	if( i != a.end()) cout << i -> first << " -> " << i -> second << endl; 
	else cout << "not found" << endl;
}
#include<map>
#include<string>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for( auto p = a.begin(); p != a.end(); p ++)
		cout << p -> first << " -> " << p -> second << endl;
	cout << endl;
}

int main(){
	multimap <int, string> a;
	a.insert( make_pair( 1, "One"));
	a.insert( make_pair( 1, "ONE"));
	a.insert( make_pair( 1, "ONe"));
	a.insert( make_pair( 1, "OnE"));
	show( a);
	
	int key;
	cout << "enter the key you want to search" << endl;
	cin >> key;
	auto p = a.find( key);
	if( p != a.end()){
		int x = a.count( key);
		for( int i = 0; i < x; i ++){
			cout << "key " << p -> first << ", value[" << i << "] is " << p -> second << endl;
			p ++;
		}
	}
	else	cout << "the key is not found" << endl;
	
}
#include<map>
#include<string>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for( auto p = a.begin(); p != a.end(); p ++)
		cout << p -> first << " -> " << p -> second << endl;
	cout << endl;
}

int main(){
	multimap <int, string> a;
	a.insert( make_pair ( 1, "One"));
	a.insert( make_pair ( 1, "ONe"));
	a.insert( make_pair ( 1, "ONE"));
	a.insert( make_pair ( 2, "Two"));
	a.insert( make_pair ( 2, "TWO"));
	a.insert( make_pair ( 3, "Three"));
	a.insert( make_pair ( 4, "Four"));
	a.insert( make_pair ( 5, "Five"));
	
	show( a);
	
	//1 a.erase( key) 
	int num = a.erase( 1);
	cout <<"erase " << num << " pairs" << endl;
	show( a);
	
	//2 a.erase( iterator)
	auto p = a.find( 2);// 只删除了迭代器指向的元素  删除了1个元素 
	if( p != a.end()) a.erase( p);
	show( a);
	
	//3 a.erase( a.lower_bound( key1), a.upper_bound( key2))
	a.erase( a.lower_bound( 3), a.upper_bound( 5));
	show( a);
	
}
#include<map>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for( auto p = a.begin(); p != a.end(); p ++)
		cout << p -> first << " -> " << p -> second << endl;
	cout << endl;
}

//定义 不区分大小写的 排序 
struct SortDescending{
	bool operator()( const string& a, const string& b) const{
		string s1( a), s2( b);
		transform( s1.begin(), s1.end(), s1.begin(), ::tolower);
		transform( s2.begin(), s2.end(), s2.begin(), ::tolower);
		return ( s1 < s2);
	}
};

int main(){
	// map a 插入时、搜索时 均不区分大小写 
	map <string, string, SortDescending> a;
	a.insert( make_pair ( "John", "135250"));
	a.insert( make_pair ( "JOHN", "135250"));
	a.insert( make_pair ( "Joey", "1823613"));
	a.insert( make_pair ( "Ross", "2224830"));
	
	show( a);
	
	map <string, string> b( a.begin(), a.end());
	show( b);
	
	cout << "In map a: enter the name you want to search"<< endl << '>';
	string s1;
	cin >> s1;
	auto p = a.find( s1);
	if( p != a.end())	cout << p -> first << "'s number is " << p -> second << endl;
	else cout << "not found" << endl;
	
	cout << endl;
	 
	cout << "In map b: enter the name you want to search" << endl << '>';
	string s2;
	cin >> s2;
	auto i = b.find( s2);
	if( i != b.end())	cout << i -> first << "'s number is " << p -> second << endl;
	else cout << "not found" << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值