Rcpp,搜索文件中的值

时间:2017-07-25 09:49:03

标签: c++ rcpp

我的问题: 我想在txt文件中搜索一个值,而不是在它上面运行一些数学运算符。我是否有可能使用输入(cin>> ..)在c ++中搜索值 这是我的代码

$args=array(
    'post_type' => 'post'

);
$the_query = null;
// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="entry-content"><?php the_content(); ?></div>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

这是我的错误 - &gt;

#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);

    }

1 个答案:

答案 0 :(得分:0)

Rcpp应该已经能够处理std::string,所以完全删除Rcpp::as<string>