[英]Using boost::counting_iterator with an existing vector?
At the moment I am doing this:目前我正在这样做:
const int n = 13;
std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(n + 1));
std::copy(v.begin(), v.end(), back_inserter(m_vecAssignmentIndex));
m_vecAssignmentIndex
is defined liek this: m_vecAssignmentIndex
定义如下:
ByteVector m_vecAssignmentIndex;
And, ByteVector
:并且,
ByteVector
:
using ByteVector = std::vector<BYTE>;
Is it possible to assign directly to m_vecAssignmentIndex
and avoid std::copy
?是否可以直接分配给
m_vecAssignmentIndex
并避免std::copy
?
So, code like this is OK:所以,这样的代码是可以的:
std::vector<BYTE> v2(boost::counting_iterator<BYTE>(0), boost::counting_iterator<BYTE>(n + 1));
std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(n + 1));
std::copy(v.begin(), v.end(), back_inserter(m_vecAssignmentSortedIndex));
Thus, I can directly increment BYTE
values.因此,我可以直接增加
BYTE
值。 So how can I avoid the requirement for the temp vector?那么如何避免对临时向量的要求呢?
I have now found the samples in the official docs:我现在在官方文档中找到了示例:
int N = 7;
std::vector<int> numbers;
typedef std::vector<int>::iterator n_iter;
std::copy(boost::counting_iterator<int>(0),
boost::counting_iterator<int>(N),
std::back_inserter(numbers));
std::vector<std::vector<int>::iterator> pointers;
std::copy(boost::make_counting_iterator(numbers.begin()),
boost::make_counting_iterator(numbers.end()),
std::back_inserter(pointers));
std::cout << "indirectly printing out the numbers from 0 to "
<< N << std::endl;
std::copy(boost::make_indirect_iterator(pointers.begin()),
boost::make_indirect_iterator(pointers.end()),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
So:所以:
std::copy(boost::counting_iterator<BYTE>(0),
boost::counting_iterator<BYTE>(n), std::back_inserter(m_vecAssignmentSortedIndex));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.