cast of vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkborregaard
    New Member
    • Sep 2007
    • 20

    cast of vectors

    Is there an easy way to cast my vector<int> to vector<double>, without generating a new vector and copying and typecasting each element from one to the other?
    I have a class constructor that takes a bunch of vector<doubles> , and it is impractical to template the entire class...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It's that cast word again. Wash your mouth out with soap.

    You cast in C++ usually a) you are calling relic C funcitons with void* arguments or b) you don't know what you are doing.

    Any cast involves making a copy.

    In your case, just use the transform() algorithm and convert your vector<int> ro vector<double>. You can even pass in the address of a function to to the conversion for you.

    The C++ compiler can convert an int to a double but cannot do this in reverse.

    Comment

    • mkborregaard
      New Member
      • Sep 2007
      • 20

      #3
      I stand corrected :)
      Could you elaborate on what is wrong with the word 'cast'?
      I am using a matrix calculation library from the web, which only accepts matrices of type double, to implement some statistics. However, all my statistics are performed on integer numbers, as my data are counts - I believe it should cause no problems, since int-to-double conversions go painlessly, and the return types are supposed to be doubles anyway.
      If what I am doing is stupid in some way, I would really appreciate having pointed out how, so I can correct my approach.
      Thanks for the pointer to the transform() algorithm, it will help me along. I might write a templated wrapper class, that transform()'s all vectors to vector<double> and then calls the constructor?

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You could also use doubles for your input values, as long as you remember to add the .0 at the end or tell users to. Doubles work just as well with ++ and similar operators as ints.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Could you elaborate on what is wrong with the word 'cast'?
          There is a lot of history with casting in C. Like C macros, casting has gotten a bad named due to years of overuse and misuse. In most cases, it deserves the bad name it earned.

          As for your vector of ints and doubles, they are two different things that can't be substituted for each other. The underlying structure in the STL vector is an array of contiguous memory. Would you try to pass an array of ints (4 bytes each) as an array of doubles (8 bytes each) and not expect a lot of transformations ?

          The transformations that the compiler does between ints and doubles are called implicit conversions. Unfortunately, the compiler will only do that for single values, not for an array of values.

          So now you are back to the transform that W4cats mentioned.

          Comment

          • mkborregaard
            New Member
            • Sep 2007
            • 20

            #6
            Thanks!
            I did a templated wrapper class, that uses the transform() algorithm - it is a bit heavy, but it works. It is very interesting for me to hear about the reputation of casts, since I am (of course) interested in improving my coding style.

            Comment

            Working...