简体   繁体   中英

How to create a vector from 1:n in C++ (Armadillo)?

Such a simple question but I haven't found an answer in the armadillo's documentation.

I'm looking for the Armadillo/C++ equivalent to Matlab's x = (1:n) where n is a number and x is thus a vector [1, 2, 3..., n-1, n] .

Please, pay attention to this function.

vec v = linspace<vec>(1, N);

Generates a vector starting at 1 and ending at N. It does just what you need.

Assuming that c++11 is acceptable and you are using std::vector , you can use std::iota :

std::vector<int> x(n);
std::iota(x.begin(), x.end(), 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM