vector入参map出参范式

972 篇文章 329 订阅
73 篇文章 32 订阅

         当输入值是一系列值, 而需要求对应的输出值时, 建议用本文要介绍的代码范式。啥也不说了, 直接撸代码:

 

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

void fun(vector<string> &v, map<string, int> &m)
{
	vector<string>::iterator it;
	int i = 0;
	for(it = v.begin(); it != v.end(); ++it)
	{
		m[*it] = i++;
	}
}

int main()
{
        vector<string> v;
        v.push_back("zhangsan");
        v.push_back("lisi");
		
        map<string, int> m;
        fun(v, m);
        map<string, int>::iterator it;
        for(it = m.begin(); it != m.end(); ++it)
        {
			cout << it->first << " : " << it->second << endl;
        }

        return 0;
}

         结果为:

 

 

taoge@localhost test> g++ test.cpp 
taoge@localhost test> ./a.out 
lisi : 1
zhangsan : 0
taoge@localhost test> 


         加上const更好, 变为:

 

 

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

void fun(const vector<string> &v, map<string, int> &m)
{
	vector<string>::const_iterator it;
	int i = 0;
	for(it = v.begin(); it != v.end(); ++it)
	{
		m[*it] = i++;
	}
}

int main()
{
        vector<string> v;
        v.push_back("zhangsan");
        v.push_back("lisi");
		
        map<string, int> m;
        fun(v, m);
        map<string, int>::iterator it;
        for(it = m.begin(); it != m.end(); ++it)
        {
			cout << it->first << " : " << it->second << endl;
        }

        return 0;
}

        结果还是一样。

 

 

        但是, 如下两个代码都错误(如果没有看出错误, 请继续学习C++基础知识奋斗):

 

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

void fun(const vector<string> &v, map<string, int> &m)
{
	vector<string>::iterator it; // error
	int i = 0;
	for(it = v.begin(); it != v.end(); ++it)
	{
		m[*it] = i++;
	}
}

int main()
{
        vector<string> v;
        v.push_back("zhangsan");
        v.push_back("lisi");
		
        map<string, int> m;
        fun(v, m);
        map<string, int>::iterator it;
        for(it = m.begin(); it != m.end(); ++it)
        {
			cout << it->first << " : " << it->second << endl;
        }

        return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

void fun(const vector<string> &v, map<string, int> &m)
{
	const vector<string>::iterator it;  // error
	int i = 0;
	for(it = v.begin(); it != v.end(); ++it)
	{
		m[*it] = i++;
	}
}

int main()
{
        vector<string> v;
        v.push_back("zhangsan");
        v.push_back("lisi");
		
        map<string, int> m;
        fun(v, m);
        map<string, int>::iterator it;
        for(it = m.begin(); it != m.end(); ++it)
        {
			cout << it->first << " : " << it->second << endl;
        }

        return 0;
}

       

 

          OK, 不多说。
       





 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值