Sehr große Nachschlagetabelle C++ - kann ich es vermeiden, das Ganze abzutippen?

Sehr große Nachschlagetabelle C++ - kann ich es vermeiden, das Ganze abzutippen?


Ich bin kein Programmierer, aber ein Ingenieur, der bei dieser Gelegenheit C++-Codierung verwenden muss, also entschuldigen Sie, wenn diese Frage ein wenig grundlegend ist.


Ich muss eine Nachschlagetabelle verwenden, da ich einige hochgradig nichtlineare Dynamiken habe, die ich modellieren muss. Es besteht aus buchstäblich 1000 gepaarten Werten, von einem Paar von (0,022815, 0,7) bis zu (6,9453, 21,85).


Ich möchte nicht alle diese Werte in meinen C-Code eingeben müssen. Die Werte werden derzeit in Matlab gespeichert. Kann ich sie aus einer .dat-Datei oder etwas Ähnlichem lesen?


Ich habe einen Wert berechnet und möchte einfach, dass das Programm den gepaarten Wert rausschmeißt.


Danke,


Adam


Einige Code-Antworten


{ 0.022815, 0.7 },
... { 6.9453, 21.85 },
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
int main(){
ifstream inFile("a.txt", ios::in);
if (! inFile ){
cout<<"unabl to open";
return 0;
}
//reading a file and inserting in a map
map<double,double>
mymap;
double a,b;
while( ! inFile.eof() ){
inFile>>a>>b;
mymap.insert ( a,b );
}
inFile.close();
//be sure to close the file
//iterating on map
map<double,double>::iterator it;
for ( it=mymap.begin() ;
it != mymap.end();
it++ ){
// (*it).first // (*it).second
}
//writing the map into a file
ofstream outFile;
outFile.open ("a.txt", ios::out);
// or ios::app if you want to append
for ( it=mymap.begin() ;
it != mymap.end();
it++ ){
outFile <<
(*it).first <<
" - " <<
(*it).second <<
endl;
//what ever!
}
outFile.close();
return 0;
}
>>
cat import_data.h #define TBL_SIZE 4 // In your case it is 1000 const double table[TBL_SIZE][2] = {
{ 0.022815, 0.7 },
{ 6.9453, 21.85 },
{ 4.666, 565.9},
{ 567.9, 34.6} };
>>
cat lookup.c #include <stdio.h>
#include "import_data.h" double lookup(double key) {
int i=0;
for(;i<TBL_SIZE;
i++) {
if(table[i][0] == key) return table[i][1];
}
return -1;
//error } int main() {
printf("1. Value is %f\n", lookup(6.9453));
printf("2. Value is %f\n", lookup(4.666));
printf("3. Value is %f\n", lookup(4.6));
return 0;
}
fopen fread fclose 
ifstream 
#include<fstream>
#include<map>
... ifstream infile("yourdatfile.dat");
std::string str;
std::map<double, double>
m;
//use appropriate type(s) while(getline(infile, str)){ //split str by comma or some delimiter and get the key, value //put key, value in m } //use m