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

(C++) Property map

Part of the Boost C++ library.

The classes in this library are:

Example

Here is an example comparing a std::map to a const_associative_property_map. The latter will always give an error when a value is not found, a std::map has to be checked beforehand.

  1. include <map>
  2. include <cassert>
  3. include <string>
  4. include <boost/property_map.hpp>
std::map<std::string,int> GetMap() { std::map<std::string,int> m; m.insert(std::make_pair("One",1)); m.insert(std::make_pair("Ten",10)); m.insert(std::make_pair("Hundred",100)); return m; } int main() { //Create a std::map typedef std::map<std::string,int> MyMap; const MyMap m = GetMap(); //Search all values in std::map { assert(m.find("One")!=m.end()); const int myStdOne = m.find("One")->second; assert(myStdOne == 1); assert(m.find("Ten")!=m.end()); const int myStdTen = m.find("Ten")->second; assert(myStdTen == 10); assert(m.find("Hundred")!=m.end()); const int myStdHundred = m.find("Hundred")->second; assert(myStdHundred == 100); } //Typedef the Boost map typedef boost::const_associative_property_map<MyMap> BoostMap; //Copy std::map to Boost map const BoostMap boostNames(m); //Search all values in Boost map { const int myBoostOne = boost::get(boostNames,"One"); assert(myBoostOne == 1); const int myBoostTen = boost::get(boostNames,"Ten"); assert(myBoostTen == 10); const int myBoostHundred = boost::get(boostNames,"Hundred"); assert(myBoostHundred == 100); } }


Questions

What is the difference between a std::map and a boost::associative_property_map ?

When a nonexisting key value is given (e.g. "khwgfruiwgp" in the example above), the boost::associative_property_map's internal asserts will find it. For the std::map you have to write these asserts yourself every time.

You cannot iterate through a boost::associative_property_map, where with a std::map you can.

Code links

External links



last edited (March 7, 2007) by bilderbikkel, Number of views: 1771, Current Rev: 8 (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.