473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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...
Nov 1 '07 #1
5 1841
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 1 '07 #2
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?
Nov 1 '07 #3
Laharl
849 Expert 512MB
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.
Nov 1 '07 #4
RRick
463 Expert 256MB
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.
Nov 2 '07 #5
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.
Nov 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.