Vector as array

Discussion in 'C++' started by Alex Vinokur, Sep 21, 2008.

  1. Alex Vinokur

    Alex Vinokur Guest

    Let v be an vector; for instance:
    std::vector<double> v;

    Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

    In other words,
    Let foo() have the following prototype:
    void (double* p, std::size_t arrarySize);

    Is this calling is correct:
    foo (&v[0], v.size()) ?

    Alex Vinokur
     
    Alex Vinokur, Sep 21, 2008
    #1
    1. Advertising

  2. Alex Vinokur

    Kai-Uwe Bux Guest

    Alex Vinokur wrote:

    > Let v be an vector; for instance:
    > std::vector<double> v;
    >
    > Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
    >
    > In other words,
    > Let foo() have the following prototype:
    > void (double* p, std::size_t arrarySize);
    >
    > Is this calling is correct:
    > foo (&v[0], v.size()) ?


    I think that is what the standard means by calling the memory of a
    vector "contiguous". The precise guarantee is for types other that bool
    [23.2.4/1]:

    ... The elements of a vector are stored contiguously, meaning that if v is
    a vector<T, Allocator> where T is some type other than bool, then it obeys
    the identity &v[n] == &v[0] + n for all 0 <= n < v.size().


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 21, 2008
    #2
    1. Advertising

  3. Alex Vinokur

    Rolf Magnus Guest

    Alex Vinokur wrote:

    > Let v be an vector; for instance:
    > std::vector<double> v;
    >
    > Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?


    Yes.

    > In other words,
    > Let foo() have the following prototype:
    > void (double* p, std::size_t arrarySize);
    >
    > Is this calling is correct:
    > foo (&v[0], v.size()) ?


    Yes.
     
    Rolf Magnus, Sep 21, 2008
    #3
    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. pmatos
    Replies:
    6
    Views:
    23,934
  2. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    328
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    374
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page