[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppMap

(C++) std::map

The std::map is a STL container class for mapping two kinds of data types.

      1. include<map>
std::map<int,double> myMap;


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).

  1. include <iostream>
  2. include <map>
  3. include <algorithm>
  4. include <memory>
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(); }


[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:

  1. include <map>
  2. include <string>
  3. include <cassert>
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); }


Other container classes

Code links

Links

Reference





last edited (July 24, 2007) by bilderbikkel, Number of views: 10556, Current Rev: 14 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.