C ++中的简单字典 [英] Simple dictionary in C++

查看:630
本文介绍了C ++中的简单字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一些代码从Python移至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?

推荐答案

如果您要进行优化,并且假设输入始终是四个字符之一,那么下面的函数可以代替地图:

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

它基于您正在处理两个对称对的事实而工作.有条件的工作将A/T对与G/C分开("G"和"C"恰好具有第二个最低有效位).其余的算法执行对称映射.它基于以下事实:a =(a + b)-b对于任何a,b都是正确的.

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.

这篇关于C ++中的简单字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆