【问题标题】:is map<string, string[]> possible in C++?map<string, string[]> 可以在 C++ 中使用吗?
【发布时间】:2012-12-10 06:57:33
【问题描述】:

我正在尝试通过转换我用 java 编写的一些程序来学习 C++。一种是加密程序,它接受文本输入并使其看起来像 DNA(AGCTGTGCT...)。我可以使用 4 个碱基的 256 个密码子加密 64 个字符。 ( "A" = "TGGC", "B" = "ATGC"...) 在 java 中,我创建了一个hashmap&lt;String, String[]&gt;,其中密钥是要加密的字符,值是一个由 4 个字符串组成的数组,其中每个string 是一个随机选择的密码子来替换加密的字符。

在 C++ 中,我试图使用地图来做同样的事情,但它给出了一个我不明白的错误。这是我尝试制作密码子表的代码:

// iterate through the characters and select 4 codons from the list
for(int i = 0; i < 64; i++){
    codonTable[charList[i]][0] = originalCodonList[4 * i];
    codonTable[charList[i]][1] = originalCodonList[4 * i + 1];
    codonTable[charList[i]][2] = originalCodonList[4 * i + 2];
    codonTable[charList[i]][3] = originalCodonList[4 * i + 3];

    }
}

charList 是包含 64 个可编码字符(它们实际上是字符串)的数组,originalCodonList 是包含 256 个密码子的字符串数组。我尝试了几种方法将 4 个密码子分配给地图中的字符串数组,但似乎没有任何效果。这会产生最少的错误垃圾邮件。这是我编译时的输出:

在 /usr/include/c++/4.6/map:61:0 包含的文件中, 来自 Genencrypt.cpp:4: /usr/include/c++/4.6/bits/stl_map.h: 在成员函数'std::map<_key _tp _compare>_Alloc>::mapped_type& std::map<_key _tp _compare _alloc>中: :operator[](const key_type&) >[with _Key = std::basic_string, _Tp = std::basic_string [4], _Compare = >std::less >, _Alloc = std::allocatorsstd::basic_string, std:: basic_string [4]> >, std::map<_key _tp _compare>_Alloc>::mapped_type = std::basic_string [4], std::map<_key _tp _compare>_Alloc>::key_type = std::basic_string]': Genencrypt.cpp:63:25:从这里实例化 /usr/include/c++/4.6/bits/stl_map.h:453:11:错误:从'int'转换为非标量类型>'std::map, std::basic_string [4]>::mapped_type {又名 >std::basic_string [4]}' 请求

当我将它复制到谷歌时,谷歌当然告诉我它太长了。我不是 java 专家,而且我在 java 方面比在 c++ 方面要好得多。

TL;DR:我想在 C++ 中创建一个map&lt;string, string[]&gt;,是否可行以及如何实现?

编辑:这是我修复它的方法,我将 codonList 从 map&lt;string. string[]&gt; 更改为 map&lt;string, vector&lt;string&gt; &gt; 并使用此代码添加密码子

    for(int i = 0; i < 64; i++){
    codonTable[charList[i]].push_back(originalCodonList[4 * i]);
    codonTable[charList[i]].push_back(originalCodonList[4 * i + 1]);
    codonTable[charList[i]].push_back(originalCodonList[4 * i + 2]);
    codonTable[charList[i]].push_back(originalCodonList[4 * i + 3]);
    std::cout << "Working?" << std::endl;
    }

【问题讨论】:

  • 您的标题谈到了 C 以及有关 C++ 的问题本身。标题好像错了。

标签: c++ string map


【解决方案1】:

问题是您不能在标准库容器中使用 C 样式的数组,因为它们不满足某些要求,例如可分配和可复制构造。但是,如果您在编译时知道数组的大小,则可以使用 std::array 容器,如果不知道,则可以使用 std::vector

#include <map>
#include <array>
#include <vector>

std::map<std::string, std::array<std::string, 4>> m_fixed;
std::map<std::string, std::vector<std::string>> m_dynamic;

请注意,std::array 是 C++11 的一部分。如果您不支持 C++11,则可以使用来自 &lt;tr1/array&gt; 标头的 TR1 版本 std::tr1::array,或 Boost 库版本。

【讨论】:

  • 到目前为止它可以工作,我使用了向量实现,因为它不能用 std::array 编译。谢谢!
【解决方案2】:

在 C++ 中,您想要完成的工作可以通过 std::map&lt;std::string, std::vector&lt;std::string&gt; &gt; 来完成。

另外,由于您实际上只是将多个字符串值与单个字符串键相关联,因此您可以使用 std::multimap&lt;std::string, std::string&gt;

另请注意,如果您不关心能否按顺序遍历键,std::unordered_multimap&lt;std::string, std::string&gt; 可能更可取(您通常期望恒定复杂度而不是对数)。

【讨论】:

    【解决方案3】:

    您实际上正在寻找另一个班级。 C++ 有std::multimap&lt;Key, Value&gt;。与常规映射不同,multimap 允许每个键有多个值。在您的情况下,您可以为每个字符串键存储 4 个字符串值。

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 2013-12-18
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多