The main question is already in the title: How to configure a standard map iterator?
But since most people ask why I need this, I will provide additional information: I have a class that has several maps. I have at least 2 differential equations, at least 2 types of sensors (field or dft) and at least 2 types of space (volume, surface). I need to save all these things and make a correlation between them. So I thought it would be wise to have cards of these things, and when things correlate with each other, they have the same key on their cards.
For simplicity, we consider only three mappings.
class Reader
{
struct MaxwellSensor
{
};
struct FieldSensor
{
uint fieldMember;
};
struct DFTSensor
{
uint dftMember;
};
std::map<uint, MaxwellSensor> maxwellSensors;
std::map<uint, FieldSensor> fieldSensors;
std::map<uint, DFTSensor> dftSensors;
uint getCountOfMaxwellSensors(){
return maxwellSensors.size();
}
uint getMemberForMaxwellSensorByIndex(uint index){
}
};
SensorInterface.
, maxwell, maxwell .
:
class MyType{
public:
uint member;
}
int main(int argc, const char* argv[])
{
Reader myReader;
for(uint i = 0; i < myReader.countOfMaxwellSensors(); ++i)
{
MyType var;
var.member = myReader.getMemberForMaxwellSensorByIndex(i);
}
}
, :
uint getMemberForMaxwellSensorByIndex(uint index)
{
auto maxIt = std::advance(maxwellSensors.begin(), index);
auto foundInFieldSensorsIt = std::find_if(fieldSensors.begin(), fieldSensors.end(), [&] (const std::pair<UInteger_T, FieldSensor>& kvp) { return kvp.first == maxIt->first; });
auto foundInDFTSensorsIt = std::find_if(dftSensors.begin(), dftSensors.end(), [&] (const std::pair<UInteger_T, DFTSensor>& kvp) { return kvp.first == maxIt->first; });
if(foundInFieldSensorsIt != fieldSensors.end())
return fieldSensors[maxIt->first].fieldMember;
else if(foundInDFTSensorsIt != dftSensors.end())
return dftSensors[maxIt->first].fieldMember;
else
{
std::cerr << "something went wrong." << std::endl;
return 0;
}
}
... std::advance(maxwellSensors.begin(), index); :
error:
no matching function for call to 'advance'
auto maxIt = std::advance(maxwellSensors.begin(), index);
^~~~~~~~~~~~
/c++/4.6/bits/stl_iterator_base_funcs.h:171:5: note:
candidate function [with _InputIterator = std::_Rb_tree_iterator<std::pair<const
unsigned int, Reader<double, unsigned int>::MaxwellSensor> >,
_Distance = unsigned int] not viable: expects an l-value for 1st argument
advance(_InputIterator& __i, _Distance __n)
, ?
auto maxIt = maxwellSensors.begin() + index;, .
: for, :
auto maxIt = maxwellSensors.begin();
for(uint i = 0; i < index; ++i){
++maxIt;
}
?
!