Assigning atomic number based on atomic symbol I currently have the following code:

unsigned int symToZ(const std::string &amp; sym){
    unsigned int atomicNum;
    if(sym == "H"){atomicNum = 1;}
    else if(sym == "He"){atomicNum = 2;}
    else if(sym == "Li"){atomicNum = 3;}
    else if(sym == "Be"){atomicNum = 4;}
    else if(sym == "B"){atomicNum = 5;}
    else if(sym == "C"){atomicNum = 6;}
    else if(sym == "N"){atomicNum = 7;}
    else if(sym == "O"){atomicNum = 8;}
    else if(sym == "F"){atomicNum = 9;}
    ...

    return atomicNum;
}


The code simply takes in a string sym (which is the atomic symbol, such as He, F, Au, and so on), and converts it to the respective atomic number. As you can see, it involves a lot of repeated code. For such simple code, I feel it is needlessly complex.

How can this be done more effectively?
The most efficient way is to use unordered_map since C++11:

int main(int argc, const char * argv[]) {
    std::unordered_map&lt;std::string, unsigned int&gt; symbol_to_number = {
        {"H",  1},
        {"He", 2},
        {"Li", 3},
        // And so on..
    };

    // Usage:
    std::cout &lt;&lt; symbol_to_number["H"] &lt;&lt; std::endl;
    return 0;
}


Above, you get the number out of a symbol in constant time which improves on going through a lot of if statements.