変更した値のローカルコピーを取らずにもう一度押す必要はありません。元の地図にstd :: mapの位置を変更しながら繰り返して実行したい
#include <string>
#include <map>
struct EmployeeKey
{
std::string name;
int amount;
int age;
};
struct EmployeeDetail
{
std::string dept;
int section;
int salary;
};
bool compareByNameAge(const std::string& name,
const int& age,
const EmployeeKey& key)
{
return name > key.name && age > key.age;
}
typedef std::map<EmployeeKey, EmployeeDetail> EmployeeMap;
int main()
{
EmployeeMap eMap;
// insert entries to the map
int age = 10;
std::string name = "John";
EmployeeMap transformMap;
foreach(iter, eMap)
{
if (compareByNameAge(name, age, iter->first))
{
//**This is what i want to avoid.......
// take a copy of the data modified
// push it in a new map.
EmployeeDetail det = iter->second;
det.salary = 1000;
transformMap[iter->first] = det;
}
}
//** Also, i need to avoid too...
// do the cpy of the modified values
// from the transform map to the
// original map
foreach(iter1, transformMap)
eMap[iter1->first] = iter1->second;
}
は、参照を追加します。述語はステートレスすることができるので
あなたは、しかし
for_each
を使用することができます。 EmployeeDetail&det = iter-> secondl;マップ内のコピーが変更されます。 – Danあなたのような単純な変更を行うには、 'iter-> second.salary = 1000;'のように適切に動作するはずです。キーを変更したり、コレクションからアイテムを挿入/削除したりすることはできません。 –
これは動作しません。 for_eachバージョンはconst_iteratorsのみをサポートしています。 – Sid