clearing a member variable

G

Gary Wessle

hi

I have a design problem with this code, it is suppose to take a space
delimited data file
file_info x( f ); //f is a text file provided below
and return the column of choice
vector<string> bb = x.get_col(1);
but if I tried it the second time
vector<string> bb = x.get_col(2);
it does not clear the member variable from the old data of col(1). how
can I do this. I can pop the elements out from vec.begin() to
vec.end() but there may be a better way for this task all together.

**************** test.txt ****************
2 0
3 1
4 2
5 3
6 4
7 5
8 6
7 5
6 4
5 3
4 2
3 1
6 4
9 7
11 9
10 8
9 7
8 6
7 5
5 3





**************** main.cpp ****************
#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::vector;

#include<string>
using std::string;

#include "useful.h"

int main() {
string f = "test_data.txt";
file_info x( f ); // space delimited
vector<string> bb = x.get_col(1);
vector<string> aa = x.get_col(2);
vector<double> b, a;

for (unsigned i=0; i<bb.size(); i++) //aa.size() give segmentation fault.
cout << bb << '\t'
<< aa << '\n';
}


**************** useful.h ****************
#include <iostream>
using std::cout;

#include <string>
using std::string;

#include <vector>
using std::vector;

#include <utility>
using std::pair;

#include "useful.h"


#ifndef USEFUL_H
#define USEFUL_H
#include <vector>
using std::vector;

class file_info
{
int nRows, nCol;
string file_name;
vector<string> all_cols;
vector<string> col;
void stock_take();
void set_col(int);

public:

file_info(string const& fileName);
~file_info();

/** returns a pair of values, the first is the
* number of rows in a space delimited data file
* and the second is the number of columns.
**/
pair<int,int> get_counts();

/** takes a number n and returns a vector
* containing the nth column of the data file
**/
vector<string> get_col(int);

};


#endif

**************** useful.cpp ****************
#include <utility>
using std::pair;
using std::make_pair;

#include <string>
using std::string;

#include <fstream>
using std::ifstream;

#include <sstream>
using std::stringstream;

#include "useful.h"


file_info::file_info( string const& fileName )
: file_name( fileName ){
nCol = 0;
nRows = 1;
stock_take();
}
void file_info::stock_take() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

string word;
while(input >> word) {
nCol++; // init'd by constructor
all_cols.push_back(word);
}

while (getline(in, line)){
nRows++; // init'd by constructor
stringstream input( line.c_str() );
string word;
while(input >> word)
all_cols.push_back(word);
}

}
file_info::~file_info() {}

pair<int,int> file_info::get_counts()
{
return make_pair(nRows, nCol);
}

void file_info::set_col(int x){
for(int i = (x-1); static_cast<unsigned> (i) < all_cols.size(); i = (i+nCol) )
col.push_back(all_cols);
}

vector<string> file_info::get_col(int y){
set_col(y);
return col;
}
 
S

Stuart Redmann

Gary said:
hi

I have a design problem with this code, it is suppose to take a space
delimited data file
file_info x( f ); //f is a text file provided below
and return the column of choice
vector<string> bb = x.get_col(1);
but if I tried it the second time
vector<string> bb = x.get_col(2);
it does not clear the member variable from the old data of col(1). how
can I do this. I can pop the elements out from vec.begin() to
vec.end() but there may be a better way for this task all together.

Use "vec.clear ();". Just one question: Why do use a member variable for
creating the vector that you return by value? Why don't you just
declare a local variable inside get_col like the following method:

vector<string> file_info::get_col(int)
{
vector RetVal;
for(int i = (x-1); static_cast<unsigned> (i) < all_cols.size();
i = (i+nCol) )
RetVal.push_back(all_cols);
return RetVal;
}

Since you copy the whole vector anyway, I don't see a reason why you
should keep the vector col around (except for the case that you want to
optimize for the case that the same column gets queries consequtively).

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top