Confusion Regarding Lists and Couples

So, I'm experimenting with trying to add first and last names to the double list. I have various text files of different lengths with the format "line, line", and I use 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 is that, firstly, the last item in the list is not added. how each file just skips the end line, so it gets a little confusing.

also, I do "mylist.size ()" so that all the names are added, and this is confusing because, say, for a text file containing 99 names, that is 99 lines of text, it will say (not forgetting, that he reads only 98 due to the absence of the last line) that the list is 48 in size.

48? - , , , , , .

.

!

+5
2

, , , :

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

, list 2 (floor(number_of_lines/2), 48), list . ?

-, std::ifstream::operator>>(std::string&) , . \n . , stuff.first "one,two", stuff.second, "three,four". list. , {"five,six","seven,eight"}. operator>> "nine,ten", , while , .

, first pair, , , , .

- std::getline , :

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);
}

std::vector, std::list.

+5

→ > ifstream . , , , , - .

getline "" .

-1

All Articles