c++ - map<string, string[]> 在 C++ 中可能吗?

标签 c++ string map

我正在尝试通过转换我用 java 编写的一些程序来学习 C++。一种是采用文本输入并使其看起来像 DNA (AGCTGTGCT...) 的加密程序。我可以使用 4 个碱基的 256 个密码子加密 64 个字符。 (“A”=“TGGC”,“B”=“ATGC”...)在 Java 中,我制作了一个 hashmap<String, String[]>其中键是要加密的字符,值是 4 个字符串的数组,其中每个字符串都是随机选择的密码子来替换加密字符。

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

// 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 个密码子分配给映射中的字符串数组,但似乎没有任何效果。这会产生最少的错误垃圾邮件。这是我编译时的输出:

In file included from /usr/include/c++/4.6/map:61:0, from Genencrypt.cpp:4: /usr/include/c++/4.6/bits/stl_map.h: In member function ‘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::allocatorstd::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: instantiated from here /usr/include/c++/4.6/bits/stl_map.h:453:11: error: conversion from ‘int’ to non-scalar type >‘std::map, std::basic_string [4]>::mapped_type {aka >std::basic_string [4]}’ requested

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

TL;DR:我想做一个 map<string, string[]>在 C++ 中,是否可行以及如何实现?

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

    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 风格的数组,因为它们不满足某些要求,例如可赋值和可复制构造。但是,您可以使用 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 支持,您可以使用 TR1 版本 std::tr1::array来自 <tr1/array> header ,或 Boost 库版本。

关于c++ - map<string, string[]> 在 C++ 中可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796360/

相关文章:

c++ - 用于模块化 C++ 框架的 CMAKE

c++ - 运行时堆栈溢出

javascript - 使用 jQuery 获取 onclick 字符串

perl - 匹配字符串数组以在 perl 中搜索的最简单方法?

c++ - std::map 是如何实现的,所以它可以要求它的 key_type 具有可比性?

types - 如何在 F# 中自动执行 map 查找链?

c++ - 函数模板的优先级是否低于相同解析类型的函数?

c++ - libao在window下编译

regex - 保持字符串的前 2 部分有一个分隔符

map - 有没有办法让 golang 的 map[string]interface{} 独一无二?