Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a vector with values 0 to n?

Tags:

rust

How do I initialize a vector from 0 to n in Rust? Is there another way of doing that than creating an empty vector and invoking push inside a loop?

I prefer a one-liner.

like image 549
Thorkil Værge Avatar asked Dec 16 '21 22:12

Thorkil Værge


People also ask

How do you initialize a vector with 0?

A vector can be initialized with all zeros in three principal ways: A) use the initializer_list, B) use the assign() vector member function to an empty vector (or push_back()), or C) use int or float as the template parameter specialization for a vector of initially only default values.

How do I make a vector with all elements 0 in C++?

You can use: std::vector<int> v(100); // 100 is the number of elements. // The elements are initialized with zero values.

How to initialize a vector with a sequential range 1 to N?

This post will discuss how to initialize a vector with a sequential range 1 to n in C++. 1. Using std::iota The std::iota function assigns consecutive values to every element in the specified range. It can be used as follows to fill a vector with successive values starting from 1 till n.

How do you initialize a vector in C++?

Initialize a vector in C++ (5 different ways) Following are different ways to create and initialize a vector in C++ STL. Initializing by one by one pushing values : // CPP program to create an empty vector. // and one by one push values. #include <bits/stdc++.h>. using namespace std;

How to initialize all the values in an array to 0?

In an array we can do int arr [100]= {0} ,this initializes all the values in the array to 0. I was trying the same with vector like vector <int> v (100)= {0} ,but its giving the error error: expected ‘,’ or ‘;’ before ‘=’ token.

How to fill a vector with successive values starting from 1?

Using std::iota The std::iota function assigns consecutive values to every element in the specified range. It can be used as follows to fill a vector with successive values starting from 1 till n.


2 Answers

A range can be collected into a vector:

pub fn sequence(n: u32) -> Vec<u32> {
    (0..n).collect()
}

Playground

like image 200
John Kugelman Avatar answered Oct 07 '22 22:10

John Kugelman


Here is how you can do it as a one-liner:

let n = 4;

let v: Vec<i32> = (0..n).collect();  // the last element will be n-1
assert_eq!(v, vec![0, 1, 2, 3]);

let v: Vec<i32> = (0..=n).collect();  // the last element will be n
assert_eq!(v, vec![0, 1, 2, 3, 4]);

Or, alternatively:

let v: Vec<i32> = Vec::from_iter(0..n);  // the last element will be n-1
assert_eq!(v, vec![0, 1, 2, 3]);

let v: Vec<i32> = Vec::from_iter(0..=n);  // the last element will be n
assert_eq!(v, vec![0, 1, 2, 3, 4]);

Instead of i32 we could use other numeric types like u8, u16, i8, etc. That's because both collect() and Vec::from_iter are generic methods.

All those solutions make use of the Range or RangeInclusive structs respectively, both of which implement Iterator. That allows them to easily be converted into a Vec, which is most often done via the collect() method.

like image 41
at54321 Avatar answered Oct 07 '22 23:10

at54321