如何将向量放入 y 值中?
How do I put a vector into y values?
我正在创建一个从文本文件读取并绘制整数的程序。这是我目前所拥有的
#include "realtime.h"
#include "ui_realtime.h"
#include <QFile>
#include<QIODevice>
Realtime::Realtime(QWidget *parent) :
QWidget(parent),
ui(new Ui::Realtime)
{
ui->setupUi(this);
int size = 1000;
QVector<double> x(size), y(size);
for (int i=0; i<size; i++)
{
x[i]= i;
y[i]= i ;
}
ui->plot->addGraph();
ui->plot->graph(0)->setData(x,y);
ui->plot->xAxis->setRange(0,10);
ui->plot->yAxis->setRange(0,10);
}
Realtime::~Realtime()
{
delete ui;
}
int main()
{
std::vector<int>ints;
QFile file("2dplotarray.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
while (!file.atEnd())
{
QByteArray line = file.readLine();
QDataStream ds(line);
int int_in_line = 0;
ds >> int_in_line;
ints.push_back(int_in_line);
}
return 0 ;
}
忽略当前的 x 和 y 值,那是我在测试绘图功能。如何将文本文件放入绘图的 y 值中?
文本文件看起来像这样(NOT CODE 只是显示它的最佳方式)
1
2
3
4
etc...
除非未说明的要点是使用 QT 类,如果我是你,我只会使用输入文件流。以下是一些代码在读取这些值时的样子。
#include <fstream>
#include <string>
int main()
{
std::vector<int> vecValues;
std::ifstream file("yourfilename.txt");
if (file.is_open())
{
//if you can anticipate that the only values will be integers
//then this will work just fine
int value;
while (file >> value)
{
vecValues.push_back(value);
}
//or you could modify the following if you expected non-numbers
//in your file and need more control over handling them
//std::string fileLine;
//while ( std::getline(file, fileLine) )
//{
// vecValues.push_back(std::atoi(fileLine.c_str()));
//}
file.close();
}
return 0;
}
我正在创建一个从文本文件读取并绘制整数的程序。这是我目前所拥有的
#include "realtime.h"
#include "ui_realtime.h"
#include <QFile>
#include<QIODevice>
Realtime::Realtime(QWidget *parent) :
QWidget(parent),
ui(new Ui::Realtime)
{
ui->setupUi(this);
int size = 1000;
QVector<double> x(size), y(size);
for (int i=0; i<size; i++)
{
x[i]= i;
y[i]= i ;
}
ui->plot->addGraph();
ui->plot->graph(0)->setData(x,y);
ui->plot->xAxis->setRange(0,10);
ui->plot->yAxis->setRange(0,10);
}
Realtime::~Realtime()
{
delete ui;
}
int main()
{
std::vector<int>ints;
QFile file("2dplotarray.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
while (!file.atEnd())
{
QByteArray line = file.readLine();
QDataStream ds(line);
int int_in_line = 0;
ds >> int_in_line;
ints.push_back(int_in_line);
}
return 0 ;
}
忽略当前的 x 和 y 值,那是我在测试绘图功能。如何将文本文件放入绘图的 y 值中? 文本文件看起来像这样(NOT CODE 只是显示它的最佳方式)
1
2
3
4
etc...
除非未说明的要点是使用 QT 类,如果我是你,我只会使用输入文件流。以下是一些代码在读取这些值时的样子。
#include <fstream>
#include <string>
int main()
{
std::vector<int> vecValues;
std::ifstream file("yourfilename.txt");
if (file.is_open())
{
//if you can anticipate that the only values will be integers
//then this will work just fine
int value;
while (file >> value)
{
vecValues.push_back(value);
}
//or you could modify the following if you expected non-numbers
//in your file and need more control over handling them
//std::string fileLine;
//while ( std::getline(file, fileLine) )
//{
// vecValues.push_back(std::atoi(fileLine.c_str()));
//}
file.close();
}
return 0;
}