Is there an equivalent function in C / C ++ for GNU-R that is ()?

Let me explain what the “which” function does:

From GNU-R Help:

What indexes are TRUE?

Give "TRUE indices of a logical entity that allow array indices."

or showing some code: (GNU-R starts index counting from 1)

> x <- c(1,2,3,1,3,5);
> which(x == 1);
[1] 1 4
> which(x == 3);
[1] 3 5
> ll <- c(TRUE,FALSE,TRUE,NA,FALSE,FALSE,TRUE);
> which(ll);
[1] 1 3 7

Does anyone know a similar function in C / C ++?

thanks for the help

rinni

+5
source share
3 answers

You have to understand that R vectorized, while C works primarily with individual data atomistic particles: one int, double...

With C ++, you can learn the STL algorithms you are suitable with.

, R ++ Rcpp ++, ; . Rcpp-sugar pdf vignette (/ Rcpp).

+6

-, , , std::for_each. , :

vector<int> values;
//fill your vector with values;

struct match_functor
{
    vector<int> value_array;
    int match_value;

    match_functor(int value): match_value(value) {}

    void operator() (int input_value)
    {
        if(match_value == input_value)
            value_array.push_back(input_value);
    }
};

match_functor matches(1);
std::for_each(values.begin(), values.end(), matches);

matches.value_array[INDEX].

, , , - -:

struct match_functor
{
    vector<int> index_array;
    int match_value;
    int index;

    match_functor(int value): match_value(value), index(0) {}

    void operator() (int input_value)
    {
        if(match_value == input_value)
            index_array.push_back(index);

        index++;
    }
};

match_functor matches(1);
matches = std::for_each(values.begin(), values.end(), matches);

matches.index_array[INDEX] orignal, 1, .

+3

the algorithm std::find_ifshould do the trick - combined with the loop I have to add.

+2
source

All Articles