在C ++ / STL中有一个与Python range()等同的紧凑型 [英] Is there a compact equivalent to Python range() in C++/STL

查看:115
本文介绍了在C ++ / STL中有一个与Python range()等同的紧凑型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C ++ / STL等效于以下操作?我想用一个值[min,max]填充 std :: vector

How can I do the equivalent of the following using C++/STL? I want to fill a std::vector with a range of values [min, max).

# Python
>>> x = range(0, 10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

我想我可以使用 std :: generate_n 并提供一个函数来生成序列,但我想知道是否有一个更简洁的方法使用STL?

I suppose I could use std::generate_n and provide a functor to generate the sequence, but I was wondering if there is a more succinct way of doing this using STL?

推荐答案

在C ++ 11中,有 std :: iota

In C++11, there's std::iota:

std::vector<int> x(10);
std::iota(std::begin(x), std::end(x), 0); //0 is the starting number

这篇关于在C ++ / STL中有一个与Python range()等同的紧凑型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆