简体   繁体   English

如何将向量放入y值?

[英]How do I put a vector into y values?

Im creating a program that reads from a text file and plots the integers. 我创建了一个从文本文件读取并绘制整数的程序。 Here is what I have so far 这是我到目前为止的

#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    ;
}

Ignore the current x and y values, that was me testing the plotting features. 忽略当前的x和y值,这就是我测试绘图功能的原因。 How do i put the text file into the y values of my plot? 如何将文本文件放入图的y值? The text file looks like this(NOT CODE just the best way to display it) 文本文件看起来像这样(不是最好的显示代码)

    1
    2
    3
    4
    etc...

Unless the unstated point is to use the QT classes, I'd just use an input file stream if I were you. 除非未声明的要点是使用QT类,否则如果我是我,我只会使用输入文件流。 Here is what some code might look like to read in those values. 以下是一些代码在这些值中读取的内容。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM