gpt4 book ai didi

C++分区问题的优雅解决方案

转载 作者:太空狗 更新时间:2023-10-29 21:08:18 27 4
gpt4 key购买 nike

我希望看到最优雅的 STL 类分区算法扩展标准文件:

given a vector of ints, partition the vector so that the positive integers appear
to the front of the negative integers
AND
return a map<int, int> where map[i]=j means that integer at index i is now at j.

显然第一部分(没有第二个要求)是这样的

partititon(vec.begin(), vec.end(), IsEven)

如果不实际重新实现分区并构建沿途 map 。

最佳答案

将您的整数 vector 复制到类似以下内容的 vector 中:

struct ValIdx
{
int val;
size_t idx;
};

使用适当的仿函数进行分区,然后迭代结果,将整数复制回来并构建您的 map 。

关于C++分区问题的优雅解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321997/

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