I recently had need for a C++ collection which could be accessed with multiple keys. Specifically, I had an object that I wanted to look up either by name or id (in O(1) time). One way to do this is to create two unordered_maps – one keyed by id and another by name, but that is error-prone. I ended up finding a boost class which will do this. It took me a while get it working, so I thought I would share the result.
#define's in C++ code are a terrible way to define collections, but they are commonplace in legacy code. After a few years of refining my approach, this is what I have come up with to replace them.
This file contains 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
auto resort = ExternalInterface_GetSkiResort("Tahoe"); | |
auto blackRuns = resort.GetRunsByDifficulty(resort.Resort, SkiRunDifficultyRating::Black); | |
//Process the runs list | |
resort.Destroy(resort.Resort); | |
blackRuns.Destroy(blackRuns.Collection); |
This file contains 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
$val = (Get-ItemProperty -path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize').AppsUseLightTheme | |
$newval = -Not $val | |
Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name AppsUseLightTheme -Value $newval | |
Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name SystemUsesLightTheme -Value $newval |
This file contains 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
void BetterApproach() | |
{ | |
{ | |
shared_ptr<TooManyResponsibilities> root(new TooManyResponsibilities()); | |
{ | |
shared_ptr<Responsibility1> first = static_pointer_cast<Responsibility1>(root); | |
{ | |
shared_ptr<Responsibility2> second = static_pointer_cast<Responsibility2>(root); |
This file contains 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
username=myusername | |
password=mysupersecretpassword |