使用std::vector的互补向量
Complementary vector using std::vector
我正在C++中编写一个现有的Matlab库。在Matlab中有一个波浪号运算符,~vec
是具有1的二进制矢量,其中vec
为零,其他地方为0。
更准确地说,我在Matlab 中有这些代码行
allDepthIdx = [1:nVec]';
goodIdx = allDepthIdx(~offVec);
goodValues = vec(~offVec);
我正在寻找一种有效的方法来找到索引goodIdx = allDepthIdx(~offVec);
。有一种方法,使用std::vector
,可以找到在1..nVec
中而不在offVec
中的索引列表吗?
我提出了这个解决方案,请随时发表评论或提出您的解决方案!
// First, I sort offVec
std::sort(offVec.begin(), offVec.end());
int k(0), idx(-1);
std::vector<real32_T> goodDepthIdx;
std::vector<real32_T> goodVal;
// For j in 1..nVec, I check if j is in offVec
for (int j = 0; j < nVec; j++)
{
k = 0;
idx = offVec.at(k);
// I go through offVec as long as element is strictly less than j
while (idx < j)
{
idx = offVec.at(k++);
}
if (idx != j) // idx not in offElemsVec
{
goodDepthIdx.push_back(j); // Build vector with indices
// in 1..nVec not in offVec
goodVal.push_back(vec.at(j)); // Build vector with values
// at these indices
}
}
相关文章:
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 我可以使用 std::vector::swap 来修改共享向量吗?
- 如何在C++中将字节向量(std::vector<uint8_t>)转换为不同的uint32_t,uint16_t和uint8_t变量
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- 'Vector.erase()' 不适用于类向量
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 向量索引变量声明(size_t 或 std::vector<DATATYPE>::size_type)
- 如何知道向量的实际最大大小?(不使用 std::vector::max_size)
- C++ 通过 std::transform 的 vector 索引获取向量
- std::vector::clear()ing 二维向量线程的内部向量安全吗?
- 如何在一对集合的向量中插入元素?vector<pair<int,set<string>>>
- 如何将 std::vector 的某些元素移动到向量中的新索引?
- 代码将向量存储为向量<vector>,为什么没有错误消息?
- 使用向量<vector>在 C++ 中表示图形
- 在展开过程中将向量成员推入向量:vector.push_back(向量[0])