私のクラスは、タイプのいくつかのベクトル行列含ま値を変更されていません。ランダム生成されたベクトルは
typedef std::vector<double> MyArray;
typedef std::vector<MyArray> MyMatrix;
クラスupdate()
メソッドを持っているが、それぞれに新しいランダム行列を生成する必要があり、それを呼び出すと、古い場所std::map<double, MyMatrix, std::greater<double>> rankedMyMatrix_;
。
Updateメソッドは、およそ次のようになります。
void MyClass::update(...){
...
this->generateMyMatrix(); // Generates new random currentMyMatrix_
...
rankedMyMatrix_.insert({matrixRank_, currentMyMatrix_});
...
}
方法generateMyMatrix()
次のようになります。
void MyClass::generateMyMatrix(){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(-1, 1);
for (int i = 0; i < noMotors_; i++) {
MyArray array(MaxArraySamples, 0);
for (int j = 0; j < MaxArraySamples; j = j + steps) {
array[j] = dist(mt);
}
currentMyMatrix_.push_back(array);
}
}
トラブルはrankedMyMatrix_
マップへの各プッシュで、新しいupdate()
に、それは同じ行列を生成することです。それともそれに書いていないのですか?おそらく私はここに何かを見逃しているので、どんな助けもありがたいです。ここでUPDATE
はコンパイルおよびテストできるコードです:
#include <iostream>
#include "MyClass.h"
using namespace std;
int main() {
MyClass* c;
c = new MyClass();
int x = 0;
while (x<3){
c->update();
x++;
}
return 0;
}
ヘッダファイル:
#ifndef PROBA_MYCLASS_H
#define PROBA_MYCLASS_H
#include <vector>
#include <map>
typedef std::vector<double> MyArray;
typedef std::vector <MyArray> MyMatrix;
const int NoMotors = 4;
const int MaxArraySamples = 10;
class MyClass {
public:
MyClass();
~MyClass();
void update();
private:
std::map<double, MyMatrix, std::greater<double>> rankedMyMatrix_;
MyMatrix currentMyMatrix_;
int matrixRank_;
void generateMyMatrix();
void writeCurrent();
};
#endif //PROBA_MYCLASS_H
クラスファイル:
#include "MyClass.h"
#include <random>
#include <iostream>
#include <fstream>
MyClass::MyClass() {
currentMyMatrix_.reserve(NoMotors);
matrixRank_ = 0;
}
MyClass::~MyClass() { }
void MyClass::update() {
this->generateMyMatrix(); // Generates new random currentMyMatrix_
rankedMyMatrix_.insert({matrixRank_, currentMyMatrix_});
matrixRank_++;
this->writeCurrent();
}
void MyClass::generateMyMatrix() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(-1, 1);
for (int i = 0; i < NoMotors; i++) {
MyArray array(MaxArraySamples, 0);
for (int j = 0; j < MaxArraySamples; j = j + 3) {
array[j] = dist(mt);
}
currentMyMatrix_.push_back(array);
}
}
void MyClass::writeCurrent() {
std::ofstream outputFile;
std::string uri = "/tmp/output/test";
outputFile.open(uri + std::to_string(matrixRank_) + ".txt");
for (int i = 0; i < MaxArraySamples; i++) {
for (int j = 0; j < NoMotors; j++) {
outputFile << currentMyMatrix_[j][i] << " ";
}
outputFile << std::endl;
}
outputFile.close();
}
とCMakeLists.txt :
cmake_minimum_required(VERSION 3.5)
project(proba)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp MyClass.h MyClass.cpp)
add_executable(proba ${SOURCE_FILES})
それでも同じ出力が得られます。私は無知だ。
:エラーがそれは常に最後に新しい値をプッシュ
を使用していましたので、コードは最初
noMotors_
arrays.Thisを参照するソリューションですですあなたは本当にMinGWを使用していますか? –@ baum-mit-augenいいえ、Mac – miller
あなたが使っているコンパイラと標準ライブラリのバージョンを教えてください。しかし、gcc/libstdC++の場合、おそらくMinGWの場合と同じ問題です。 –