By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

Vector Help Please!!

P: n/a
Hello,

I am writing a program that will read each line of a file into a vector of
vectors. This data will then be analysed. Here is what i have done:

typedef vector<string> lines;
...
vector<lines> SourceVector;
string one_line;

while (getline(inFile, one_line, '\n'))
{
vector<string> text;
text.push_back(one_line);
SourceVector.push_back(text);
cout << " " << endl;
cout << one_line << endl;
}

all this does, is it reads each line into a vector. I want it to read each
'data' of the line into an element of the vector. so, if my file is:

A 0.2 0.4 0.5
B 0.6 0.7 0.9

then 'A' will be in index 0, '0.2' will be in index 1 and so on...
'B' will be in index 0 of another vector etc.

can someone please help me? how can i do this?

any help given would be greatly appreciated.

Thanx!
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

kittykat wrote:
Hello,

I am writing a program that will read each line of a file into a vector of vectors. This data will then be analysed. Here is what i have done:

typedef vector<string> lines;
..
vector<lines> SourceVector;
string one_line;

while (getline(inFile, one_line, '\n'))
{
vector<string> text;
text.push_back(one_line);
This pushes the entire line into 'text'.

SourceVector.push_back(text);


This pushes a vector containing 1 element (the entire line) into
'SourceVector'.

What you want to do here is really push each "token" into the 'text'
vector. For example:

std::istringstream lineStream(one_line);
while (lineStream.good()) {
std::string token;
lineStream >> token;
text.push_back(token);
}
SourceVector.push_back(text);

You can play around to make this more efficient, but this is the
general idea to solve your problem.

Hope this helps,
-shez-

Jul 23 '05 #2

P: n/a
Thanx!! That compiles fine. But how can i print it so that i can see that
its doing the job? i have this:

while (lineStream.good())
{
lineStream >> token;
text.push_back(token);
}

SourceVector.push_back(text);
cout << token << endl;
cout << " " << endl;
cout << one_line << endl;

but only a blank screen comes up. Any ideas?

Jul 23 '05 #3

P: n/a
kittykat wrote:

Thanx!! That compiles fine. But how can i print it so that i can see that
its doing the job? i have this:

while (lineStream.good())
{
lineStream >> token;
text.push_back(token);
}

SourceVector.push_back(text);
cout << token << endl;
cout << " " << endl;
cout << one_line << endl;

but only a blank screen comes up. Any ideas?

Please. If you want us to help you, then an optimium
strategy is to post a complete, compileable program,
especially if it is short. If it is not short, then
shorten your program, but make sure that it still is
compilable such that we can, in a worst case, cut&paste
it into our own development environments. There are simply
to many things you can do wrong. Things we cannot guess
by you posting just a few snippets.

The section should read something like this

while( getline( inFile, one_line ) ) {

// it is always a good idea during debugging to immediatly
// output what was read. This way errors during the reading
// phase turn up early.
cout << "Read from file: " << one_line << endl;

istringstream lineStream( one_line );
string token;
while( lineStream >> token ) {
// again: it is a good idea for debugging purposes
// to output immediatly what has been read
cout << " Token: " << token;
text.push_back( token );
}

SourceVector.push_back( text );
}

To output the whole SourceVector you do something like this:

for( int i = 0; i < SourceVector.size(); ++i ) {
cout << "Next line:" << endl;

for( j = 0; j < SourceVector[i].size(); ++j ) {
cout << SourceVector[i][j] << endl;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

P: n/a
ok, here is my complete code (which was modified because of Karls help)

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <istream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;

typedef vector<string> lines;

int main()
{
ifstream inFile;
inFile.open("data.txt");

if (inFile.fail())
{ cout <<"\nThe file was not successfully opened" << endl;
exit(1);
}

vector<lines> SourceVector;
vector<string> text;
string one_line, token;

istringstream lineStream(one_line);

while( getline( inFile, one_line ) )
{
cout << "Read from file: " << one_line << endl;
istringstream lineStream( one_line );
string token;

while( lineStream >> token )
{
cout << " Token: " << token << endl;
text.push_back( token );
}

SourceVector.push_back( text );
}
for( int i = 0; i < SourceVector.size(); ++i )
{
cout << "Next line:" << endl;
for( int j = 0; j < SourceVector[i].size(); ++j )
{
cout << SourceVector[i][j];
}
}

system("PAUSE");
return 0;
}

when i print the whole SourceVector, it first prints the first line, then
the first and second line, then the first, second and third line all stuck
together. Does this mean that the file stored in the vector of vectors is
stored in this way??

I would like to know because my next step is to compare the SourceVector
with another vector. I have written that bit of the code, but it only
works correctly for the first element in the vector.

This will be added to the code shown above:

vector<string> DNApattern;
string pattern;

cout << "Please enter the pattern you are searching for: " << endl;
cin >> pattern;

DNApattern.push_back(pattern); // reads pattern into a DNApattern
vector.
cout << " " << endl;
cout << pattern << endl;

//Compare text with pattern
vector<string>::iterator iter1, iter2;
iter1 = DNApattern.begin();
iter2 = text.begin();

while (iter1 != text.end() && iter2 != DNApattern.end())
{
bool match;
if (*iter1 == *iter2)
{ match = true;
cout << "YAAAAY" << endl; }
else
{ match = false;
cout << "False" << endl; }
}

The "data.txt" file looks something like this:

A 0.2 0.3 0.4
B 0.4 0.7 0.8

and so on...

So, my code only works if the user enters A. I'm guessing that this is
because the file is not stored in the way that I need it to be stored. Can
anyone help me out please?

Jul 23 '05 #5

P: n/a
kittykat wrote:


when i print the whole SourceVector, it first prints the first line, then
the first and second line, then the first, second and third line all stuck
together. Does this mean that the file stored in the vector of vectors is
stored in this way??


Yes. The question is: Do you want it to be that way or do you
prefer that each SourceVector item holds only one line
of tokens.

If the later is the case you should familiarize yourself with
the thought that there is some bug in your program.

Since the only thing that gets added to SourceVector is the vector
text, you probably clear this vector to empty before you process
the next line read from the file. The simplest thing is to
move the definition of that vector into the outer while loop. This
way text gets destroyed and recreated in each loop iteration and
thus starts out empty

vector<lines> SourceVector;
string one_line, token;

istringstream lineStream(one_line);

while( getline( inFile, one_line ) )
{
cout << "Read from file: " << one_line << endl;
istringstream lineStream( one_line );
string token;

vector<string> text;
while( lineStream >> token )
{
cout << " Token: " << token << endl;
text.push_back( token );
}

SourceVector.push_back( text );
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

P: n/a
Thanx!! I've tried your way, and now it only prints out the first line. All
i have to do now is do a loop, so that it does the same thing for each
line. :)

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.