问题
After declaring std::map<std::string, std::string> M
it's possible to:
- Write to the map:
M["Jack"] = "323 Union St";
- Read from the map:
std::cout << M["Jack"];
And yet after declaring boost::associative_property_map<std::map<std::string, std::string>> PM(M)
we are not able to do much more than:
- Write to the property map:
boost::put(PM, "Fred", "323 Union St");
- Read from the property map:
boost::get(PM, "Fred");
What can you do with a property map that you cannot already do with a map?
Context
Consider someone for whom
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
is distinctly false. IIUC, property maps were used so heavily throughout BGL from 1998 to 2002 to eliminate the need to write one library for MSVC and another for gcc. The idea apparently was that what MSVC will handle, gcc will happily also handle. Now that MSVC handles C++98, are property maps still necessary or are they just a relic? Why?
回答1:
PropertyMaps
are largely an interface to abstract away writing and reading of data and through that to hide the implementation of the storage.
Your interface sometimes needs access to data and you don't want to force the kind of storage to use on your users. Inversely you sometime provide data storage but want to hide its implementation. In both cases property maps provide a nice solution for which the AssociativeSequence requirements of the standard library are not really suited.
回答2:
Here is some background on Property Map to compliment pmr's answer.
As I mentioned in the comment. According to the boost documentation (Property Map Library):
"The need for property maps came out of the design of the Boost Graph Library, whose algorithms needed an interface for accessing properties attached to vertices and edges in a graph. In this context the vertex and edge descriptors are the key type of the property maps."
They are an interface, basically what pmr is saying.
I did not find any information relating to MSVC.
Later, under the history section, they say:
"The property map interface originated as data accessors in Dietmar Kühl's Masters Thesis on generic graph algorithms. The property map idea also appeared under the guise of decorators in early versions of the Generic Graph Component Library (GGCL), which is now the Boost Graph Library (BGL). The main motivation for the property map interface was to support the access of data associated with vertices and edges in a graph, though the applicability of property maps goes beyond this."
You can also take a look at Dietmar Kühl's Generic Graph Algorithms from Springer (or Google scholar and click 'all versions' to find a pdf).
来源:https://stackoverflow.com/questions/23540786/do-property-maps-remain-necessary-for-bgl