Created
May 12, 2014 00:16
-
-
Save cporter/ee5f278d40d5860648e0 to your computer and use it in GitHub Desktop.
Making a lookup table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- compile-command: "clang++ -std=c++1y map.cpp -o map" -*- | |
#include <map> | |
#include <string> | |
#include <iostream> | |
class NoteLookup { | |
private: | |
std::map<std::string, double> lookup; | |
public: | |
NoteLookup () | |
: lookup { | |
{ "C", 440.0 }, | |
{ "D", 480.0 }, | |
{ "E", 520.0 }, | |
} | |
{ | |
} | |
bool isNote (const std::string& s) { | |
auto found = lookup . find (s); | |
return found != lookup . end (); | |
} | |
double noteValue (const std::string& s) { | |
auto found = lookup . find (s); | |
return found == lookup . end () ? 0.0 : found -> second; | |
} | |
}; | |
int main (int argc, char **argv) { | |
NoteLookup note_lookup; | |
for (int i = 1; i < argc; ++i) { | |
std::string note { argv[i] }; | |
if (note_lookup . isNote (note)) { | |
std::cout << note << ": " << note_lookup . noteValue (note) << "\n"; | |
} else { | |
std::cout << note << ": ???\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment