简体   繁体   English

如何在不退出 function 并随后将其存储在 c++ 的情况下返回一个值

[英]how to return a value without exiting a function and storing it subsequently, in c++

Here I'm trying to mimic data feed streaming from stock exchange using random number and subsequently store it in array.在这里,我尝试使用随机数模拟来自证券交易所的数据馈送流,然后将其存储在数组中。

#include <iostream>
#include <array>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <ctime>
#include <stdlib.h>

long int prevcntr=0;

using namespace std;

std::pair<long int, double>feedgenerator(long int len)
{
    srand(time(0));
    for(long int itr=1;itr<=len;itr++)
    {
        return {itr, (double)rand()/RAND_MAX};
        //should continue evaluating the function without exiting
        pause(0.001); //To allow some interval for 'makearray' to store it
    }
}

template<size_t nn>
std::array<double, nn> makearray(long int cntr, double value, long int len)
{
    std::array<double, nn> outarr; // should be able to pass the value of 'len' to 'nn'
    long int itr=0;
    begin:
    while(cntr <= prevcntr)goto begin; // should wait until the next update
    outarr[itr] = value;
    prevcntr = cntr;
    while(itr<len)
    {
        itr++;
        goto begin; // control goes back to the beginning until all the elements of the array are filled with value
    }
    //should return the array after it is fully filled
    return outarr;
}

int main()
{
    double *p=new double[];
    long int len = 100000;
    *p = makearray(feedgenerator(len), len)

    // I should be able to call these as nested functions as above

    for(int i=0;i<len;i++)
    cout<<*p[i]<<"\n";

    return 0;
}

Question is how do I return a value without exiting the feedgenerator function.问题是如何在不退出feedgenerator function 的情况下返回一个值。 If I try to get all the values at once then it wouldn't mimic the data feed.如果我尝试一次获取所有值,那么它就不会模仿数据馈送。 Data feed is essentially, the same value being updated, sequentially.数据馈送本质上是按顺序更新相同的值。

To store the data, makearray is being used ( vector shouldn't be used as it is extremely slow).为了存储数据,正在使用makearray (不应该使用vector ,因为它非常慢)。

Overall the idea is, feedgenerator should update the same value with an increasing counter (in the real scenario counter will be the time and value will be price etc.) and makearray should store the data (Unless I store the data, the data would be lost, as in the case of data feed from stock exchange) for subsequent analysis.总的来说, feedgenerator应该使用递增的计数器更新相同的值(在实际场景中,计数器将是时间将是价格等)并且makearray应该存储数据(除非我存储数据,否则数据将是丢失,如来自证券交易所的数据馈送)以供后续分析。

In makearray , I want to pass the length of the array as an argument of the function, so that it can be controlled by another program.makearray中,我想将数组的长度作为 function 的参数传递,以便它可以由另一个程序控制。 How can it be done is not clear to me.我不清楚如何做到这一点。

The code in it's present form, doesn't compile.当前形式的代码无法编译。

Question is how do I return a value without exiting the feedgenerator function问题是如何在不退出 feedgenerator function 的情况下返回一个值

Because you are trying to mimic the data feed stream from stock exchange, so, I think you should consider to use thread.因为您试图模仿来自证券交易所的数据馈送 stream,所以我认为您应该考虑使用线程。 Example:例子:

class FeedGenerator {
  long int max_length;
  std::deque<double> queue;
  std::mutex mutex;
  std::thread thread;
public:
  FeedGenerator(long int len) : max_length(len) {
    thread(&FeedGenerator::run, this);
  }

  // Get the latest `len` values
  int getData(std::vector<double>& vec, int len) {
    std::lock_guard<std::mutex>(mutex);
    int nlen = std::min(len, queue.size());
    vec.resize(nlen);
    std::copy(queue.end() - nlen, queue.end(), vec.begin());
    return nlen;
  }

private:
  void run() {
    srand(time(0));
    while (TRUE)
    {
      {
        std::lock_guard<std::mutex>(mutex);
        queue.push_back((double)rand()/RAND_MAX);
        if (queue.size() >= len) {
          queue.pop_front();
        }
      }
      pause(0.001);
    }
  }
};

int main()
{
  long int len = 100000;
  FeedGenerator feedgenerator(len);

  std::vector<double> p;
  feedgenerator.getData(p, 10); // get the latest 10 values

  for(int i=0;i<p.size();i++)
  std::cout << p[i] << std::endl;

  return 0;
}

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

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