2010-08-21 41 views
3

我正尝试使用两个数组创建地图。下面是代码:使用两个数组创建地图

#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; 
} 

但这里有错误:

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 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(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 ========== 

我该如何解决这个问题?


EDITED 我试图解决的另一个版本,这个问题(通过互联网和C++的网站使用),我已经找到一种方法来解决这个问题,这样

#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; 
} 

回答

1

的代码看起来是有效的,并且在更改for循环中的语法错误(逗号应该是初始部分之后的分号)后,它将使用GCC进行编译。

请注意,C++ 03编译器不一定需要正确得到函数指针make_pair<string,string>。只有C++ 0x允许直接获取函数模板特化的地址而不用强制转换,而仅仅提供一个模板参数列表,但通常C++ 03编译器会将该特性追溯到该特性并在C++ 03模式下实现它。但我认为这不太可能是你问题的根源。

+0

有趣的是,这意味着visual C++编译器有问题吗?分号是我有变化谢谢,但错误与变换功能 – 2010-08-21 13:22:19

+0

我已经删除了make_pair但尚未修复 – 2010-08-21 13:22:54