[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppMap
The integer is the key and the double is termed the value[1].
One key can only have one value. If a key needs to have one or more values, use a std::multimap.
An example to map key presses of type WORD to AnsiString in C++ Builder can be found here (although you might not be familar to the data types, it proves the point).
How would one find an equilibrium or a cycle? Easy: one keeps track of the values and their occurance in time. If a value is found that has already been tracked, the period is found, as this equals the actual time minus the time at which this value has been tracked before.
In the code below, the function GetPeriod finds the period of a function in equilibrium. To do so, I use a std::map<double,int> in which the [[CppDouble|double]]'s are values and the [[CppInt|integers]] are the number of timesteps corresponding to this value. When a certain value is already present in this map, the period of this function is the actual timestep minus the time this value appeared for the first time.
Note that the function shown here is a 'brute-force' approach that will always work. An improvement of it would be to first track the value in time: if the value first increases, then one might start mapping after the value has had its first decrease (i.e. it crossed the value it is attracted to).
The reason for this is that the index operator [] is not a const method. This is because this method inserts an element to the std::map! It is created to make insertion easy and to never throw. Therefore, the code above will not compile : if myMap does not have the key i, an exception has to be thrown. So, due to this it does not compile. The way to solve the above example is:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppMap
(C++) std::map
The std::map is a STL container class for mapping two kinds of data types.std::map<int,double> myMap;
- include<map>
The integer is the key and the double is termed the value[1].
One key can only have one value. If a key needs to have one or more values, use a std::multimap.
An example to map key presses of type WORD to AnsiString in C++ Builder can be found here (although you might not be familar to the data types, it proves the point).
Example: finding the period of a function
From Richel Bilderbeek's page:How would one find an equilibrium or a cycle? Easy: one keeps track of the values and their occurance in time. If a value is found that has already been tracked, the period is found, as this equals the actual time minus the time at which this value has been tracked before.
In the code below, the function GetPeriod finds the period of a function in equilibrium. To do so, I use a std::map<double,int> in which the [[CppDouble|double]]'s are values and the [[CppInt|integers]] are the number of timesteps corresponding to this value. When a certain value is already present in this map, the period of this function is the actual timestep minus the time this value appeared for the first time.
Note that the function shown here is a 'brute-force' approach that will always work. An improvement of it would be to first track the value in time: if the value first increases, then one might start mapping after the value has had its first decrease (i.e. it crossed the value it is attracted to).
struct FunctionBase { virtual double GetY(const double& x) const = 0; }; struct FunctionLogisticGrowth : public FunctionBase { FunctionLogisticGrowth(const double& r) : mR(r) {} double GetY(const double& x) const { //The discrete time version of the logistic growth equation //Assumes a K of 1.0 return (mR * x * (1.0 - x)); } const double mR; }; int GetPeriod( const FunctionBase * const anyFunction, const double& xZero, const int& maxT) { std::map<double,int> values; //value, time double x = xZero; for(int t=0; t<maxT; ++t) { if (values.find(x)!=values.end()) { //The previous time this value was found const int tPrev = values[x]; //The distance between now and the previous time const int period = t-tPrev; return period; } //Map this population size to the time now values[x] = t; //Set the value to the next value x = anyFunction->GetY(x); } //Time too long return maxT; } int main() { const int maxT = 100000; const double xZero = 0.1; for (double r = 0.1; r < 4.0; r+=0.001) { std::auto_ptr<FunctionBase> myFunction(new FunctionLogisticGrowth(r)); std::cout << r << " : " << GetPeriod(myFunction.get(),xZero,maxT) << std::endl; } std::cin.get(); }
- include <iostream>
- include <map>
- include <algorithm>
- include <memory>
[C++ Error] Unit1.cpp(6): E2094 'operator+' not implemented in type '_STL::map<int,double,_STL::less<int>,_STL::allocator<_STL::pair<const int,double> > >' for arguments of type 'int'
A very long error for this code:
double getDouble(const std::map<int,double>& myMap, const int& i)
{
return myMap[i];
}
The reason for this is that the index operator [] is not a const method. This is because this method inserts an element to the std::map! It is created to make insertion easy and to never throw. Therefore, the code above will not compile : if myMap does not have the key i, an exception has to be thrown. So, due to this it does not compile. The way to solve the above example is:
std::map<int,std::string> GetNumberMap() { std::map<int,std::string> numberMap; numberMap[0] = "Zero"; numberMap[1] = "One"; numberMap[2] = "Two"; //Etcetera return numberMap; } template <class KeyType, class ValueType> bool IsKeyInMap(const std::map<KeyType,ValueType>& anyMap, const KeyType& key) { if (anyMap.find(key)!=anyMap.end()) return true; else return false; } int main() { const std::map<int,std::string> myMap(GetNumberMap()); assert(IsKeyInMap(myMap,-2)==false); assert(IsKeyInMap(myMap,-1)==false); assert(IsKeyInMap(myMap,0)==true); assert(IsKeyInMap(myMap,1)==true); assert(IsKeyInMap(myMap,2)==true); }
- include <map>
- include <string>
- include <cassert>
Other container classes
Code links
- #include
- algorithm (header file)
- assert
- cassert (header file)
- const
- double
- for
- include
- int
- iostream (header file)
- main
- map (header file)
- memory (header file)
- virtual
Links
- Wikipedia: http://en.wikipedia.org/wiki/Map_(computer_science)
- SGI: http://www.sgi.com/tech/stl/Map.html
Reference
- 1) Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Chapter 'Introduction', paragraph 'Terms, terms, terms'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
