User Tools

Site Tools


journal:fall2019:mgardne8:week11

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
journal:fall2019:mgardne8:week11 [2019/11/06 22:16]
mgardne8
journal:fall2019:mgardne8:week11 [2019/11/07 00:19]
mgardne8 [NOVEMBER 6, 2019]
Line 1: Line 1:
 =====data week11===== =====data week11=====
 ---- ----
-====MONTH DayYEAR====+====NOVEMBER 62019====
  
 +Something new I learned about this week within C++;
 +
 +*Range-based for Statement*
 +
 +Microsoft c++ documentation describes them as follows:
 +
 +Use the range-based for statement to construct loops that must execute through a "​range",​ which is defined as anything that you can iterate through—for example, std::​vector,​ or any other C++ Standard Library sequence whose range is defined by a begin() and end().
 +
 +They are used in the following format:
 +
 +for ( for-range-declaration : expression )
 +statement
 +
 +I am glad to find that there exists something like this within C++ as it is a lot like pythons 'for x in y' and this is something that I love, and made me sad to live without.
 +
 +I used this recently in an assignment for Han's c++ class in  conjunction with vectors as follows:
 +
 +        for( vector<​int>​ v : vect )
 +        {
 +                total = accumulate( v.begin(), v.end(), total );
 +                count = count + v.size();
 +        }
 +
 +This allowed me to iterate over all the objects within a vector of vectors that contain ints, summing them up and counting the number of elements. (To calculate an average).
 +
 +This has the added benefit of 'just working'​ even when all the vectors within the main vector vary in size.
 +
 +Additionally I nested these style of for loops to create a compact and efficient way of outputting the data for the project. ​
 +        for( string name : names )
 +        {
 +                cout   <<​ endl << name << endl << " ​   Grades: ";
 +                for( int grade : grades.at(i) )
 +                        cout   <<​ grade << " ";
 +                cout   <<​ endl << " ​   Average: " << average(grades.at(i));​
 +                cout   <<​ endl << " ​   LetterG: " << get_let(avgs.at(i)) << endl;
 +                i++;
 +         }
  
  
journal/fall2019/mgardne8/week11.txt · Last modified: 2019/11/07 00:19 by mgardne8