Skip to content

Instantly share code, notes, and snippets.

@JohannMG
Created September 24, 2020 03:46
Show Gist options
  • Save JohannMG/4ab5a171a417d40fbdeb240c2390558a to your computer and use it in GitHub Desktop.
Save JohannMG/4ab5a171a417d40fbdeb240c2390558a to your computer and use it in GitHub Desktop.
Confirm: Can use array with const char* to have a sub array of other const char* to fill a QMap
#include <QCoreApplication>
#include <QMap>
#include <QMapIterator>
#include <QDebug>
using StringIntMap = QMap<const char*, int>;
void addToMap(StringIntMap &map, const char* key, int value)
{
map[key] = value;
}
static const struct {
const char* finally;
const char* some;
const char* good;
const char* food;
} GoodKeys = {
.finally = "finally",
.some = "some",
.good = "good",
.food = "food"
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
StringIntMap goodmap;
const char* elsewhere[] = {
GoodKeys.finally,
GoodKeys.some,
GoodKeys.good,
GoodKeys.food,
};
for (const char* key : elsewhere){
goodmap[key] = 400;
}
addToMap(goodmap, "function1", 200);
addToMap(goodmap, "function2", 400);
QMapIterator<const char*, int> iter(goodmap);
while (iter.hasNext()) {
iter.next();
qDebug() << iter.key() << " = " << iter.value();
}
/* prints:
finally = 400
some = 400
good = 400
food = 400
function1 = 200
function2 = 400
*/
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment