Create map using two arrays
I am trying to create a map using two arrays. Here is the code:
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <iterator>
#include <string>
using namespace std;
using std::transform;
int main(){
const char* word[]={"A","B","C","D","E"};
const char * clue[]={"a","b","c","d","e"};
map<string,string>dictionary;
map<string,string>::iterator it;
transform(word, word+sizeof(word)/sizeof(word[0]), clue,
inserter(dictionary,dictionary.end()), make_pair<string,string>);
for (it=dictionary.begin(),it!=dictionary.end();it++)
cout<<it->first<< " "<<it->second<<endl;
return 0;
}
But here are the mistakes:
1>------ Build started: Project: map_array, Configuration: Debug Win32 ------
1>Build started 8/21/2010 4:27:25 PM.
1>PrepareForBuild:
1> Creating directory "c:\users\david\documents\visual studio 2010\Projects\map_array\Debug\".
1>InitializeBuildStatus:
1> Creating "Debug\map_array.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> map_array.cpp
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_OutTy (&)[_OutSize]' from 'std::insert_iterator<_Container>'
1> with
1> [
1> _Container=std::map<std::string,std::string>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1293) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InIt2,_OutTy (&)[_OutSize],_Fn2)' : could not deduce template arg开发者_StackOverflow社区ument for '_OutTy (&)[_OutSize]' from 'std::insert_iterator<_Container>'
1> with
1> [
1> _Container=std::map<std::string,std::string>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1279) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2914: 'std::transform' : cannot deduce template argument as function argument is ambiguous
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2784: '_OutIt std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutIt,_Fn2)' : could not deduce template argument for '_OutIt' from 'std::insert_iterator<_Container>'
1> with
1> [
1> _Container=std::map<std::string,std::string>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1267) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2914: 'std::transform' : cannot deduce template argument as function argument is ambiguous
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2784: '_OutIt std::transform(_InIt1,_InIt1,_InIt2,_OutIt,_Fn2)' : could not deduce template argument for '_OutIt' from 'std::insert_iterator<_Container>'
1> with
1> [
1> _Container=std::map<std::string,std::string>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1249) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2780: '_OutTy *std::transform(_InIt,_InIt,_OutTy (&)[_OutSize],_Fn1)' : expects 4 arguments - 5 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1127) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(16): error C2780: '_OutIt std::transform(_InIt,_InIt,_OutIt,_Fn1)' : expects 4 arguments - 5 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(1111) : see declaration of 'std::transform'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(20): error C2143: syntax error : missing ';' before ')'
1>c:\users\david\documents\visual studio 2010\projects\map_array\map_array\map_array.cpp(20): error C2451: conditional expression of type 'std::_Tree_iterator<_Mytree>' is illegal
1> with
1> [
1> _Mytree=std::_Tree_val<std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.19
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How do I fix this problem?
EDITED I have tried another version of the solution to this problem (using by the Internet and C++ sites), and I have found a way to solve this problem like this
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <iterator>
#include <string>
using namespace std;
template<typename KeyType, typename ValueType, int N>
class mapmaker
{
std::pair<KeyType, ValueType> (&table)[N];
const KeyType (&keys)[N];
const ValueType (&vals)[N];
template<int pos> void fill_pair()
{
table[pos].first = keys[pos];
table[pos].second = vals[pos];
fill_pair<pos-1>();
}
template<> void fill_pair<0>()
{
table[0].first = keys[0];
table[0].second = vals[0];
}
public:
mapmaker( std::pair<KeyType, ValueType> (&t)[N], const KeyType (&k)[N], const ValueType (&v)[N] )
: table(t), keys(k), vals(v)
{
fill_pair<N-1>();
}
};
template<typename KeyType, typename ValueType, int N>
std::map<KeyType,ValueType> make_map(const KeyType (&keys)[N], const ValueType (&vals)[N])
{
std::pair<KeyType, ValueType> table[N];
mapmaker<KeyType, ValueType, N>( table, keys, vals );
return std::map<KeyType, ValueType>(table, table+N);
}
int main(){
static const string word[]={"A","B","C","D","E"};
static const string clue[]={"a","b","c","d","e"};
static map<string,string>dictionary=make_map(word,clue);
map<string,string>::iterator it;
//transform(word,word+sizeof(word)/sizeof(word[0]),clue,clue,inserter(dictionary,dictionary.end()));
for (it=dictionary.begin();it!=dictionary.end();it++)
cout << it->first << " " << it->second << endl;
//cout<<dictionary.size();
return 0;
}
The code looks valid, and it compiles with GCC, after changing the syntax error in your for loop (the comma should be a semicolon after the initial part).
Note that C++03 compilers are not necessarily required to correctly get a function pointer with make_pair<string,string>
. Only C++0x allows directly getting the address of a function template specialization without casting but merely giving a template argument list, but usually C++03 compilers backpart that feature and implement it even in C++03 mode. But i think it is unlikely that this is the source of your problem here.
Maybe can we make something simpler.
vector<string> aKeys = {"#1", "#2", "#3", "#4"};
vector<int> aValues = {1, 2, 3, 4};
map<string,int> createdDict = {};
for (auto i = 0; i < aKeys.size(); i++) {
createdDict[aKeys[i]] = aValues[i];
}
精彩评论