CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015

    istream_iterator with for loop

    I have added the following function, just for learning about stls . Not professional code, so please bear with me

    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include "string"
    #include <vector>
    #include "sstream"
    #include "iterator"
    #include "algorithm"
    using namespace std;
    // Write your Student class here
    class Student
    	vector<int> scores;
    	int sum;
    	Student() :scores(0), sum(0) {}
    	int calculateTotalScore() { return sum; }
    	void input()
    		string str;
    		std::getline(cin, str);
    		if (str.empty())
    			std::getline(cin, str);
    		std::stringstream ss(str);
    		copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(scores));
    		for_each(scores.begin(), scores.end(), [this](int p) {sum += p; });
    		//for_each(istream_iterator<int>(ss), istream_iterator<int>(), [this](int p) {sum += p; });
    int main() {
    	int n; // number of students
    	cin >> n;
    	Student *s = new Student[n]; // an array of n students
    	for (int i = 0; i < n; i++) {
    	// calculate kristen's score
    	int kristen_score = s[0].calculateTotalScore();
    	// determine how many students scored higher than kristen
    	int count = 0;
    	for (int i = 1; i < n; i++) {
     		int total = s[i].calculateTotalScore();
    		if (total > kristen_score) {
    	// print result
    	cout << count;
    	return 0;
    Here i have a doubt why the red commented line doesnot work !!!.
    Also, my doubt about cin and getline ignoring newline..I am bit confused about the input delimiters

    like lets say:


    How the getline and cin work ? I guess entire input is read as buffer and assigned to each getline/cin ?

    thanks a lot

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: istream_iterator with for loop

    That line works just fine! That's not the problem. If you're reading from a file and you read all the data, what happens to the file state? When you get to the end of the file, can you read again from the beginning? A string stream is treated 'like a file stream'.

    for_each(istream_iterator<int>(ss), istream_iterator<int>(), [this](int p) {sum += p; });

    re getline/>>

    getline() reads all characters up to the specified delim char (default \n) and removes the delim char.

    >> ignores all prefix white space and reads the succeeding chars up to the next white space char. It doesn't remove the white space char.
    Last edited by 2kaud; October 12th, 2020 at 08:30 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.2.4)

  3. #3
    Join Date
    May 2015

    Re: istream_iterator with for loop

    Thanks a lot kaud

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width