Basic C++ Question... Associative array?

I have more of a web development programming background where it’s quite simple to create an associative array and do something like:

public $levels = 
    'emergency' => 'bf2020',
    'alert'     => 'f53d3d',
    'critical'  => 'bf6021',
    'error'     => 'f5873d',
    'warning'   => 'f5b23d',
    'notice'    => '3df541',
    'info'      => '3daef5',
    'debug'     => '6592b4',
    'log'       => 'ffffff'

public function log($message, $level = 'log') {
    $level = array_key_exists($level, $this->levels) ? $level : 'log';

    $color = '#' . $this->levels$level];
    $message = sprintf('%s] %s', strtoupper($level), $message);

    // Imagine some logger class exists.
    $this->logger->addLine($message, $color);

I’m trying to create similar code in C++ with Unreal to log to the screen, what I have works but I’m curious to know how I can simplify it…

#include "UntitledProject.h"
#include "ScreenLogger.h"

void UScreenLogger::Log(FString Message, FString Level)
    if (!GEngine) {

    FColor Color;

    if (Level == "emergency") {
        Color = FColor::FromHex("#bf2020");
        Message = "[EMERGENCY] " + Message;
    } else if (Level == "alert") {
        Color = FColor::FromHex("#f53d3d");
        Message = "[ALERT] " + Message;
    } else if (Level == "critical") {
        Color = FColor::FromHex("#bf6021");
        Message = "[CRITICAL] " + Message;
    } else if (Level == "error") {
        Color = FColor::FromHex("#f5873d");
        Message = "[ERROR] " + Message;
    } else if (Level == "warning") {
        Color = FColor::FromHex("#f5b23d");
        Message = "[WARNING] " + Message;
    } else if (Level == "notice") {
        Color = FColor::FromHex("#3df541");
        Message = "[NOTICE] " + Message;
    } else if (Level == "info") {
        Color = FColor::FromHex("#3daef5");
        Message = "[INFO] " + Message;
    } else if (Level == "debug") {
        Color = FColor::FromHex("#6592b4");
        Message = "[DEBUG] " + Message;
    } else {
        Color = FColor::FromHex("#ffffff");
        Message = "[LOG] " + Message;

    GEngine->AddOnScreenDebugMessage(-1, 5.f, Color, Message);

(Side note: Bit ironic that the forum software has syntax highlighting for PHP but hasn’t been modified to support C++ being that it’s used more frequently around here…)

Look up map. It’s a std class for key value pairs. Other options are a pair of arrays or a single multi dimensional array. You could also create a struct for it. C++ also adopted tuples in C++ 11 if you are familiar with that concept.

Oh and switch. Look at switch rather than the if else if ladder

I thought about a switch but C++ doesn’t support string comparisons in switch statements, furthermore, it wouldn’t really reduce the code replication.

Could you give me an example of how a map would be used?


Then don’t use strings in comparisons. It’s bad practice. Just compare an FString of size 16 bytes to a single byte if you use enums for example.
Furthermore, if-ladders look ugly and are bad for optimization.

TMap<FString, FString> ColorToHex;
ColorToHex.Add("Red", "f5873blabla"); 

UE_LOG(LogTemp, Warning, TEXT("The hex of Red is %s"), *ColorToHex"Red"]);

If I remember correctly.

Here is another option.

Look at DataTable, especially if it’s going to be game data that you need to look up such as parameters for a level by it’s name. Just name the Row the level name.