How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
2015-01-16 11:31
1241 查看
I have an array of values that is passed to my function from a different part of the program that I need to store for later processing. Since I don't know how many times my function will be called before it is time to process the data, I need a dynamic storage structure, so I chose a std::vector. I don't want to have to do the standard loop to push_back all the values individually, it would be nice if I could just copy it all using something similar to memcpy.
There have been many answers here and just about all of them will get the job done.
However there is some misleading advice!
Here are the options:
[/code]
Here are 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's a loop (implied with the copy function), each element must be treated individually; no performance improvements can be made based on the fact that 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 adding it. For large arrays this might help. However the best advice here is never to use reserve unless profiling suggests you may be able to get an improvement (or you need to ensure your iterators are 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 comprehensively explain why it was 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 is required to be called since memcpy works outside the bounds of vector and there is no way to tell a vector that its size has changed. Apart from being an ugly solution (byte copying!) remember that this can only be used for POD types. I would never use this solution.
Method 4 is the best way to go. It's meaning is clear, it's (usually) the fastest and it works for any objects. There is no downside to using this method for this application.
Method 5 is a tweak on Method 4 - copy the array into a vector and then append it. Good option - generally fast-ish and clear.
Finally, you are aware that you can use vectors in place of arrays, right? Even when a function expects c-style arrays you can use vectors:
There have been many answers here and just about all of them will get the job done.
However there is some misleading advice!
Here are the options:
vector<int> dataVec; int dataArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; unsigned dataArraySize = sizeof(dataArray) / 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()); }
[/code]
Here are 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's a loop (implied with the copy function), each element must be treated individually; no performance improvements can be made based on the fact that 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 adding it. For large arrays this might help. However the best advice here is never to use reserve unless profiling suggests you may be able to get an improvement (or you need to ensure your iterators are 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 comprehensively explain why it was 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 is required to be called since memcpy works outside the bounds of vector and there is no way to tell a vector that its size has changed. Apart from being an ugly solution (byte copying!) remember that this can only be used for POD types. I would never use this solution.
Method 4 is the best way to go. It's meaning is clear, it's (usually) the fastest and it works for any objects. There is no downside to using this method for this application.
Method 5 is a tweak on Method 4 - copy the array into a vector and then append it. Good option - generally fast-ish and clear.
Finally, you are aware that you can use vectors in place of arrays, right? Even when a function expects c-style arrays you can use vectors:
vector<char> v(50); // Ensure there's enough space strcpy(&v[0], "prefer vectors to c arrays");
相关文章推荐
- How to copy the contents of std::vector to c-style static array,safely?
- How Would You Get the Count of an Array in C++?
- How Would You Get the Count of an Array in C++?
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- stackover flow 上的一个问题:Find the most common entry in an array
- How do I sort an array of hashes by a value in the hash?
- How to find size of array in C/C++ without using sizeof ?
- How do I sort an array of hashes by a value in the hash?
- From a response document, how do you update a field in the parent document?
- It is said that wars in the 21st century will be fought over water. Do you agree? What do you think can be done now to alleviate
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- How to Copy an Assembly From the GAC to the File System
- How do you remove the duplicate characters in a given string without using any additional buffer.
- 【摘录】How do you change the drawing order of views in Android?
- (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
- FAQ How do I write to the console from a plug-in?
- How to get IOleSite interface of the WebBrowser in an ActiveX control
- How do I bind the values of an enum to a ComboBox?
- How can I throw an exception without losing the original stack trace information in .NET?