CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2001
    Location
    Ridgecrest, CA.
    Posts
    41

    Question Passing a vector

    If I have a vector of structs in a method, example:


    Code:
    typedef struct {
        double x;
        double y;
        double z;
    } mystruct;
    
    vector<mystruct> myvector;
    
    ......
    myvector.push_back(...);
    ...
    printvector(???);

    How would I pass the vector to another method (by reference)?

    Code:
    void printvector(????){
    
    
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Resolved Re: Passing a vector

    Quote Originally Posted by amt35
    How would I pass the vector to another method (by reference)?
    Like this:
    Code:
    void printvector (const vector <mystruct> & vecMyStructures)
     {
       for (int nIndex = 0; nIndex < vecMyStructures.size (); ++nIndex)
     	  std::cout << vecMyStructures &#091;nIndex&#093;.x << std::endl;
     }
    Last edited by Siddhartha; July 13th, 2005 at 11:36 AM. Reason: improved...

  3. #3
    Join Date
    Sep 1999
    Posts
    137

    Re: Passing a vector

    See Effective STL by Scott Meyers. Item 16 talks about some problems to avoid.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Passing a vector

    Quote Originally Posted by amt35
    How would I pass the vector to another method (by reference)
    Yes, that would be a good idea, since it won't create a temporary object.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured