I have a map like this:
typedef boost::shared_ptr<NodeNodePtr;
typedef std::vector<NodePtrNodeVecPtr;
typedef std::map<std::string, NodePtrNodeMap;
typedef std::map<std:string, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
maps to lists/vectors like this.
NodeVecPtr nodes;
for (NodeMapItr itr = nmap.begin(); itr != nmap.end(); ++itr)
nodes.push_back(itr->second);
I may also have node be a list of NodePtrs as opposed to vectors. I'm
wondering if there is some standard STL routines, I can use to try to
reduce this kind of code being repeated. I now std::copy() exists but
it takes iterators. But since I'm dealing with maps, I'm not sure how
to pass the second value as an iterator. There is also
std::transform() but that also works with iterators.
I've been searching the web, and found stuff like boost::bind and
boost::iterator_adaptors that might work but I'm trying to read up the
documentation to figure out the syntax.
If somebody can help me in showing how I could accomplish this using
STL/boost features, this will be helpful. Thanks for you time. 5 1872
pallav wrote:
I have a map like this:
typedef boost::shared_ptr<NodeNodePtr;
typedef std::vector<NodePtrNodeVecPtr;
typedef std::map<std::string, NodePtrNodeMap;
typedef std::map<std:string, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
maps to lists/vectors like this.
NodeVecPtr nodes;
for (NodeMapItr itr = nmap.begin(); itr != nmap.end(); ++itr)
nodes.push_back(itr->second);
NodeVecPtr nodes( nmap.begin(), nmap.end() );
or
nodes.append( nmap.begin(), nmap.end() );
I don't know if std::list has an append method, but it does have an
insert method that works like append.
>
I may also have node be a list of NodePtrs as opposed to vectors. I'm
wondering if there is some standard STL routines, I can use to try to
reduce this kind of code being repeated. I now std::copy() exists but
it takes iterators. But since I'm dealing with maps, I'm not sure how
to pass the second value as an iterator. There is also
std::transform() but that also works with iterators.
I've been searching the web, and found stuff like boost::bind and
boost::iterator_adaptors that might work but I'm trying to read up the
documentation to figure out the syntax.
If somebody can help me in showing how I could accomplish this using
STL/boost features, this will be helpful. Thanks for you time.
On Apr 17, 4:58 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
pallav wrote:
I have a map like this:
typedef boost::shared_ptr<NodeNodePtr;
typedef std::vector<NodePtrNodeVecPtr;
typedef std::map<std::string, NodePtrNodeMap;
typedef std::map<std:string, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
maps to lists/vectors like this.
NodeVecPtr nodes;
for (NodeMapItr itr = nmap.begin(); itr != nmap.end(); ++itr)
nodes.push_back(itr->second);
NodeVecPtr nodes( nmap.begin(), nmap.end() );
or
nodes.append( nmap.begin(), nmap.end() );
I don't know if std::list has an append method, but it does have an
insert method that works like append.
But the map and the vector have different value_types, so this won't
work.
Cheers! --M
On Apr 17, 2:55 pm, pallav <pallavgu...@gmail.comwrote:
I have a map like this:
typedef boost::shared_ptr<NodeNodePtr;
typedef std::vector<NodePtrNodeVecPtr;
typedef std::map<std::string, NodePtrNodeMap;
typedef std::map<std:string, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
maps to lists/vectors like this.
NodeVecPtr nodes;
for (NodeMapItr itr = nmap.begin(); itr != nmap.end(); ++itr)
nodes.push_back(itr->second);
I may also have node be a list of NodePtrs as opposed to vectors. I'm
wondering if there is some standard STL routines, I can use to try to
reduce this kind of code being repeated. I now std::copy() exists but
it takes iterators. But since I'm dealing with maps, I'm not sure how
to pass the second value as an iterator. There is also
std::transform() but that also works with iterators.
I've been searching the web, and found stuff like boost::bind and
boost::iterator_adaptors that might work but I'm trying to read up the
documentation to figure out the syntax.
If somebody can help me in showing how I could accomplish this using
STL/boost features, this will be helpful. Thanks for you time.
I don't have time to work up a complete solution here, but here's a
snippet from Karlsson's Boost book (pp. 260f):
void print_string( const std::string& s ) {
std::cout << s << '\n';
}
// ...
std::map<int, std::stringmy_map;
my_map[0] = "Boost";
my_map[1] = "Bind";
std::for_each(
my_map.begin(),
my_map.end(),
boost::bind( &print_string, boost::bind(
&std::map<int,std::string>::value_type::second, _1)
)
);
That should get you started.
Cheers! --M
mlimber wrote:
On Apr 17, 4:58 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>pallav wrote:
But the map and the vector have different value_types, so this won't
work.
Ya - missed that one.
OK - how about an interator that derefs to the second element ?
psuedo code :
template <typename T>
struct second_iterator
{
T m_iterator;
second_iterator( const T& i_iterator );
typename T::second_type & operator*()
{
return m_iterator->second;
}
....
};
template <typename T>
second_iterator<Tmake_second_iterator( const T & i_iterator )
{
return second_iterator<T>( i_iterator );
}
.... and then you can write:
NodeVecPtr nodes(
make_second_iterator( nmap.begin() ),
make_second_iterator( nmap.end() )
);
On Apr 17, 8:55 pm, pallav <pallavgu...@gmail.comwrote:
I have a map like this:
typedef boost::shared_ptr<NodeNodePtr;
typedef std::vector<NodePtrNodeVecPtr;
typedef std::map<std::string, NodePtrNodeMap;
typedef std::map<std:string, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
maps to lists/vectors like this.
NodeVecPtr nodes;
for (NodeMapItr itr = nmap.begin(); itr != nmap.end(); ++itr)
nodes.push_back(itr->second);
I may also have node be a list of NodePtrs as opposed to vectors. I'm
wondering if there is some standard STL routines, I can use to try to
reduce this kind of code being repeated. I now std::copy() exists but
it takes iterators. But since I'm dealing with maps, I'm not sure how
to pass the second value as an iterator. There is also
std::transform() but that also works with iterators.
I've been searching the web, and found stuff like boost::bind and
boost::iterator_adaptors that might work but I'm trying to read up the
documentation to figure out the syntax.
If somebody can help me in showing how I could accomplish this using
STL/boost features, this will be helpful. Thanks for you time.
Wouldn't something like the following work:
template< typename Map >
struct ValueFromMap
: public std::unary_function< typename Map::value_type,
typename Map::mapped_type >
{
typename Map::mapped_type operator()(
typename Map::value_type const&
elem ) const
{
return elem.second ;
}
} ;
typedef boost::transform_iterator< ValueFromMap< NodeMap >,
NodeMap::const_iterator >
ValueIter ;
(I tried it with a std::map< std::string, int >, and it seemed
to work.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Craig Storey |
last post by:
I have a little php application to store day hiking trips and to keep a
running total of their yearly trips for a few users. Currently they
select their hiking routes using drop lists or...
|
by: Fred Ma |
last post by:
Hello,
I was looking at Meyers's "Effective STL", item 23
about choosing between vectors and maps (at least
that the choice for me). In many cases, using
sorted vectors is faster for lookups. ...
|
by: Ash Lux |
last post by:
Does the C++ vector template class use pointer-based link lists?
Could I get a link explaining how it works, regardless of what it uses?
Thank You,
Ash Lux
|
by: LeTubs |
last post by:
Hi
I'm having a bit of trouble with maps (I'm a c programmer making the
transistion).
Now I want to do it this way as I won't know how many files/names untill
runtime, not bothered
by access...
|
by: roberts.noah |
last post by:
It is my understanding that if you contain vectors or maps you don't
need to create copy constructors because the default calls that
constructor. It is my understanding that if you use these types...
|
by: ballpointpenthief |
last post by:
How good is the C language for implementing maps, filters and
accumulators?
SCHEME programmers would be able to pass procedures as arguments quite
easily, whereas C programmers would - I guess -...
|
by: DrLex |
last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
|
by: Jess |
last post by:
Hello,
I tried to clear a vector "v" using "v.clear()". If "v" contains
those objects that are non-built-in (e.g. string), then "clear()" can
indeed remove all contents. However, if "v"...
|
by: mavrik |
last post by:
Hi,
I've written allocator for STL vectors, which take all memory from my heap.
But I don't have any idea to implement the same for Maps.
As maps take Key and Values. So should I use the same...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |