我在C ++中编程的向量没有返回任何内容。它应该返回5个元素

时间:2018-06-07 03:09:58

标签: c++ vector

您好,我在使用Code Blockers开发的C ++代码时遇到了问题。它实现了机器人的感觉和移动(非常基本),有概率的矢量 p ,我将应用感觉并移动我的主要功能。

下面有一个感知功能和一个移动功能,它基本上改变了我的向量概率 p

我在他的主要功能中称呼它们,但它似乎不起作用它返回5纳米值。

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

using namespace std;

vector<float> p (5);
vector<string> world (5);
vector<string> measurements (2);
vector<int> motions (2);
float pHit;
float pMiss;
float pExact;
float pOvershoot;
float pUndershoot;
vector<float> sense(vector<float> p, string Z);
vector<float> move(vector<float> p, int U);


int main(){
    vector<float> p = {0.2, 0.2, 0.2, 0.2, 0.2};
    vector<string> world = {"green", "red", "red", "green", "green"};
    vector<string> measurements = {"red", "green"};
    //string measurements = "red";
    vector<int> motions = {1,1};
    float pHit = 0.6;
    float pMiss = 0.2;
    float pExact = 0.8;
    float p0vershoot = 0.1;
    float pUndershoot = 0.1;

    vector<float> sense(vector<float> p, string Z);
    vector<float> moves(vector<float> p, int U);


    for(int i=0; i<measurements.size(); i++){
        p = sense(p, measurements[i]);
        p = moves(p, motions[i]);
    }

    for(int i=0; i<p.size(); i++){
        cout << p[i] << " ";
    }

    return 0;
}

vector<float> sense(vector<float> p, string Z){
    vector<float> q;
   // q.reserve(p.size());
    for(int i=0; i<p.size(); i++){
        bool hit = (Z == world[i]);
        q.push_back(p[i] * (hit * pHit + (1-hit) * pMiss));
    }

    float s = 0.0;
    for(int i=0; i<q.size(); i++){
        s += q[i];
    }

    for(int i=0; i<q.size(); i++){
        q[i] /= s;
    }

    return q;
}

vector<float> moves(vector<float> p, int U){
    vector<float> q;
   // q.reserve(p.size());
    for(int i=0; i<p.size(); i++){
        float s = pExact * p[(i-U) % p.size()];
        s = s + pOvershoot * p[(i-U-1) % p.size()];
        s = s + pUndershoot * p[(i-U+1) % p.size()];
        q.push_back(s);
    }
    return q;
}

1 个答案:

答案 0 :(得分:0)

您的代码不会按照您在此处提供的方式进行编译。首先,您尝试在其他函数中引用main中定义的局部变量。其次,您有一堆隐式的double到float转换。如果以某种方式解决这些问题,您的代码似乎有效(请参阅下面的代码)。

#include <vector>
#include <iostream>

using namespace std;

vector<double> p = { 0.2, 0.2, 0.2, 0.2, 0.2 };
vector<string> world = { "green", "red", "red", "green", "green" };
vector<string> measurements = { "red", "green" };

float pHit = 0.6;
float pMiss = 0.2;
float pExact = 0.8;
float pOvershoot = 0.1;
float pUndershoot = 0.1;

int main() {
    //string measurements = "red";
    vector<int> motions = { 1,1 };

    vector<double> sense(vector<double> p, string Z);
    vector<double> moves(vector<double> p, int U);


    for (int i = 0; i<measurements.size(); i++) {
        p = sense(p, measurements[i]);
        p = moves(p, motions[i]);
    }

    for (int i = 0; i<p.size(); i++) {
        cout << p[i] << " ";
    }

    return 0;
}

vector<double> sense(vector<double> p, string Z) {
    vector<double> q;
    // q.reserve(p.size());
    for (int i = 0; i<p.size(); i++) {
        bool hit = (Z == world[i]);
        q.push_back(p[i] * (hit * pHit + (1 - hit) * pMiss));
    }

    float s = 0.0;
    for (int i = 0; i<q.size(); i++) {
        s += q[i];
    }

    for (int i = 0; i<q.size(); i++) {
        q[i] /= s;
    }

    return q;
}

vector<double> moves(vector<double> p, int U) {
    vector<double> q;
    // q.reserve(p.size());
    for (int i = 0; i<p.size(); i++) {
        float s = pExact * p[(i - U) % p.size()];
        s = s + pOvershoot * p[(i - U - 1) % p.size()];
        s = s + pUndershoot * p[(i - U + 1) % p.size()];
        q.push_back(s);
    }
    return q;
}