我想將NSDictionary映射整數轉換爲浮點值,轉換爲C++ std :: vector,其中原始NSDictionary的鍵是向量的索引。將NSDictionary轉換爲std :: vector
我有代碼,我認爲會工作,但它似乎創建一個向量比字典中鍵值對的數量更大。我正在猜測它是如何處理我向矢量索引的。
任何幫助非常感謝。
這裏是我的代碼:
static std::vector<float> convert(NSDictionary* dictionary)
{
std::vector<float> result(16);
NSArray* keys = [dictionary allKeys];
for(id key in keys)
{
id value = [dictionary objectForKey: key];
float fValue = [value floatValue];
int index = [key intValue];
result.insert(result.begin() + index, fValue);
}
return result;
}
爲什麼不使用'std :: map'。它是STD中的NSDictionary的對應物。也許你的鍵沒有排序,你不能在'std :: vector'上任意插入索引。你需要按順序執行:0,1,2,3 ... –
因爲我正在寫一個插件,它給了我一個NSDictionary,並且希望將它傳遞給我使用的API,它需要一個std :: vector – ekj