and why doesnt the standard vector have such conversion available? 10 15604
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... and why doesnt the standard vector have such conversion available?
You can use the vector constructor that takes two iterators:
#include <vector>
#include <assert.h>
using namespace std;
int main()
{
vector<float> a;
a.push_back(1.2f);
vector<double> b(a.begin(), a.end());
assert(!b.empty());
cout << b.front() << '\n';
}
Ali
> Re: how do u convert a vector<float> A to a vector<double> B
std::vector<float> A;
/* etc */
std::vector<double> B(A.begin(), A.end())
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... and why doesnt the standard vector have such conversion available?
It does. See above.
-Mike
What if b is already constructed?
Another question
Say you have
vector<float> a(3);
//init a
vector<std::complex<float> > c(3);
//init c
Now I want something like
real(c)=a;
Currently I have
for(int i=0;i<c.size();i++)
c[i].real()=a[i];
Which I dont really like.
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed?
Then it's assignment:
b = vector<double>(a.begin(), a.end());
Another question Say you have vector<float> a(3); //init a vector<std::complex<float> > c(3); //init c Now I want something like real(c)=a;
Currently I have for(int i=0;i<c.size();i++) c[i].real()=a[i];
Which I dont really like.
There is nothing wrong with it. Just wrap it in a function and you are done:
typedef vector<float> Reals;
typedef vector<complex<float> > Complexes;
void set_reals(Complexes & complexes, Reals const & reals)
{
// some checks
if (complexes.size() != reals.size())
{
throw SomeError;
}
/* the logic here */
}
Very neat :)
More cool (and possibly more obscure) things can probably be done, but I
really don't think that it's worth it.
Ali
"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:df**********@domitilla.aioe.org... <bl**********@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed?
Then it's assignment:
b = vector<double>(a.begin(), a.end());
Actually, a probably better expression (for clarity,
maintainability, and performance) is:
b.assign( a.begin(), a.end() );
hth, Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
On 2005-08-31 18:14:04 -0400, bl**********@gmail.com said: and why doesnt the standard vector have such conversion available?
#include <vector>
#include <algorithm> //for std::copy
....
std::vector<float> A = ...;
//Construct B with the contents of A
std::vector<double> B(A.begin(), A.end());
//Or if B is already constructed:
B.clear();
B.insert(B.begin(), A.begin(), A.end());
//Or:
B.resize(A.size());
std::copy(A.begin(), A.end(), B.begin());
--
Clark S. Cox, III cl*******@gmail.com
On 2005-09-01 00:09:55 -0400, "Ivan Vecerina"
<IN*************************@vecerina.com> said: "Ali Çehreli" <ac******@yahoo.com> wrote in message news:df**********@domitilla.aioe.org... <bl**********@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed?
Then it's assignment:
b = vector<double>(a.begin(), a.end());
Actually, a probably better expression (for clarity, maintainability, and performance) is:
b.assign( a.begin(), a.end() );
There is no "assign" function on std::vector.
--
Clark S. Cox, III cl*******@gmail.com
Clark S. Cox III wrote:
<snip> Actually, a probably better expression (for clarity, maintainability, and performance) is:
b.assign( a.begin(), a.end() );
There is no "assign" function on std::vector.
23.2.4.1 vectors constructors, copy, and assignment
<snip>
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
Effects:
erase(begin(), end());
insert(begin(), first, last);
template <class Size, class U> void assign(Size n, const U& u = U());
Effects:
erase(begin(), end());
insert(begin(), n, t);
<snip>
Maybe your compiler lacks member template support?
Marc
Clark S. Cox III wrote:
<snip> //Or: B.resize(A.size()); std::copy(A.begin(), A.end(), B.begin());
b.clear();
b.reserve( a.size() );
std::copy( a.begin(), a.end(), std::back_inserter( b ) );
Marc
On 2005-09-01 08:52:49 -0400, Marc Mutz
<ma**@klaralvdalens-datakonsult.se> said: Clark S. Cox III wrote: <snip> Actually, a probably better expression (for clarity, maintainability, and performance) is:
b.assign( a.begin(), a.end() );
There is no "assign" function on std::vector.
23.2.4.1 vectors constructors, copy, and assignment <snip> template <class InputIterator> void assign(InputIterator first, InputIterator last);
Effects: erase(begin(), end()); insert(begin(), first, last);
template <class Size, class U> void assign(Size n, const U& u = U());
Effects: erase(begin(), end()); insert(begin(), n, t); <snip>
Maybe your compiler lacks member template support?
Well, I'll be. I can't believe I missed that.
I think I was looking at the first listing in 23.2.4 without
remembering the "Descriptions are provided here only for operations on
vector that are not described in one of these tables or for operations
where there is additional semantic information." part.
--
Clark S. Cox, III cl*******@gmail.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Pepijn Kenter |
last post by:
Dear experts.
I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:
#include <vector>
#include <iostream>
using namespace std;
int main() {
|
by: Active8 |
last post by:
I put the bare essentials in a console app.
http://home.earthlink.net/~mcolasono/tmp/degub.zip
Opening output.fft and loading it into a vector<float> screws
up, but input1.dat doesn't. It does...
|
by: Amit |
last post by:
Hi,
I was wondering how to define the + and = operator for a vector of
double or float and do I need to define it explicitly?
does it not get defined automatically(like copy constructor) if one...
|
by: keith bannister via .NET 247 |
last post by:
(Type your message here)
--------------------------------
From: keith bannister
Hi,
I'm new to .net (as of last week) but here goes.
I want to serialize/deserialize a file the conforms...
|
by: richard_lavoie |
last post by:
Hi,
I have something like this:
vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
I always become a
error: syntax error before `>'...
| |
by: perroe |
last post by:
Hi
I have a array of complex numbers that are stored in a simple double
array. This is done since the array is part of an wrapper for an
external C library, and the imaginary part of the first...
|
by: T. Crane |
last post by:
Hi,
I'm struggling with how to initialize a vector<vector<double>>
object. I'm pulling data out of a file and storing it in the
vector<vector<double>object. Because any given file will have a...
|
by: mathieu |
last post by:
Hi there,
I am dealing with the following problem:
I need to convert a std::vector<T(where T can be any interget
type: char, short, ushort) by applying a linear transform (a,b),
following;
...
|
by: matheussousuke |
last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails.
The goal is: Make it send the email with the URL...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |