[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <map>
  2. include <cassert>
  3. include <boost/property_map.hpp>
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)); } }


Code links



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