gpt4 book ai didi

c++ - 查找不在索引 vector 中的索引

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:54 24 4
gpt4 key购买 nike

如何查找不在数组索引中的索引?例如。如果索引 vector 是 (2, 8, 6, 9)。那么结果应该是(1,3,4,5,7)。 R 有一个函数 (not) %in% 来做这个。一种天真的方法是创建一个标志数组。但是在非索引上创建标志和迭代将是两个不同的循环。有没有办法在一个循环中做到这一点?

最佳答案

这应该有效:

int j = 0;
for(int i = 0;; ++i) {
if(oldvec[j] == i) {
j++;
if(j >= oldvec.length())
break;
} else {
newvec.push_back[i];
}
}

新答案:

  std::set<int> result;
int max = -1;
for(unsigned int i=0; i<oldvec.size(); ++i)
{
int cur = oldvec[i];
while(max < cur) {
max++;
result.insert(max);
}
result.erase(cur);
}

怎么样? :)等等,结果必须是 std::vector 吗?

关于c++ - 查找不在索引 vector 中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19545722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com