How to get the element index of an array

Ok.... sorry for the ackward phrasing, but basically, if I have a certain value such a Au for gold, and I give it a certain array slot, how can I get the number of the slot it is in? Ala, in the array, I have the first value as H for hydrogen, He for helium as the second, and so on. What should happen is when I get user input for an element, then it returns the atomic number, which should also be its array slot+1?
You can use the find algorithm on the array (or better yet, a vector), or you could use a map instead, which would have better performance.

http://www.cplusplus.com/reference/algorithm/find/
http://www.cplusplus.com/reference/stl/map/
A map will give worse performance than a array if he needs to find by atomic number, as well.

There's two things you can do:
1. Keep everything single array sorted by atomic number and find symbol->number by using std::find().
2. Keep everything in two arrays: one sorted by atomic number, and the other by symbol. Use indexing on the first one to find number->symbol, and use std::binary_search() on the second one to find symbol->number.

1 is simpler to implement, but 2 gives better performance.
Last edited on
A boost::multi_index_container is the way to go if you need to index by multiple things.
Topic archived. No new replies allowed.