The code
#include <iostream>
#include <vector>
int main () { std::vector<int> N = {3e6, 4e6}; }
Gives an error
error: narrowing conversion of ‘3.0e+6’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]|||.
Whereas
#include <iostream>
#include <vector>
int main () {
std::vector<int> N = {3*10^6, 4*10^6};
for (int n: N ){ std::cout << n << std::endl; }
}
prints
24
46.
How to define this vector as an integer vector correctly?
If have found while writing this question that setting
int n1 = 3e6, n2 = 4e6;
vector<int> N = {n1, n2};
does work, but is there a better way? The type is still converted from double to int in declarations of n1 and n2.