-3

Is there a way to take a series of vectors in C++ and output a CSV file where the columns of the CSV file are the elements of the vectors respectively? So column 1 would be the elements of say the first vector of doubles for examples, etc.

gb4
  • 11
  • 2
  • What do you mean by "a series of vectors"? What have you tried so far? – Mance Rayder Jun 26 '19 at 20:21
  • unless the question is `is there a way to solve the halting problem?`, the answer is almost always `yes`. What have you tried? Do you know how vector indexing works? – MPops Jun 26 '19 at 20:56

1 Answers1

2
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    // series of vectors could be put in another collection
    vector<double> col1 = { 0, 1, 2 };
    vector<double> col2 = { 3, 4, 5 };
    vector<double> col3 = { 6, 7, 8 };

    ofstream csvfile("series_vectors.csv");
    if (csvfile.is_open())
    {
        // it is possible that the vectors are not of the same size
        // if you get the maximum, then you may need to fill in empty fields 
        //     for some of the columns
        int num_of_rows = min({ col1.size(), col2.size(), col3.size() });
        for (int i = 0; i < num_of_rows; i++)
        {
            csvfile << col1[i] << "," << col2[i] << "," << col3[i] << endl;
        }
    }
    else
    {
        cout << "File could not be opened";
    }

    return 0;
}
Khushimia
  • 36
  • 1