Skip to content

Newbie here: How do I read csv into a vector of structs? #160

Closed Answered by d99kris
ChuckStarchaser asked this question in Q&A
Discussion options

You must be logged in to vote

Hi - so the API of rapidcsv is more intended to operate directly on columns of data, say get an vector of floats holding all close prices.

There are other C++ CSV libraries which provides more low-level access to parsed data and as such are more suitable for populating vectors of custom structs.

Having that said, it is certainly possibly to use rapidcsv. One can for example iterate over the rows and build a struct using something like this:

#include <iostream>
#include <vector>
#include "rapidcsv.h"

struct Daily
{
  std::string m_Date;
  float m_Open = 0.0;
  float m_High = 0.0;
  float m_Low = 0.0;
  float m_Close = 0.0;
  int m_Volume = 0.0;
  float m_AdjClose = 0.0;
};

int main()
{
 …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by d99kris
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants