Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copy and Cast a List

Copy and Cast a List

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    Is there a standard vectorized way to copy a list of doubles to a list of int8 values? I would like to code the following:

    typedef __uint8 unsigned __int8;
    double *const srcList = new double [ARRAY_SIZE];
    __uint8 *const dstList = new __uint8[ARRAY_SIZE];
    for (size_t i = 0; i < ARRAY_SIZE; ++i)
    dstList[i] = static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));

    I would like to code this more like the following:

    __uint8 ToUint8(double const inVal) {
    return static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));
    }
    std::???(srcList, srcList + ARRAY_SIZE, dstList, &ToUint8);

    Is there some existing function that will make the above work, or do I need to just make the iterator myself? Thanks,

    Sounds like somebody's got a case of the Mondays -Jeff

    S P 2 Replies Last reply
    0
    • S Skippums

      Is there a standard vectorized way to copy a list of doubles to a list of int8 values? I would like to code the following:

      typedef __uint8 unsigned __int8;
      double *const srcList = new double [ARRAY_SIZE];
      __uint8 *const dstList = new __uint8[ARRAY_SIZE];
      for (size_t i = 0; i < ARRAY_SIZE; ++i)
      dstList[i] = static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));

      I would like to code this more like the following:

      __uint8 ToUint8(double const inVal) {
      return static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));
      }
      std::???(srcList, srcList + ARRAY_SIZE, dstList, &ToUint8);

      Is there some existing function that will make the above work, or do I need to just make the iterator myself? Thanks,

      Sounds like somebody's got a case of the Mondays -Jeff

      S Offline
      S Offline
      Sameerkumar Namdeo
      wrote on last edited by
      #2

      you or the build in functions have to parse the entire list. try stl's for_each

      My Blog.
      My codeproject Articles.

      1 Reply Last reply
      0
      • S Skippums

        Is there a standard vectorized way to copy a list of doubles to a list of int8 values? I would like to code the following:

        typedef __uint8 unsigned __int8;
        double *const srcList = new double [ARRAY_SIZE];
        __uint8 *const dstList = new __uint8[ARRAY_SIZE];
        for (size_t i = 0; i < ARRAY_SIZE; ++i)
        dstList[i] = static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));

        I would like to code this more like the following:

        __uint8 ToUint8(double const inVal) {
        return static_cast<__uint8>(max<double>(0, min<double>(255, srcList[i])));
        }
        std::???(srcList, srcList + ARRAY_SIZE, dstList, &ToUint8);

        Is there some existing function that will make the above work, or do I need to just make the iterator myself? Thanks,

        Sounds like somebody's got a case of the Mondays -Jeff

        P Offline
        P Offline
        Paul Michalik
        wrote on last edited by
        #3

        std::transform(src.begin(), src.end(),
        dst.begin(), // or std::back_inserter(dst)
        [] (double v)->(__uint8) {
        return (__uint8)v;
        });

        ... if you're on vc++10, define trivial std::unary_function converter otherwise. You could also try one of the selectors or projectors from <algorithm>, with the other overload of transform then. Cheers, Paul

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups