수정 된 값의 로컬 복사본을 취하지 않고 다시 위치를 수정하지 않고도 위치 수정을 수행하는 가장 좋은 방법을 알아야합니다. 원래지도로.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;
}
참조를 추가합니다. EmployeeDetail & det = iter-> secondl; 지도에서 복사본을 변경합니다. – Dan
간단한 수정을 위해서'iter-> second.salary = 1000;'을 적절하게 수행해야합니다. 그것은 키를 변경하거나 컬렉션에서 항목을 삽입/제거 할 때 사용할 수 없습니다. –
이것은 작동하지 않습니다. for_each 버전은 const_iterators 만 지원합니다. – Sid