Simple dictionary in C++

前端 未结 8 1081

Moving some code from Python to C++.

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

Thinking maps might be overkill? W

相关标签:
8条回答
  • 2021-01-30 16:59

    Here's the map solution:

    #include <iostream>
    #include <map>
    
    typedef std::map<char, char> BasePairMap;
    
    int main()
    {
        BasePairMap m;
        m['A'] = 'T';
        m['T'] = 'A';
        m['C'] = 'G';
        m['G'] = 'C';
    
        std::cout << "A:" << m['A'] << std::endl;
        std::cout << "T:" << m['T'] << std::endl;
        std::cout << "C:" << m['C'] << std::endl;
        std::cout << "G:" << m['G'] << std::endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-30 17:02

    Until I was really concerned about performance, I would use a function, that takes a base and returns its match:

    char base_pair(char base)
    {
        switch(base) {
            case 'T': return 'A';
            ... etc
            default: // handle error
        }
    }
    

    If I was concerned about performance, I would define a base as one fourth of a byte. 0 would represent A, 1 would represent G, 2 would represent C, and 3 would represent T. Then I would pack 4 bases into a byte, and to get their pairs, I would simply take the complement.

    0 讨论(0)
提交回复
热议问题