在C++中使用STL,我將如何應用函數來獲取std::string
(值的打印表示)並將std::string
(s)應用到按浮點鍵排序的集合中的每個值它來自應用於地圖中每個對應值的另一個函數?如何將函數應用於地圖的每個值以創建排序序列?
換言之,我想迭代映射中的鍵值對,並創建一組新的鍵值對,其中新鍵和值是舊值的函數。
double getNewKey(origValue value);
std::string getNewValue(origValue value);
// Or is it better to map both at once in a pair?
std::pair<double, std::string> getNewPair(origValue value);
std::map<origKey, origValue> origMap;
// Perform some transformation on each value of origMap to get a new map:
std::map<double, std::string> transformedMap =
/* What goes here to use getNewKey() and getNewValue() or use getNewPair()? */
;
但是,請不要使用C++ 11。
查找到'的std :: transform'和'的std :: back_inserter' – 2012-08-03 18:55:45
雖然我認爲如果輸出也是一張地圖,你需要'std :: inserter' – Useless 2012-08-03 18:57:02