通过std :: for_each中的向量位置
我有一个稀疏压缩列格式的数据结构。
对于我给定的算法,我需要迭代数据“列”中的所有值并执行一系列操作。目前,它使用常规 for 循环运行良好。老板希望我将其重新编码为 for_each 循环,以便将来并行化。
对于那些不熟悉稀疏压缩列的人来说,它使用 2(或 3)个向量来表示数据。一个向量只是一长串值。第二个向量是每列开始位置的索引。
当前版本 // 用于处理第5列中的数据 向量值; 向量列索引; 向量行索引;
int column = 5;
for(int i = conIndex[5]; i != colIndex[6]; i++){
value = values[i];
row = rowIndex[i];
// do stuff
}
关键是我需要知道值列中的位置(作为整数),以便查找行位置(还有一些我不想在此处列出的其他内容。)
如果我使用 std::for_each() 函数,我会得到该位置的值,而不是该位置。我需要这个职位本身。
一种想法是创建一个与我的数据长度相同的整数向量,但显然效率不高。这样,我可以将该虚拟向量上的迭代器传递给 for_each 中的函数,并且传递给我的函数的值将是位置。然而,这似乎是效率最低的方法。
有什么想法吗?
我的挑战是我需要知道向量中的位置。 for_each 接受一个迭代器并将该迭代器的值发送给函数。
I have a data structure in sparse compressed column format.
For my given algorithm, I need to iterate over all the values in a "column" of data and do a bunch of stuff. Currently, it is working nicely using a regular for loop. The boss wants me to re-code this as a for_each loop for future parallelization.
For those not familiar with sparse compressed column, it use 2 (or 3) vectors to represent the data. One vector is just a long list of values. The second vector is the index of where each column starts.
The current version
// for processing data in column 5
vector values;
vector colIndex;
vector rowIndex;
int column = 5;
for(int i = conIndex[5]; i != colIndex[6]; i++){
value = values[i];
row = rowIndex[i];
// do stuff
}
The key is that I need to know the location(as an integer) in my values column in order to lookup the row position (And a bunch of other stuff I'm not bothering to list here.)
If I use the std::for_each() function, I get the value at the position, not the position. I need the position itself.
One thought, and clearly not efficient, would be to create a vector of integers the same length as my data. That way, I could pass an iterator over that dummy vector to the function in for_each and the value passed to my function would be the postion. However, this seems like the least efficient way.
Any thoughts?
My challenge is that I need to know the position in the vector. for_each takes an iterator and sends the value of that iterator to the function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
boost::counting_iterator
,或实现您自己的。Use
boost::counting_iterator<int>
, or implement your own.@nm的答案可能是最好的,但是只有标准库提供的内容,尽管我想痛苦慢:
而且写了这一点,我确实只能推荐Boost
Counting_iterator
版本。 )@n.m.'s answer is probably the best, but it is possible with only what the standard library provides, though painfully slow I assume:
And after writing that, I really can only recommend the Boost
counting_iterator
version. ;)