So I have a function, where KaylesPosition is a class with a vector<int> called piles:
// 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;
}
My expected output should be all of the elements in piles in order, separated by periods. However instead, I get key return as "_M_range_check". I have tried this using std::string.append() and I get either an empty string or a period. How do I get this function to return a string of all of the values in piles as expected?
key += std::to_string(temp.at(i));might suit you better. Also,"" + temp.at(0)evaluates to a random address, not a string.