所以我有一個函數,其中KaylesPosition
是一個vector<int>
稱爲piles
類:C++將矢量項目轉換爲單個字符串錯誤?
// Produces a key to compare itself to equivalent positions
std::string KaylesPosition::makeKey(){
std::vector<int> temp(piles.size());
for (int i = 0;i<piles.size();i++){
temp[i]=piles[i];
}
std::sort (temp.begin(),temp.end());
std::string key = "" + temp.at(0);
for (int i=1 ; i<temp.size() ; i++){
key.push_back('.');
key.push_back(temp.at(i));
}
return key;
}
我的期望輸出應該是所有在piles
元素有序,用句點分隔的。然而,相反,我得到key
返回爲「_M_range_check」。我已經嘗試過使用std :: string.append(),我得到一個空字符串或句點。如何獲得該函數以按預期方式返回piles
中的所有值的字符串?
詛咒C++和它的隱性內部 - >炭轉化。像'key + = std :: to_string(temp.at(i))'''可能更適合你。此外,'「」+ temp.at(0)'計算爲一個隨機地址,而不是一個字符串。 – chris