2017-07-25 23 views
0

我的问题: 我想在txt文件中搜索一个值,而不是在其上运行一些数学运算符。是有可能,我输入搜索类似值在C++中(CIN >> ..) 这里是我的代码Rcpp,搜索文件中的值

#include <fstream> 
#include <cstdlib> 
#include <sstream> 
#include <string> 
#include <vector> 
#include <stdlib.h> 
#include <iostream> 
#include <Rcpp.h> 

//[[Rcpp::plugins(cpp14)]] 



using namespace std; 
using namespace Rcpp; 

//[[Rcpp::export]] 

vector<string> split(string str, char delimiter) { 
vector<string> internal; 
stringstream ss(str); 
string tok; 
while (getline(ss, tok, delimiter)) { 
    internal.push_back(tok); 
} 
return internal; 
} 


//[[Rcpp::export]] 
CharacterVector to_string(SEXP t) 
{ 
return CharacterVector(t); 
} 


//[[Rcpp::plugins(cpp14)]] 


//[[Rcpp::export]] 
List read(string filename, string arg) 

{ 


    ifstream file(filename.c_str()); 

    string search= Rcpp::as<string>(arg); 
    // for example string search="usa"; 

    string line; 
    vector<vector<string>> table; 
    while (file.good()) { 
     getline(file, line); 
     if (!file.fail()) { 
      table.push_back(split(line, ',')); 
     } 
    } 
    vector<vector<string>> searchResults; 

    for (vector<string> line : table) { 
     for (string column : line) { 
      if (column == suchen) { 
       searchResults.push_back(line); 
      } 
     } 
    } 


    //calc average 
    int length = searchResults.size(); 
    vector<string> result; 
    double secondRow=0; 
    double thirdRow =0; 
    for (vector<string> line : searchResults) { 
     secondRow += atoi(line.at(1).c_str()); 
     thirdRow += atoi(line.at(2).c_str()); 
    } 






    result.insert(result.begin(), search); 
    result.insert(result.end(),to_string(secondRow/length)); 
    result.insert(result.end(),to_string(thirdRow/length)); 





    for (string i : result) { 
     cout << i ; 
    } 


    return Rcpp::List::create(
     Rcpp::Named("search") = search, 
     Rcpp::Named("total") =secondRow/length, 
     Rcpp::Named("average")= thirdRow/length); 

    } 

这是我的错误 - >

c:/Rtools/mingw_64/bin/g++ -I"C:/PROGRA~1/R/R-34~1.0/include" -DNDEBUG  -I"C:/Users/xy/Documents/R/win-library/3.4/Rcpp/include"  -I"c:/Users/Yesim/Desktop/julivierrcpp"   -I"d:/Compiler/gcc-4.9.3/local330/include" -std=c++14 -O2 -Wall -mtune=core2  -c main.cpp -o main.o 
main.cpp: In function 'Rcpp::List read(std::string, std::string)': 
main.cpp:48:39: error: no matching function for call to 'as(std::string&)' 
string suchen = Rcpp::as<string>(arg); 
           ^
    main.cpp:48:39: note: candidate is: 
    In file included from C:/Users/Yesim/Documents/R/win-       library/3.4/Rcpp/include/RcppCommon.h:160:0, 
      from C:/Users/Yesim/Documents/R/win-        library/3.4/Rcpp/include/Rcpp.h:27,     from main.cpp:8: 
C:/Users/xy/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:143:29:  note: template<class T> T Rcpp::as(SEXP) 
    template <typename T> T as(SEXP x) { 
         ^
    C:/Users/xy/Documents/R/win-library/3.4/Rcpp/include/Rcpp/as.h:143:29: note:  template argument deduction/substitution failed: 
    main.cpp:48:39: note: cannot convert 'arg' (type 'std::string {aka    std::basic_string<char>}') to type 'SEXP' 
     string search= Rcpp::as<string>(arg); 
           ^
    make: *** [main.o] Error 1 

回答

0

RCPP应该已经是能够处理std::string s so删除Rcpp::as<string>

+0

谢谢,它的工作原理 – Moritz