Notes From CS Undergrad Courses FSU
This project is maintained by awa03
string str = "hello";
str
- L Value"Hello"
- R Value, Temporarystring x= findMax(a);
string & y = x;
cout << y << endl;
// Refrence to an R Value
string && str2 = "Hello";
vector <string> v("hello", "world");
cout << randomItem(v) << endl; // L Value
cout << randomItem({"Hello", "World"}) << endl; // R Value
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
double ave( const vector<int> & arr, int n, bool & errorFlag)
{
int sum = 0;
for( int i = 0; i < arr.size( ); ++i )
sum += arr[i];
n = 100;
errorFlag = true;
return ((double)sum)/arr.size();
}
int main( )
{
int nn = 5;
bool err = false;
vector<int> myArray {1, 2, 3, 4, 5};
double average = 0.0;
cout << "Before: average = " << average << ", nn = " << nn << ", err = " << err << endl;
average = ave(myArray, nn, err);
cout << " After: average = " << average << ", nn = " << nn << ", err = " << err << endl;
return 0;
}
Lifeline extended beyond function call for by const reference and reference.
A constructor is called whenever..
Problem with Defaults
// Return the maximum item in the array a
template<typename Comparable>
const Comparable& findMax(const vector<Comparable>& a){
int maxIndex = 0;
for(int i= 1; i < a.size(); i++){
if(a[maxIndex] < a[i]){
maxIndex = i;
}
}
return a[maxIndex];
}
operator ()
#code #functions #pass-by-ref #assignment #l-value #r-value #defaults #memberFunctions #memberVariables #declaration #cpp