I use a function from another library which takes any STL sequential container as an argument. Just for arguments sake, I'll keep it as std::vector
.
do_something (std::vector<double>& k)
{
...
}
I have a whole vector
of data (happens to be of type double
) Is there a way to pass a sub vector preferably via reference and without copying and splitting vector before hand , to this function? It is straight forward via copying (by simply making a copy of subvector , returning the vector
from function and putting the respective subvector back into vector
). why?:
In other words, can I do something like this (in pseudocode exmaple below)?
std::vector<double> myVector{1.2, 5.6, 8.6, 2.2, 1.7, 9.1}; // myVeector of size 6
do_something (&myVector[ -- elementss 2,3,4--]);
Any help appreciated, even if it is not possible and if you know a way to copy back and forth efficiently
Note: If it's any help to SO programmers, my vector will be the same size with the same prelocated subvector which is not equal to from myVector.begin()
or myVector.back()
. Also this will never change for that one vector
No, it can't be done. That's why standard algorithms accept two (or more) iterators as parameters, rather than a container.
You could also look at the Boost.Range library, which allows for range adaptors including slices.
Depending what's in this function template that you call, you might be able to pass a sliced range in place of the sequence, since the concrete ranges defined in Boost do have begin()
and end()
member functions. But since it takes a non-const reference to a container, there's a chance that it adds or removes elements, in which case you're out of luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.