我正在尝试在R中使用Rcpp程序包。我有以下c ++代码在投票矢量上使用D'Hondt方法:
#include <iostream>
#include <vector>
#include<Rcpp.h>
// original: http://www.brainum.es/code/ccplusplus/sistema-d-hondt-en-c
struct DhondtParty {
int votes, seats;
bool ok;
};
class Dhondt {
public:
DhondtParty* parties;
double minimun;
int votes,
numparties,
blank_votes,
esc;
Dhondt(int total,int arrayMem) {
this->esc = total;
this->numparties = 0; this->minimun = 0; this->blank_votes = 0;
this->parties = new DhondtParty[arrayMem];
}
void addblank(int pvotes) {
this->votes += pvotes;
this->blank_votes += pvotes;
}
void addparty(int partyvotes) {
DhondtParty party;
party.seats = 0;
party.votes = partyvotes;
party.ok = false;
this->parties[this->numparties] = party;
this->votes = this->votes+partyvotes;
this->numparties++;
}
void setminimun(double minimun) {
this->minimun = minimun;
}
void results() {
int i,z,mparty;
for(z=0;this->parties[z].votes;z++) {
if((double)this->parties[z].votes/(double)this->votes >= (double)this->minimun/100.0) this->parties[z].ok = true;
}
for(i=1;i<=this->esc;i++) {
mparty = -1;
for(z=0;this->parties[z].votes;z++) {
if(this->parties[z].ok && (this->parties[z].votes/(this->parties[z].seats+1) > this->parties[mparty].votes/(this->parties[mparty].seats+1) || mparty < 0)) {
mparty = z;
}
}
this->parties[mparty].seats++;
}
}
};
// [[Rcpp::export]]
Rcpp::NumericVector aplicaleydHont_a(){
Dhondt d(100,6); // Se llama a la clase, y se guarda en la variable d.
// Añadimos los partidos:
d.addparty(23);
d.addparty(21);
d.addparty(15);
d.addparty(11);
d.addparty(1);
d.addparty(18);
d.addblank(3); // votos en blanco
d.setminimun(3); // En %
d.results(); // Procesamos
Rcpp::NumericVector escas(d.numparties);
for(int i = 0; i < d.numparties; ++i){
std::cout << d.parties[i].seats << std::endl;
escas[i] = d.parties[i].seats;
}
return escas;
}
// [[Rcpp::export]]
Rcpp::NumericVector aplicaleydHont(int v){
Dhondt d(100,6); // Se llama a la clase, y se guarda en la variable d.
// Añadimos los partidos:
d.addparty(23);
d.addparty(21);
d.addparty(15);
d.addparty(11);
d.addparty(1);
d.addparty(18);
d.addblank(3); // votos en blanco
d.setminimun(3); // En %
d.results(); // Procesamos
Rcpp::NumericVector escas(d.numparties);
for(int i = 0; i < d.numparties; ++i){
std::cout << d.parties[i].seats << std::endl;
escas[i] = d.parties[i].seats;
}
return escas;
}
如您所见,我有一个类和2个相同的函数,第一个不带任何参数,第二个不带参数。事实是,当我将函数与参数一起使用时(最后我想在矢量参数中通过投票),我不会得到任何结果。
我用来调用此函数的R代码:
library(Rcpp)
Rcpp::sourceCpp('D:/Elecciones/modeloR_espa16/leydHont.cpp')
aplicaleydHont_a()
aplicaleydHont(1)
我得到了这个结果:
> aplicaleydHont_a()
35
21
15
11
0
18
[1] 35 21 15 11 0 18
> aplicaleydHont(1)
0
0
0
0
0
0
[1] 0 0 0 0 0 0
有人知道为什么我不能将参数传递给这些函数吗? Rcpp和c ++类的集成是否存在一些问题?
谢谢!
Xevi
答案 0 :(得分:2)
处理文件时,我得到以下输出:
dHondt.cpp: In function ‘Rcpp::NumericVector aplicaleydHont_a()’:
dHondt.cpp:35:29: warning: ‘d.Dhondt::votes’ is used uninitialized in this function [-Wuninitialized]
this->votes = this->votes+partyvotes;
~~~~~~^~~~~
dHondt.cpp: In function ‘Rcpp::NumericVector aplicaleydHont(int)’:
dHondt.cpp:35:29: warning: ‘d.Dhondt::votes’ is used uninitialized in this function [-Wuninitialized]
this->votes = this->votes+partyvotes;
~~~~~~^~~~~
>
aplicaleydHont_a()
0
0
0
0
0
0
[1] 0 0 0 0 0 0
>
aplicaleydHont(1)
0
0
0
0
0
0
[1] 0 0 0 0 0 0
添加后
this->votes = 0;
对于构造函数,我得到:
>
aplicaleydHont_a()
35
21
15
11
0
18
[1] 35 21 15 11 0 18
>
aplicaleydHont(1)
35
21
15
11
0
18
[1] 35 21 15 11 0 18
注意:
Rcpp::Rcout
代替std::cout
。vector
而不是裸数组。顺便说一句,在CRAN上搜索“ D’Hondt”会产生几个结果。您检查这些还不够吗?