473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Vector Help Please!!

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
6 1880

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
6
by: Mattias Brändström | last post by:
Hello all! I am trying to write code that allows me to initialise one of my classes inline (with a vector like structure). Inline might not be the best term to use here but I can't think of any...
9
by: John Guo | last post by:
Hi all, Please help see why this snippet does not compile. Thanks a lot. John #include <string> #include <vector> namespace PatternMsg { std::vector<std::string> msg(17);
3
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
5
by: streamkid | last post by:
i have a class table, which has a vector of records(-db). i 'm trying to remove an element, but it doesn't seem to work.. i read this http://www.cppreference.com/cppvector/erase.html] and that's...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.