How do I copy the contents of an array to a std::vector in C + + without looping? (from stack over flow)

Source: Internet
Author: User

I have an array of values which is passed to my function from a different part of the program that I need to store for late R processing. Since I don ' t know how many times my function'll be called before it's time to process the data, I need a dynamic Stora GE structure, so I chose a std::vector. I don ' t want to any of the standard loops to push_back all the values individually, it would is nice if I could just co PY it all using the something similar to memcpy.

There has been many answers here and just on all of them would get the job done.

However there is some misleading advice!

Here is the options:

 vector<int> datavec;int dataarray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,};unsigned dataarraysize = sizeof (Dataar Ray)/sizeof (int);//Method 1:copy the array to the vector using Back_inserter. {copy (&dataarray[0], &dataarray[dataarraysize], Back_inserter (DATAVEC));} Method 2:same as 1 but pre-extend the vector by the size of the array using reserve{Datavec.reserve (Datavec.size () + dataarraysize); Copy (&dataarray[0], &dataarray[dataarraysize], Back_inserter (DATAVEC));} Method 3:memcpy{datavec.resize (datavec.size () + dataarraysize); memcpy (&datavec[datavec.size ()-dataarraysize], &dataarray[0], dataarraysize * sizeof (int)); Method 4:vector::insert{Datavec.insert (Datavec.end (), &dataarray[0], &dataarray[dataarraysize]);} Method 5:vector + vector{vector<int> dataVec2 (&dataarray[0], &dataarray[dataarraysize]); Datavec.insert (Datavec.end (), Datavec2.begin (), Datavec2.end ());} 


Here is some gory details:

Method 1 is probably the easiest to understand. Just copy each element from the array and push it into the back of the vector. Alas, it ' s slow. Because there ' a loop (implied with the copy function), each element must be treated individually; No performance improvements can be made based on the fact, we know the array and vectors are contiguous blocks.

Method 2 is a suggested performance improvement to Method 1; just pre-reserve the size of the array before add ing it. For large arrays this might . However the best advice here's never to use reserve unless profiling suggests we are able to get an improvement (or Y ou need to ensure your iterators is not going to be invalidated). Bjarne agrees. Incidentally, I found that this method performed the slowest most of the time though I ' m struggling to Comprehens Ively explain why it is regularly significantly slower than Method 1 ...

Method 3 is the old school Solution-throw some C at the problem! Works fine and fast for POD types. In this case resize are required to being called since memcpy works outside the bounds of vector and there is no-I Vector, its size has changed. Apart from being an ugly solution (byte copying!) Remember the This can is only a used for PODtypes. I would never use this solution.

Method 4 is the best go. It ' s meaning is clear, it's (usually) the fastest and it works for any objects. There is no downside to using the This method for this application.

Method 5 is a tweak on Method 4-copy the array to a vector and then append it. Good option-generally fast-ish and clear.

Finally, you are aware so can use vectors on place of the arrays, right? Even when a function expects C-style arrays you can use vectors:

vector<char> v(50); // Ensure there‘s enough spacestrcpy(&v[0], "prefer vectors to c arrays");

How do I copy the contents of an array to a std::vector in C + + without looping? (from stack over flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.