Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusion about lists and pairs

So I'm experimenting with trying to add first and last names into a double linked list. I have a various text files of different lengths with the format "string, string", and am using list> to store my data.

I am using this code:

typedef std::list< std::pair<string,string> > listPair;

...

list<pair<string, string> > mylist;
ifstream myFile; 
myFile.open("20.txt");

pair<string, string> stuff;
while (myFile >> stuff.first >> stuff.second)
{
    mylist.push_back(stuff);
}

listPair::iterator iter = mylist.begin();

for(;iter != mylist.end();iter++)
{
    string s = (*iter).first;
    cout << s << endl;
    string c = (*iter).second;
    cout << c << endl;
}

now the problem i'm having is that firstly, the last item in the list is not being added. like every file just misses the end line, so that's a little confusing.

also, I'm doing a "mylist.size()" to ensure all the names have been added, and it's confusing me because say for a text file containing 99 names, i.e 99 lines of text, it will say (not forgetting it only reads in 98 due to missing the last line) that the list has size 48.

WHY 48? Is it something to do because I have done pairs, which still would not make sense as if it was not reading in pairs there would actually be double the about, since the pairs are just to take the first and last name as one value.

Mind boggling to me.

once again thanks for all your help!

like image 840
aspirant_sensei Avatar asked Oct 05 '22 05:10

aspirant_sensei


1 Answers

I have a feeling your file doesn't actually have spaces between the values as you've described, so it looks like this:

one,two
three,four
five,six
seven,eight
nine,ten

If you were to run your program on this, the size of the list will be 2 (floor(number_of_lines/2), which for you would give 48) and the last line won't have been put in the list at all. Why?

Firstly, each call to std::ifstream::operator>>(std::string&) will extract up until it hits some white space. In this case, the first white space on the first line is the \n at the end of it. So on the first iteration, stuff.first will be "one,two" and then the next line will be read into stuff.second, making it "three,four". This is then pushed into the list. The next two lines are read in the same way, giving you the pair {"five,six","seven,eight"}. On the next iteration, the first operator>> will extract "nine,ten" and the second will fail, causing the while condition to end and the last line to be discarded.

Even if you did have spaces, you would end up with commas in the first of every pair, which is certainly not what you want.

The nicer way to approach a problem like this is to use std::getline to extract a line, and then parse that line as appropriate:

std::string line;
std::pair<std::string, std::string> line_pair;
while (std::getline(myFile, line)) {
  std::stringstream line_stream(line);
  std::getline(line_stream, line_pair.first, ',');
  std::getline(line_stream, line_pair.second);
  mylist.push_back(line_pair);
}

I also recommend using std::vector unless you have a good reason to use std::list.

like image 77
Joseph Mansfield Avatar answered Oct 07 '22 19:10

Joseph Mansfield