[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppGet
The boost::get function is shorter and internally asserts that the keyvalue is present.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppGet
(C++) boost::get
Function from the Boost C++ Library. Used for Property Maps.Example
The example below compares the std::map::find method to a boost::get on an boost::associative_property_map.The boost::get function is shorter and internally asserts that the keyvalue is present.
struct MyKey { MyKey(const int& x) : mX(x) {} const int mX; }; bool operator<(const MyKey& lhs, const MyKey& rhs) { return lhs.mX < rhs.mX; } struct MyValue { MyValue(const int& y) : mY(y) {} const int mY; }; bool operator==(const MyValue& lhs, const MyValue& rhs) { return lhs.mY == rhs.mY; } std::map<MyKey,MyValue> GetMap() { std::map<MyKey,MyValue> m; m.insert(std::make_pair(MyKey(1),MyValue(1))); m.insert(std::make_pair(MyKey(2),MyValue(4))); m.insert(std::make_pair(MyKey(3),MyValue(9))); return m; } int main() { //Create a std::map typedef std::map<MyKey,MyValue> MyMap; const MyMap m = GetMap(); //Search a values in std::map { const MyKey myKey(2); assert(m.find(myKey)!=m.end()); const MyValue myValue = m.find(myKey)->second; assert(myValue == MyValue(4)); } //Typedef the Boost map typedef boost::const_associative_property_map<MyMap> BoostMap; //Copy std::map to Boost map const BoostMap boostNames(m); //Search a value in Boost map { const MyValue myValue = boost::get(boostNames,MyKey(2)); assert(myValue == MyValue(4)); } }
- include <map>
- include <cassert>
- include <boost/property_map.hpp>
Code links
- assert
- associative_property_map, boost::associative_property_map
- bool
- boost::associative_property_map
- boost::const_associative_property_map
- const
- const_associative_property_map, boost::const_associative_property_map
- int
- main
- map, std::map
- operator
- return
- std::map
- struct
- typedef
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
