Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) ConvertVector

 

Convert code snippet to convert std::vector<X> to std::vector<Y> using boost::lexical_cast.

 

#include <vector>
#include <boost/lexical_cast.hpp>

//From http://www.richelbilderbeek.nl/CppConvertVector.htm
template <class Source, class Target>
const std::vector<Target> ConvertVector(
  const std::vector<Source>& v)
{
  const size_t sz = v.size();
  std::vector<Target> v_new(sz);
  for(size_t i=0; i!=sz; ++i)
  {
    v_new[i] = boost::lexical_cast<Target>(v[i]);
  }
  return v_new;
}

 

 

 

 

 

Versions that do not work

 

At the moment, I do not have time to get the better pieces of code to compile. Feel free to contact me if you can.

 

#include <algorithm>
#include <vector>
#include <boost/lexical_cast.hpp>

//From http://www.richelbilderbeek.nl/CppConvertVector.htm
template <class Source, class Target>
const std::vector<Target> ConvertVector(
  const std::vector<Source>& v)
{
  std::transform(v.begin(),v.end(),std::back_inserter(v_new),
    std::ptr_fun<Source,Target>(boost::lexical_cast<Target>()));
  return v_new;
}

 

#include <algorithm>
#include <vector>
#include <boost/lexical_cast.hpp>

//From http://www.richelbilderbeek.nl/CppConvertVector.htm
template <class Source, class Target>
const std::vector<Target> ConvertVector(
  const std::vector<Source>& v)
{
  std::vector<Target> v_new(v.size());
  const std::vector<Source>::const_iterator source_end = v.end();
  std::vector<Source>::const_iterator source_in = v.begin();
  std::vector<Target>::const_iterator target_out = v_new.begin();
  for( ; source_in!=source_end; ++source_in, ++target_out)
  {
    *target_out = boost::lexical_cast<Target>(*source_in);
  }
  return v_new;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict