1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <vector>
#include<string>
#include<iostream>
using namespace std;
template <typename T>
T demap(const vector<T> &a,const vector<int> &r,int idx)
{
T ret;
for (int n =0;n<r.size();n++)
{ if (r[n]==idx) return a[n];}
cout<<idx<< ": invalid_argument"<<endl;
return ret;
}
int main()
{
vector<int> a{1,5,6,2,7,0};
vector<string>s{"a","g","b","w","m","n"};
vector<int> r{3,7,5,2,4,1}; // reference vector.
cout<<demap(a,r,5)<<endl;
cout<<demap(s,r,2)<<endl;
cout<<"press return to exit . . ."<<endl;
cin.get();
}
|