I am trying to learn C++ by converting some programs I wrote in java. One is an encryption program that takes text input and makes it look like DNA ( AGCTGTGCT… ). I can encrypt 64 characters using 256 codons of 4 bases each. ( “A” = “TGGC”, “B” = “ATGC”…) In java, I make a hashmap<String, String[]> where the key is the character to be encrypted and the value is an array of 4 strings, where each string is a codon that is chosen at random to replace the encrypted character.
In C++ I am trying to use a map to do the same thing, but it gives an error I just don’t understand. Here is the code where I try to make the codon table:
// 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 is the array that holds the 64 encodable characters ( they are really strings ) and originalCodonList is a string array that contains the 256 codons. I’ve tried several ways to assign 4 codons to the string array in the map, but nothing really seems to work. This generates the least error spam. Here is the output when I compile it:
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
Of course google tells me it’s too long when I copy it into google. I’m not an expert in java, and I am much much better at java than at c++.
TL;DR: I want to make a map<string, string[]> in c++, is it possible and how to do it?
EDIT: Here is how I fixed it, I changed codonList from map<string. string[]> to map<string, vector<string> > and used this code to add the codons
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;
}
The problem is that you cannot use C-style arrays in standard library containers, because they do not meet certain requirements such as being assignable and copy constructible. However, you can use the
std::arraycontainer if you know the size of the array at compile time, or anstd::vectorif not:Note that
std::arrayis part of C++11. If you don’t have C++11 support, you may use the TR1 versionstd::tr1::arrayfrom the<tr1/array>header, or the Boost library version.