使用两个数组创建映射

时间:2010-08-21 12:30:42

标签: c++ visual-c++ templates transform

我正在尝试使用两个数组创建一个地图。这是代码:

#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   我已经尝试了另一个版本的解决方案来解决这个问题(通过Internet和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;
}

2 个答案:

答案 0 :(得分:1)

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

请注意,C ++ 03编译器不一定需要使用make_pair<string,string>正确获取函数指针。只有C ++ 0x允许直接获取函数模板特化的地址而不进行转换,而只是给出一个模板参数列表,但通常C ++ 03编译器的后端部分即使在C ++ 03模式下也能实现它。但我认为这不太可能是你问题的根源。

答案 1 :(得分:0)

也许我们可以做一些更简单的事情。

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