Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple dictionary in C++

Moving some code from Python to C++.

BASEPAIRS = { "T": "A", "A": "T", "G": "C", "C": "G" } 

Thinking maps might be overkill? What would you use?

like image 378
y2k Avatar asked Mar 01 '13 05:03

y2k


People also ask

Is dictionary available in C language?

Generally, the C standard library does not include a built-in dictionary data structure, but the POSIX standard specifies hash table management routines that can be utilized to implement dictionary functionality.

What is data dictionary in C?

A data dictionary contains metadata i.e data about the database. The data dictionary is very important as it contains information such as what is in the database, who is allowed to access it, where is the database physically stored etc.

What is Hashmap in C?

Hash Table is a data structure which stores data in an associative manner. In hash table, the data is stored in an array format where each data value has its own unique index value. Access of data becomes very fast, if we know the index of the desired data.


2 Answers

You can use the following syntax:

#include <map>  std::map<char, char> my_map = {     { 'A', '1' },     { 'B', '2' },     { 'C', '3' } }; 
like image 136
StackedCrooked Avatar answered Sep 17 '22 21:09

StackedCrooked


If you are into optimization, and assuming the input is always one of the four characters, the function below might be worth a try as a replacement for the map:

char map(const char in) { return ((in & 2) ? '\x8a' - in : '\x95' - in); } 

It works based on the fact that you are dealing with two symmetric pairs. The conditional works to tell apart the A/T pair from the G/C one ('G' and 'C' happen to have the second-least-significant bit in common). The remaining arithmetics performs the symmetric mapping. It's based on the fact that a = (a + b) - b is true for any a,b.

like image 23
jogojapan Avatar answered Sep 19 '22 21:09

jogojapan