CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2001
    Posts
    588

    Getting an array from a std::vector's contents

    I'm writing a program now in which I need to keep an array of HANDLE objects, to pass to a Win32 API function that I'm using. The HANDLE's refer to threads that I'm creating in a for loop. Having paid much attention to the comments of posters here, I'm placing each one into a std::vector container as I create it. Is there any good way to get the contents of a vector as an array?

    If I remember my introductory C++ class correctly, we talked about <vector>, and I think it held the data internally in an array of increasing size as was needed. If this is correct, could I use vector::front() to get the first element, and vector::size() to get the size of the "array?" It sounds like it will work for me, but I'm not sure that this is good practice. Is this a case where I should just dynamically allocate an array instead of using std::vector? Thanks for any input on this.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    If you want to pass the vector as an array, just take the address of the 0'th element:
    Code:
    #incude <vector>
    
    void legacyfunction(int arr[], int num);
    
    int main()
    {
        std::vector<int> vi;
    
        // fill vi
    
        legacyfunction(&vi[0], vi.size());
    }
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Getting an array from a std::vector's contents

    Originally posted by Bob Davis
    ...If I remember my introductory C++ class correctly, we talked about <vector>, and I think it held the data internally in an array of increasing size as was needed. If this is correct, could I use vector::front() to get the first element, and vector::size() to get the size of the "array?" It sounds like it will work for me, but I'm not sure that this is good practice. Is this a case where I should just dynamically allocate an array instead of using std::vector? Thanks for any input on this.
    The vector is guaranteed to store its data in contiguous memory. The solution that Graham gave is correct. You can even pass a vector to a function that modifies the array, given that the vector has enough room already allocated.

    To read up more on this, you can get Scott Meyers "Effective STL". There is a whole section on this topic alone (passing vectors to legacy functions).

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    Just want to add one more thing, which i notice a lots of ppls do wrong when using vector first time.

    Be sure to check vector is not empty before using index operator.

    Code:
    	vector<int> a;
    
    	// do something
    
    	if (!a.empty())
    		fun(&a[0], a.size());
    Personaly i like to use at() function to get the value, because this function throw exception when index is out of range and index operator doesnt.

    Code:
    	try
    	{
    		fun(&a.at(0), a.size());
    	}
    	catch(exception& e)
    	{
    		cout << e.what() << endl;
    	}
    Hope it helps.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Good point and worth making.

    Not sure I agree about using at(), but that's just an opinion - if it works for you, and it's maintainable, what the hey?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


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