I am looking for an efficient way to crop or copy a subset of an existing std :: vector. The criteria for elements suitable for a subset / remainder is that their index is contained in a separate, predefined std :: vector.
eg std::vector<String> Test = { "A", "B", "C", "D", "E"} std::vector<int> SelectionV = {1,2,5} Result = {"A", "B", "E"}
I will do this on a very large vector and probably on a regular basis, so I am looking for an efficient method as possible.
An alternative that I am also considering, but again unsure of an effective method ...
As the Test object fills up (in my case it is a third-party specific object), this is the result of one pass through the iterator (direct access to the element is impossible). I was wondering if instead you can add only elements of the Test vector that appear in the counter defined in SelectionV
eg
int count = 0 for (Iterator.begin, Iterator.end(), Iterator++) { if (count is a number contained in selectionV) add to Test }
but I assume that this will lead to going through selection V at each iteration, which would be much less efficient than just adding all the elements and then selecting the subset.
Any help is greatly appreciated.
source share