I have created a function in main to count and return the number of lines in the text file ('size'). 'Size' is then passed to the constructor used to create an object of type/called 'Data'. A member function of the Data class is getData(). getData() reads the text file, line by line, and initializes three vectors to various substrings of the strings per line.
9Sorry for some reason the code formatting button doesn't work so here's my code in plain text):
#include <iostream>
#include <fstream>
...
using namespace std;
Data::Data(){
}
Data::Data(int size){
m_size = size;
}
void Data::getData(){
int i = 0;
string s1, s2, 23;
ifstream myfile("test.txt");
if (myfile.is_open()){
while (getline(myfile, line)){
s1=line.substr(0, 10);
DataVector1[i] = s1;
s2=line.substr(11, 16);
DataVector2[i] = s2;
s3=line.substr(17, 20);
DataVector3[i] = s3;
i++;
}}
else { cout << "Unable to open file"; }
}
Now I want to be able to work with DataVector1, DataVector2 and DataVector3 outside of this member function in my main client file.
So one way I was thinking was to define, initialize, and make the member function return a 2D vector instead of void/nothing... then in the main file assign another 2D vector to the vector returned by getData().
But is there another way with pointers? ... can I create a pointer in main to reference DataVector 1, DataVector2 and DataVector3 in getData()? So I don't have to re-copy anything?
What would be the best way to accomplish this with the best efficiency (it's a large data set)?
Sorry if this is a bad question I'm still new at C++.
Thanks for getting back. Actually no ... I just used 3 in this example, but I will actually need to work with 8 vectors (each containing 8 small substring portions of the line string) e.g.
If line 0: abc473nnn^^^^99 etc...
v1[0]=abc
v2[0]=473
v3[0]=nnn
v4[0]=^^^^
v5[0]=99
etc... till v8
And then again for the next line
Line 1: bbb732qqq**77
v1[1]=bbb
v2[1]=732
v3[1]=qqq
v4[1]=**
etc...
so eight vectors named v1...v8 (all of string type [treat integers as strings])
each with n-elements (number of lines in the text file, as previously counted by 'size').
Any insight on how to best accomplish this would be appreciated.
I guess my main question is this: I want to make these vectors 'global' in the sense that they will be initialized in getData() ... but thereafter accessible by other member functions (assuming getData() will be called before any other functions in main).
e.g. after v1, v2, v3 ... are initialized in getData(). Define another member function getV1 that returns v1.
But getV1() first has to access V1 from getData() where it is defined, and then return it. Is there some way to make these vectors 'global'? (Maybe declaring them as public/private header file ... and then constructing v1(size), v2(size) in the constructor ?
Sorry if this sounds naive, I'm still starting out with C++.
then you could return a vector of vectors back I guess?
Or encapsulate that in another new class (for example "VectorHolder". then when you have all your vectors construct a vectorHolder object and pass this back into main.