【问题标题】:Create map using two arrays使用两个数组创建地图
【发布时间】:2011-04-02 01:05:11
【问题描述】:

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

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

我该如何解决这个问题?


已编辑 我已经尝试过解决这个问题的另一个版本(由 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;
}

【问题讨论】:

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


    【解决方案1】:

    也许我们可以让事情变得更简单。

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

    【讨论】:

      【解决方案2】:

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

      请注意,不一定需要 C++03 编译器才能正确获取带有 make_pair&lt;string,string&gt; 的函数指针。只有 C++0x 允许直接获取函数模板特化的地址而无需强制转换,而仅提供模板参数列表,但通常 C++03 编译器支持该功能并即使在 C++03 模式下也实现它。但我认为这不太可能是您的问题的根源。

      【讨论】:

      • 有趣是不是说visual c++编译器有问题?分号是的,我有更改,谢谢,但转换函数出现错误
      • 我已删除 make_pair 但尚未修复
      猜你喜欢
      • 2021-08-30
      • 2020-11-13
      • 1970-01-01
      • 2012-06-17
      • 2022-01-09
      • 1970-01-01
      • 2020-05-03
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多