returning vector

Discussion in 'C++' started by Richard, Oct 26, 2005.

  1. Richard

    Richard Guest

    what is the syntax for returning a vector?

    temp is a vector

    return temp; ?

    return temp<>; ?

    return temp<int>; ?

    I got a book that does not talk about how to do this. Any helps are
    appreciated.
     
    Richard, Oct 26, 2005
    #1
    1. Advertising

  2. Richard wrote:
    > what is the syntax for returning a vector?
    >
    > temp is a vector
    >
    > return temp; ?
    >
    > return temp<>; ?
    >
    > return temp<int>; ?
    >
    > I got a book that does not talk about how to do this. Any helps are
    > appreciated.
    >
    >



    vector<int> f()
    {
    vector<int> xxx;

    return xxx;
    }

    (rather an expensive operation if the vector is big however!)
     
    Gianni Mariani, Oct 26, 2005
    #2
    1. Advertising

  3. Richard

    mlimber Guest

    Richard wrote:
    > what is the syntax for returning a vector?
    >
    > temp is a vector
    >
    > return temp; ?
    >
    > return temp<>; ?
    >
    > return temp<int>; ?
    >
    > I got a book that does not talk about how to do this. Any helps are
    > appreciated.


    Presumably you mean something like this:

    vector<int> GetVec()
    {
    vector<int> v;
    // Do something here to fill the vector
    return v;
    }

    The problem is that this function returns by value, which means the
    vector will likely be copied in its entirety, which could get expensive
    unless your compiler can figure out that it doesn't need to copy it
    (but I wouldn't depend on that). Alternately, you could pass the vector
    in by reference:

    void GetVec2( vector<int>& v )
    {
    // You might need to check to see if v holds anthing on entry
    // (existing elements might be fine or might not be)

    // Do something here to fill/change the vector
    }

    Now, no copying is involved no matter what optimizations you use.

    There are some other techniques, too, but this latter one is probably
    what you want.

    Cheers! --M
     
    mlimber, Oct 26, 2005
    #3
  4. Richard wrote:
    > what is the syntax for returning a vector?


    Returning where? From where?

    > temp is a vector


    of what? What do you mean by "temp is a vector".

    > return temp; ?


    Most likely.

    > return temp<>; ?


    This is very likely a syntax error.

    > return temp<int>; ?


    No, but you may be getting close if 'temp' is a template-id.

    > I got a book that does not talk about how to do this.


    Get a different book.

    > Any helps are
    > appreciated.


    Read FAQ 5.8 while you're at it.

    V
     
    Victor Bazarov, Oct 26, 2005
    #4
  5. Richard

    Mirek Fidler Guest

    > vector<int> GetVec()
    > {
    > vector<int> v;
    > // Do something here to fill the vector
    > return v;
    > }
    >
    > The problem is that this function returns by value, which means the
    > vector will likely be copied in its entirety, which could get expensive
    > unless your compiler can figure out that it doesn't need to copy it
    > (but I wouldn't depend on that). Alternately, you could pass the vector
    > in by reference:


    Or wait for r-value references or use NTL now:

    http://upp.sourceforge.net/srcdoc$pick_$en-us.html

    Mirek
     
    Mirek Fidler, Oct 26, 2005
    #5
  6. Richard

    Kaz Kylheku Guest

    Gianni Mariani wrote:
    > vector<int> f()
    > {
    > vector<int> xxx;
    >
    > return xxx;
    > }
    >
    > (rather an expensive operation if the vector is big however!)


    Why is that? Couldn't the vector data could be shared by reference
    between the two copies of the vector, subject to copy-on-write?

    Unix-like operating systems have a fork() function which this for an
    entire address space.

    It's perfectly good style to return strings from functions in C++, and
    strings are essentially vectors of characters.
     
    Kaz Kylheku, Oct 26, 2005
    #6
  7. Richard

    Mirek Fidler Guest

    Kaz Kylheku wrote:
    > Gianni Mariani wrote:
    >
    >>vector<int> f()
    >>{
    >> vector<int> xxx;
    >>
    >> return xxx;
    >>}
    >>
    >>(rather an expensive operation if the vector is big however!)

    >
    >
    > Why is that? Couldn't the vector data could be shared by reference
    > between the two copies of the vector, subject to copy-on-write?


    It could not. operator[] overloading mechanism is not robust enough for
    that:

    Part of std::vector definition is that you are get non-const reference for

    xxx[index]

    now consider

    vector<int> a;
    vector<int> b;
    .....
    T& x = a[index];
    b = a;
    x = 123;

    - there is no way how to detect last assignment and perform copy ("on
    write"). You can to some degree solve this problem with operator[]
    returning some sort of proxy instead of reference, but such solution
    would render vector less usable...

    Mirek
     
    Mirek Fidler, Oct 26, 2005
    #7
  8. Richard wrote:
    > what is the syntax for returning a vector?
    >
    > temp is a vector
    >
    > return temp; ?
    >
    > return temp<>; ?
    >
    > return temp<int>; ?
    >
    > I got a book that does not talk about how to do this. Any helps are
    > appreciated.
    >
    >


    return temp of course.

    Your book most likely doesn't talk about because the expert who wrote it
    didn't realise that newbies often think things have to be different,
    i.e. there must be some special syntax for vectors of something. Temp is
    a variable, it matters not one bit that it is a vector, string, pointer,
    or anything else, to return the value contained in a variable is always
    the same syntax.

    john
     
    John Harrison, Oct 26, 2005
    #8
  9. Gianni Mariani wrote:
    > vector<int> f()
    > {
    > vector<int> xxx;
    > return xxx;
    > }
    > (rather an expensive operation if the vector is big however!)


    Only if your compiler does not do NRVO. The standard allows optimising the copy away.
    Unfortunately only a few compilers do this, and because of this in a general case I prefer

    template<class OutIt>
    void f(OutIt out)
    {
    // (for example std::copy(begin, end, out))
    }

    std::vector<int> v;
    f(std::back_inserter(v));

    This will always avoid the extra copy of the vector, no matter how good your compiler is.

    --

    Valentin Samko - http://www.valentinsamko.com
     
    Valentin Samko, Oct 29, 2005
    #9
    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,852
  2. Replies:
    11
    Views:
    670
    Christos Georgiou
    May 2, 2006
  3. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    368
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page