[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppBoostPropertyMap
The classes in this library are:
You cannot iterate through a boost::associative_property_map, where with a std::map you can.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppBoostPropertyMap
(C++) Property map
Part of the Boost C++ library.The classes in this library are:
- boost::associative_property_map
- boost::const_associative_property_map
- boost::identity_property_map
- boost::iterator_property_map
- boost::vector_property_map
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.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); } }
- include <map>
- include <cassert>
- include <string>
- include <boost/property_map.hpp>
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
- The Boost documentation: http://www.boost.org/libs/property_map/property_map.html
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
