今天研究了一下形如map<T,vector<T>>或map<vector<T>,T>(T为某个内置类型)的关联容器如何初始化并显示其内容的问题,总结了一些版本如下,欢迎补充指正!

################   版本1   ################

#include<iostream>
#include<string>
#include<map>
#include<vector>

using namespace std;

typedef map<vector<string>,int>  mymap;

int main()
{
	mymap m;
	vector<string> vs;
	vs.push_back("hello");
	m.insert(pair<vector<string>,int>(vs,100));
	
	for(auto &w:m)
	{
		cout<<w.second<<endl;
		for(auto &v:w.first)
			cout<<v<<endl;
	}

	system("pause");
	return 0;
}
################   版本2    ################

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<utility>

using namespace std;

int main()
{
	map<int,vector<int>> a_dd;
	vector<int> a_dd_vec;
	a_dd_vec.push_back(1);
	a_dd[0] = a_dd_vec;
	//或a_dd.insert(make_pair(0,a_dd_vec));
	a_ddd_vec.clear();
	a_ddd_vec.push_back(9);
	a_ddd[1] = a_ddd_vec;

	for(map<int,vector<int>>::iterator iter_int = a_ddd.begin();iter_int != a_ddd.end(); iter_int++)
	{
		//iter_int->first是int索引
		//获取到vecotr<int>内容
		vector<int> a_vec = iter_int->second;
		for(auto &it:iter_int->second)
			cout<<it<<endl;
		a_vec.push_back(3);
	}

	system("pause");
	return 0;
}
################   版本3    ################
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<utility>

using namespace std;

void add_family(map<string,vector<pair<string,int>>> &families,const string &family)
{
	if(families.find(family) == families.end())
		families[family] = vector<pair<string,int>>();
}

void add_child(map<string,vector<pair<string,int>>> &families,const string &family,const string &name,const int birth)
{
	//families[family].push_back(child);
	families[family].push_back(make_pair(name,birth));
}

int main()
{
	map<string ,vector<pair<string,int> > > families;

	add_family(families,"Marry");
	add_child(families,"Marry","Jack",0112);
	add_child(families,"Marry","Tom",0601);
	add_family(families,"John");
	add_child(families,"John","Tony",0625);

	for(auto &f:families)  //这里可加&也可不加
	{
		cout<<f.first<<"家的孩子:";
		for(auto &c:f.second)
			cout<<c.first<<" birthday:"<<c.second<<" ";
		cout<<endl;
	}

	system("pause");
	return 0;
}
################   版本4    ################
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <string>

using namespace std;

int main()
{
	map<string, vector< pair<string, string>>> family;
	string familyName("HelloWorld!");
	vector<pair<string, string>> member;
	pair<string,string> information;
	information = make_pair(familyName,familyName);
	member.push_back(information);
	family.insert(make_pair(familyName, member));
	family.find(familyName);

	//此处的迭代器类型为map<string, vector< pair<string, string> > >::iterator
	for (auto beg = family.begin();beg != family.end();++beg)
    {
		cout<<beg->first.c_str()<<endl;       
        for(auto begvec = beg->second.begin();begvec != beg->second.end();++begvec)  
			//此处的迭代器类型为vector< pair<string, string> >::iterator
        {
			cout<<"first:"<<begvec->first.c_str()<<endl;
			cout<<"second:"<<begvec->second.c_str()<<endl;
        }
    }

	system("pause");
	return 0;
}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐