DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Noiroi Guest

    iterator use in functions...


    I have the following line:
    vector<int>::iterator p = varrInts.begin();

    I would like to pass p as a parameter to a function. Since this is an iterator,
    I'm unsure how to do that. Can someone shed some light on this?

    Noiroi

  2. #2
    Noiroi Guest

    Re: iterator use in functions...


    Ok, I've been thinking about this... would it be better to do something like:

    int MyFunction( vector<int> vec )
    {
    /*... some manipulation ...*/
    return something;
    }

    or better to pass a pointer to the vec iterator created?
    vector<int>::iterator p = varrInts.begin();

    int MyFunction( int* vec )
    {
    /*... some manipulation ...*/
    return something;
    }

    I'm thinking that iterators are basically abstract pointers to sometype,
    so since the vector is defined as an int, then I'd use the above format.
    Can anyone verify this?


    "Noiroi" <noiroi@noiroi.com> wrote:
    >
    >I have the following line:
    >vector<int>::iterator p = varrInts.begin();
    >
    >I would like to pass p as a parameter to a function. Since this is an iterator,
    >I'm unsure how to do that. Can someone shed some light on this?
    >
    >Noiroi



  3. #3
    Danny Kalev Guest

    Re: iterator use in functions...



    Noiroi wrote:
    >
    > Ok, I've been thinking about this... would it be better to do something like:
    >
    > int MyFunction( vector<int> vec )


    this is really a bad idea. You're passing a whole container by value,
    which means that when called, the function gets a copy of the original
    container and that copy is discarded when the function returns. You
    certainly don't want to that.
    > {
    > /*... some manipulation ...*/
    > return something;
    > }
    >
    > or better to pass a pointer to the vec iterator created?
    > vector<int>::iterator p = varrInts.begin();


    It's simple. You already know how to declare an iterator. To pass it as
    a parameter, use the same declaration in the function's prototype:

    int MyFunction(vector<int>::iterator p)
    {
    ..
    }

    Danny

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links