From da7742b2188d694446ffedc1835b778fb71849b7 Mon Sep 17 00:00:00 2001 From: David Sarrut Date: Thu, 27 Oct 2011 15:49:01 +0200 Subject: [PATCH] Add MapToVec (first/second) functions. --- common/clitkCommon.h | 7 +++++++ common/clitkCommon.txx | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/common/clitkCommon.h b/common/clitkCommon.h index 31f28dc..97808d8 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -209,6 +209,13 @@ namespace clitk { //-------------------------------------------------------------------- void PrintMemoryUsed(); + //-------------------------------------------------------------------- + // Convert a map to a vector + template + void MapToVecFirst(const M & m, V & v); + template + void MapToVecSecond(const M & m, V & v); + #include "clitkCommon.txx" } // end namespace diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index dc7f165..5ee83bd 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -195,6 +195,7 @@ std::string GetTypeAsString() } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- template void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) @@ -217,5 +218,22 @@ void CloneImage(const typename ImageType::Pointer & input, typename ImageType::P } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl +template +void MapToVecFirst(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->first ); + } +} +template +void MapToVecSecond(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->second ); + } +} +//-------------------------------------------------------------------- + #endif /* end #define CLITKCOMMON_TXX */ -- 2.45.1