iota is not a member of std

Hi,

I have these two functions. The first one runs fine. The second one I get two errors:
- iota is not a member of 'std' at the line beginning with "std::iota"
- and comparison between signed and unsigned integer expression in the "for" line

Can anyone kindly help?

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<algorithm>
#include<string>
#include<cmath>
#include <numeric>
#include <vector>
#include <iostream>

double CalculateVaR2(std::vector<double> x, std::string method){
  double alpha = 0.99;
  double alpha_tilde = 0.974234518211730;
  double out = 0.0;
  std::sort(x.begin(), x.end());
  if( method == "quantile"){
    int idx = std::ceil((1. - alpha)*x.size());
    out = x[idx];
  }else if( method == "ETL" ){
    int idx = std::ceil((1. - alpha_tilde)*x.size());
    for(int i=0; i<=idx; ++i){
      out += x[i];
    }
    out /= idx + 1;
  }
  return out;
}


double CalculateSVaR2(const std::vector<double>& x, int length_window = 250, std::string method = "quantile") {
  
  std::vector<int> s_index(x.size() - length_window + 1);
  std::iota(s_index.begin(), s_index.end(), length_window);
  std::vector<double> out_vec(s_index.size());
  
  for(int i=0; i<s_index.size(); ++i) {
    
    int s = s_index[i];
    std::vector<double> x_sub(x.begin() + s - length_window, x.begin() + s - 1);
    out_vec[i] = CalculateVaR2(x_sub, method);
  }
  
  double out_min = *std::min_element(out_vec.begin(), out_vec.end());
  
  return(out_min);}
std::iota was added in C++11. Is your compiler set to support C++11?


vector::size() does return an unsigned integer. Your loop counter is int, a signed integer. Comparison of the two usually gives a "warning", not "error" (unless compiler is told to handle warnings as errors).

You could write:
for ( size_t i=0; i < s_index.size(); ++i ) {
Are you compiling using a C++11 or higher compliant compiler?

The std::vector.size() returns a size_t not an int.

Thank you keskiverto. Both jib and keskiverto, I am running the code from Rcpp in R, it is possible C++11 is not supported. I tried to plug it unsuccessfuly. So I guess I need to initialize a vector of indices in another way
Last edited on
Rccp is not a compiler. It calls some external compiler. There must be a way to pass options to the compiler.
Thank you kesikeverto, I understand. I did try to plug the c++11 compiler in R, but I don't think it worked. I found a way around in the code. Thank you both for getting me on the right route!

1
2
3
4
5
6
7
8
9
10
   int n = x.size() - length_window + 1;
  std::vector<double> out_vec(n);
  int s = length_window;
  
  for(int i=0; i<n; ++i) {
    
    std::vector<double> x_sub(x.begin() + s - length_window, x.begin() + s - 1);
    out_vec[i] = CalculateVaR2(x_sub, method);
    ++s;
  }
Topic archived. No new replies allowed.