是否有一个类似STL的函数来用索引的某个函数填充数组
Is there an STL-like function to fill an array with some function of the index?
在C++中,我们有像std::fill
或std::fill_n
这样的函数,它们是用值填充指针数组、向量、std::array
和其他容器的方便的单行方式。一些容器也有自己的fill
方法,以允许使用恒定值填充。还有函数std::generate{_n}
和std::iota
,前者允许使用生成器函数填充元素,后者用索引填充范围。
我正在寻找一个类似的解决方案——最好是一个在标准库中定义的单行解决方案——它允许用索引的一些函数填充容器。例如,这将是一个阵列的解决方案:
std::array<int, 100> arr;
for (std::size_t i = 0; i < 100; i++)
arr[i] = f(i);
其中CCD_ 7是索引的某个函数。
有没有一种综合的方法来做到这一点?
您可以使用有状态的lambda:
std::array<int, 100> arr;
std::generate(arr.begin(), arr.end(), [i = std::size_t(0)]() mutable {return f(i++);});
但我认为这会使代码变得比需要的更复杂。使用纯循环可能是最好的选择。
我无法在编译器中尝试这一点,因为我找不到一个具有C++20范围的编译器,但一旦编译器实现了它们,类似的东西就会起作用:
std::ranges::transform(std::views::iota{1, arr.size()+1}, arr, f);
或者,如果你只想要生成的值的序列,只需要用创建一个范围
auto const values = std::views::iota{1, arr.size()+1} | std::views::transform(f);
在拥有C++20编译器之前,您可以使用其中一个范围库,例如在Boost或Eric Nieblers ranges-v3 中
考虑编写自己的STL类函数模板apply_idx_func()
:
template<typename FwdItor, typename F>
void apply_idx_func(FwdItor first, FwdItor last, F f) {
for (size_t idx = 0; first != last; ++first, ++idx)
*first = f(idx);
}
例如:
auto main() -> int {
std::array<int, 10> arr;
// just adds 100 to the index
auto func = [](size_t idx) -> int {
return 100 + idx;
};
apply_idx_func(std::begin(arr), std::end(arr), func);
for (auto elem: arr)
std::cout << elem << ' ';
std::cout << 'n';
}
输出为:
100 101 102 103 104 105 106 107 108 109
如果你总是遍历整个数组,你甚至不必使用任何stl函数,只需使用范围循环:
std::array<int, 100> arr;
int idx = 0;
for (auto& item : array)
item = f(idx++);
另一种选择是使用for_each
(如果将来您想仅部分填充阵列,这可能更有用(
#include <algorithm>
std::array<int, 100> arr;
int idx = 0;
std::for_each(begin(arr), end(arr), [&idx](int &n){ n = f(idx++); });
不幸的是,在这两种情况下,您都必须有单独的索引变量(此处:idx
(
#include <algorithm>
size_t i = 0;
std::generate_n(arr.begin(),arr.size(),[&i](){return f(i++);});
或者可能分两步:
#include <numeric>
std::iota(arr.begin(),arr.end(),0);
std::transform(arr.begin(),arr.end(),f);
在助推的帮助下,我们可以在一个步骤中做到这一点:
std::transform(
arr.begin(),
arr.end(),
boost::irange(0,arr.size()),
[](const auto& val,const auto& index){return f(index);}
);
https://www.boost.org/doc/libs/1_64_0/libs/range/doc/html/range/reference/ranges/irange.html
- C++ 在函数内按索引更改数组
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 如何在 STL 函数中找到传递给谓词的元素的索引?
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- C++函数返回两个 char 数组的相同索引元素
- 如何使用 erase() 函数通过索引擦除矢量?
- 如何访问委托的paint()函数中的另一个索引?
- 使用此网格函数的立方体的正确顶点和索引是什么
- 是否有一个类似STL的函数来用索引的某个函数填充数组
- 为什么函数的任何索引处的下标运算符在C++中总是返回1
- 如何使用递归函数返回数组中整数的索引?
- CUDA 全局函数未正确添加某些索引的数组值
- 索引和访问 mex 函数中单元数组和矩阵的元素
- 为什么STD :: SPAN超载函数调用操作员索引
- 在成员函数之间传递const变量为数组的索引
- 如果我将索引变量更改为零,并且合并函数中的k = 0,则获得合并排序的错误结果
- 从给定索引返回子字符字符串的函数
- 使用函数从数组中索引最小值的问题
- 如何从函数返回索引?
- 如何使用函数参数中的int指针来获取2d数组中最大值的索引并返回指针