Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字符串字段中查找字符串的索引 我对一个非常基本的问题感到抱歉,我对Rcpp和C++的R.是新的。_C++_R_Rcpp_Armadillo - Fatal编程技术网

在字符串字段中查找字符串的索引 我对一个非常基本的问题感到抱歉,我对Rcpp和C++的R.是新的。

在字符串字段中查找字符串的索引 我对一个非常基本的问题感到抱歉,我对Rcpp和C++的R.是新的。,c++,r,rcpp,armadillo,C++,R,Rcpp,Armadillo,我有一个字段(arma::field),我已经初始化它来保存字符串(arma::field my_vector)。我还有一个字符串std::string id,它位于字符串字段中的某个位置,我希望找到它所在的位置。我习惯于用向量和数字来做这件事,如下所示: arma::vec fun(arma::vec输入向量){ //查找向量等于5的位置(例如) uvec指数=arma::find(输入向量=5); 收益指数; } 我最初尝试做完全相同的事情,但给出了一个字符串而不是数字进行比较: arma

我有一个字段(
arma::field
),我已经初始化它来保存字符串(
arma::field my_vector
)。我还有一个字符串
std::string id
,它位于字符串字段中的某个位置,我希望找到它所在的位置。我习惯于用向量和数字来做这件事,如下所示:

arma::vec fun(arma::vec输入向量){
//查找向量等于5的位置(例如)
uvec指数=arma::find(输入向量=5);
收益指数;
}
我最初尝试做完全相同的事情,但给出了一个字符串而不是数字进行比较:

arma::uvec-fun(arma::vec-input\u-vector,std::string-id){
//查找向量字符串id所在的位置
uvec索引=arma::find(输入向量=id);
收益指数;
}
这将返回一个错误

错误:二进制表达式('arma::vec'(又称“Col”)的操作数无效
和'std::string'(也称为'basic_string'))
这是有意义的,因为向量未初始化为包含字符串。虽然我不认为向量可以初始化为包含字符串,因为当我尝试
arma::vec
时,它会给出一大堆错误

这就引出了可以容纳更多种类变量的字段

arma::uvec-fun(arma::field-input\u-field,std::string-id){
//查找向量等于5的位置(例如)
uvec index=arma::find(输入字段==id);
收益指数;
}
然而,这又回来了

错误:二进制表达式('arma::field'和
'std::string'(也称为'basic_string'))
我也尝试过strcmp,这也抛出了一个错误

错误:从'arma::field'到'const char*'没有可行的转换
这让我想问,如何才能找到字段中字符串的位置

我愿意将类型更改为更好的类型,我怀疑使用
std::vector
可能会更好,或者可能是我从未听说过的另一种类型。然而,我的第一次实验并不是很成功。如果有人能告诉我们该往哪个方向走,我们将不胜感激


Edit:澄清了
find
arma::find
而不是
std::find
,因为它不太清楚

arma
不支持在其数据结构中存储
std::string

根矩阵类为Mat,其中类型为:

  • float、double、std::complex、std::complex、short、int、long和short、int、long的无符号版本
反过来,
Rcpp
不支持将
std::vector
导入或导出到
armadillo
。因此,这是一个错误

在这种情况下,查找字符串的最简单方法是循环遍历向量并检查每个元素。或者,制作一张地图,看看钥匙是否在地图内

循环字符串检查方法:

#include <Rcpp.h>

// [[Rcpp::export]]
std::vector<int> find_string_locs(std::vector<std::string> input_vector, std::string id)
{
  std::vector<int> id_locs; // Setup storage for found IDs

  for(int i =0; i < input_vector.size(); i++) // Loop through input
    if(input_vector[i] == id) // check if input matches target
      id_locs.push_back(i);

    return id_locs; // send locations to R (c++ index shift!)
}

/***R
input_vector = c("stat","toad","stat","rcpp")
id = "stat"
find_string_locs(input_vector,id)
*/

<>注意C++索引移位…从0开始,而不是像R中那样从1开始。

arma
不支持在其数据结构中按

根矩阵类为Mat,其中类型为:

  • float、double、std::complex、std::complex、short、int、long和short、int、long的无符号版本
反过来,
Rcpp
不支持将
std::vector
导入或导出到
armadillo
。因此,这是一个错误

在这种情况下,查找字符串的最简单方法是循环遍历向量并检查每个元素。或者,制作一张地图,看看钥匙是否在地图内

循环字符串检查方法:

#include <Rcpp.h>

// [[Rcpp::export]]
std::vector<int> find_string_locs(std::vector<std::string> input_vector, std::string id)
{
  std::vector<int> id_locs; // Setup storage for found IDs

  for(int i =0; i < input_vector.size(); i++) // Loop through input
    if(input_vector[i] == id) // check if input matches target
      id_locs.push_back(i);

    return id_locs; // send locations to R (c++ index shift!)
}

/***R
input_vector = c("stat","toad","stat","rcpp")
id = "stat"
find_string_locs(input_vector,id)
*/

<>注意C++索引移位…从0开始,而不是在R.

< P>中,1,除了什么之外,Coatless说,我不确定,你理解C++中的向量的使用。在C++中,向量只是一个可重列表。 关于代数向量,它有一个量值(大小),但实际上没有方向。换句话说,它不是代数意义上的向量

如果要在std::vector中搜索字符串,可以这样做:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int get_str_position(const std::string& str_to_find, const std::vector<std::string>& vector_to_search)
{
    std::vector<std::string>::const_iterator it = std::find(vector_to_search.begin(), vector_to_search.end(), str_to_find);

    if (it == vector_to_search.end()) {
        return -1;
    }
    else {
        return it - vector_to_search.begin();
    }
};

void find_str(const std::string& str_to_find, const std::vector<std::string>& vector_to_search)
{
    int position = get_str_position(str_to_find, vector_to_search);

    if (position == -1) {
        std::cout << "Could not find string: '" << str_to_find << "'" << std::endl;
    }
    else {
        std::cout << "Found string '" << str_to_find << "' at position " << position << std::endl;
    }
};

int main()
{

    std::vector<std::string> vec = { "one", "two", "three" };

    std::string existent_str = "two";
    std::string non_existent_str = "four";

    find_str(existent_str, vec);
    find_str(non_existent_str, vec);

    return 0;
}
此外,在你的“乐趣”功能中,你说:

arma::uvec fun(arma::field<std::string> input_field, std::string id){

    // Find where vector equals 5 (for example)
    uvec index = find(input_field == id);

    return index;
}
arma::uvec-fun(arma::field-input\u-field,std::string-id){
//查找向量等于5的位置(例如)
uvec索引=查找(输入字段==id);
收益指数;
}
如果您使用的是std::find,您将只在“input_field”中搜索0或1的值,因为您将布尔表达式作为参数传递给find

// Not sure how this works at all
// You should not be able to compare a string to a std::vector<std::string>
bool true_or_false = (input_field == id); 


// Find 0 (false) or 1 (true)
uvec index = find(true_or_false);
//根本不知道这是怎么回事
//您应该无法将字符串与std::vector进行比较
布尔真或假=(输入字段==id);
//查找0(假)或1(真)
uvec索引=查找(真或假);

> p>除了什么之外,Coatless说,我不确定,你理解C++中的向量的使用。在C++中,向量只是一个可重列表。 关于代数向量,它有一个量值(大小),但实际上没有方向。换句话说,它不是代数意义上的向量

如果要在std::vector中搜索字符串,可以这样做:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int get_str_position(const std::string& str_to_find, const std::vector<std::string>& vector_to_search)
{
    std::vector<std::string>::const_iterator it = std::find(vector_to_search.begin(), vector_to_search.end(), str_to_find);

    if (it == vector_to_search.end()) {
        return -1;
    }
    else {
        return it - vector_to_search.begin();
    }
};

void find_str(const std::string& str_to_find, const std::vector<std::string>& vector_to_search)
{
    int position = get_str_position(str_to_find, vector_to_search);

    if (position == -1) {
        std::cout << "Could not find string: '" << str_to_find << "'" << std::endl;
    }
    else {
        std::cout << "Found string '" << str_to_find << "' at position " << position << std::endl;
    }
};

int main()
{

    std::vector<std::string> vec = { "one", "two", "three" };

    std::string existent_str = "two";
    std::string non_existent_str = "four";

    find_str(existent_str, vec);
    find_str(non_existent_str, vec);

    return 0;
}
此外,在你的“乐趣”功能中,你说:

arma::uvec fun(arma::field<std::string> input_field, std::string id){

    // Find where vector equals 5 (for example)
    uvec index = find(input_field == id);

    return index;
}
arma::uvec-fun(arma::field-input\u-field,std::string-id){
//查找向量等于5的位置(例如)
uvec索引=查找(输入字段==id);
收益指数;
}
如果您使用的是std::find,您将只在“input_field”中搜索0或1的值,因为您将布尔表达式作为参数传递给find

// Not sure how this works at all
// You should not be able to compare a string to a std::vector<std::string>
bool true_or_false = (input_field == id); 


// Find 0 (false) or 1 (true)
uvec index = find(true_or_false);
//根本不知道这是怎么回事
//您应该无法将字符串与std::vector进行比较
布尔真或假=(输入字段==id);
//查找0(假)或1(真)
uvec索引=查找(真或假);

谢谢您的建议。这部分代码在de中