how do u convert a vector<float> A to a vector<double> B

Discussion in 'C++' started by bluekite2000@gmail.com, Aug 31, 2005.

  1. Guest

    and why doesnt the standard vector have such conversion available?
    , Aug 31, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Aug 31, 2005
    #2
    1. Advertising

  3. Mike Wahler Guest

    > 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())

    <> wrote in message
    news:...
    > and why doesnt the standard vector have such conversion available?


    It does. See above.

    -Mike
    Mike Wahler, Aug 31, 2005
    #3
  4. Guest

    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.real()=a;

    Which I dont really like.
    , Aug 31, 2005
    #4
  5. <> wrote in message
    news:...
    > 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.real()=a;
    >
    > 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 1, 2005
    #5
  6. "Ali Çehreli" <> wrote in message
    news:df5dem$88c$...
    > <> wrote in message
    > news:...
    >> 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
    Ivan Vecerina, Sep 1, 2005
    #6
  7. On 2005-08-31 18:14:04 -0400, 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
    Clark S. Cox III, Sep 1, 2005
    #7
  8. On 2005-09-01 00:09:55 -0400, "Ivan Vecerina"
    <> said:

    > "Ali Çehreli" <> wrote in message
    > news:df5dem$88c$...
    >> <> wrote in message
    >> news:...
    >>> 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
    Clark S. Cox III, Sep 1, 2005
    #8
  9. Marc Mutz Guest

    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
    Marc Mutz, Sep 1, 2005
    #9
  10. Marc Mutz Guest

    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
    Marc Mutz, Sep 1, 2005
    #10
  11. On 2005-09-01 08:52:49 -0400, Marc Mutz
    <> 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
    Clark S. Cox III, Sep 1, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pepijn Kenter
    Replies:
    2
    Views:
    8,455
    Pepijn Kenter
    Aug 16, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,483
    Victor Bazarov
    Feb 17, 2005
  3. bd
    Replies:
    0
    Views:
    621
  4. Replies:
    9
    Views:
    1,314
    Nate Barney
    Nov 14, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,533
    James Kanze
    Oct 8, 2009
Loading...

Share This Page