# include <iostream>
# include <initializer_list>
template < typename T >
T average_list ( std:: initializer_list< T> arr)
{
T sum = 0 ;
int num = 0 ;
for ( auto x = arr. begin ( ) ; x != arr. end ( ) ; ++ x, ++ num)
{
sum += * x;
}
return sum / num;
}
int main ( )
{
using namespace std;
auto q = average_list ( { 15.4 , 10.7 , 9.0 } ) ;
cout << q << endl;
cout << average_list ( { 20 , 30 , 19 , 17 , 45 , 38 } ) << endl;
auto ad = average_list < double > ( { 'A' , 70 , 65.33 } ) ;
cout << ad << endl;
return 0 ;
}
# include <iostream>
# include <string>
# include <utility>
using namespace std;
class Cpmv
{
public :
struct Info
{
string qcode;
string zcode;
} ;
private :
Info * pi;
public :
Cpmv ( ) ;
Cpmv ( string q, string z) ;
Cpmv ( const Cpmv & cp) ;
Cpmv ( Cpmv && mv) ;
~ Cpmv ( ) ;
Cpmv & operator = ( const Cpmv & cp) ;
Cpmv & operator = ( Cpmv && mv) ;
Cpmv operator + ( const Cpmv & obj) const ;
void Display ( ) const ;
} ;
Cpmv :: Cpmv ( )
{
cout << "Default constructor called.\n" ;
pi = new Info;
pi-> qcode = "none" ;
pi-> zcode = "none" ;
}
Cpmv :: Cpmv ( string q, string z)
{
cout << "String constructor called.\n" ;
pi = new Info;
pi-> qcode = q;
pi-> zcode = z;
}
Cpmv :: Cpmv ( const Cpmv & cp)
{
cout << "Copy constructor called.\n" ;
pi = new Info;
pi-> qcode = cp. pi-> qcode;
pi-> zcode = cp. pi-> zcode;
}
Cpmv :: Cpmv ( Cpmv && mv)
{
cout << "Move constructor called.\n" ;
pi = mv. pi;
mv. pi = nullptr ;
}
Cpmv :: ~ Cpmv ( )
{
delete pi;
}
Cpmv & Cpmv:: operator = ( const Cpmv & cp)
{
if ( this != & cp)
{
delete pi;
pi = new Info;
pi-> qcode = cp. pi-> qcode;
pi-> zcode = cp. pi-> zcode;
}
return * this ;
cout << "Copy assignment operator called.\n" ;
}
Cpmv & Cpmv:: operator = ( Cpmv && mv)
{
if ( this != & mv)
{
delete pi;
pi = mv. pi;
mv. pi = nullptr ;
}
return * this ;
cout << "Move assignment operator called.\n" ;
}
Cpmv Cpmv:: operator + ( const Cpmv & obj) const
{
return Cpmv ( pi-> qcode + obj. pi-> qcode, pi-> zcode + obj. pi-> zcode) ;
}
void Cpmv :: Display ( ) const
{
if ( pi != nullptr )
{
cout << "The qcode is " << pi-> qcode << endl;
cout << "The zcode is " << pi-> zcode << endl;
}
else
{
cout << "Cpmv::pi pointer is nullptr" << endl;
}
cout << endl;
return ;
}
int main ( )
{
Cpmv temp1;
cout << "temp1:\n" ;
temp1. Display ( ) ;
Cpmv temp2 ( "black" , "clover" ) ;
cout << "temp2:\n" ;
temp2. Display ( ) ;
Cpmv temp3 ( temp1) ;
cout << "temp3:\n" ;
temp3. Display ( ) ;
cout << "temp4 = temp1:\n" ;
Cpmv temp4;
temp4 = temp1;
cout << "temp4:\n" ;
temp4. Display ( ) ;
cout << "temp1:\n" ;
temp1. Display ( ) ;
cout << "temp5 = move(temp2):\n" ;
Cpmv temp5;
temp5 = move ( temp2) ;
cout << "temp5:\n" ;
temp5. Display ( ) ;
cout << "temp2:\n" ;
temp2. Display ( ) ;
cout << "temp6 = temp3 + temp4:\n" ;
Cpmv temp6 = temp3 + temp4;
cout << "temp6:\n" ;
temp6. Display ( ) ;
return 0 ;
}
# include <iostream>
using namespace std;
long double sum_value ( )
{
return 0 ;
}
template < typename T >
long double sum_value ( const T & value)
{
return value;
}
template < typename T , typename . . . Args>
long double sum_value ( const T & value, const Args & . . . args)
{
return value + sum_value ( args. . . ) ;
}
int main ( )
{
int n = 14 ;
double x = 2.71828 ;
long y = 100000 ;
char ch = 'x' ;
cout << sum_value ( n, x, y, ch, 'K' , 66 , 33.67 ) ;
return 0 ;
}
# include <iostream>
# include <list>
# include <algorithm>
using namespace std;
int main ( )
{
int vals[ 10 ] = { 50 , 100 , 90 , 180 , 60 , 210 , 415 , 88 , 188 , 201 } ;
list< int > yadayada ( vals, vals + 10 ) ;
list< int > etcetera ( vals, vals + 10 ) ;
auto lambda_outint = [ ] ( int n)
{ cout << n << " " ; } ;
cout << "Original lists:\n" ;
for_each ( yadayada. begin ( ) , yadayada. end ( ) , lambda_outint) ;
cout << endl;
for_each ( etcetera. begin ( ) , etcetera. end ( ) , lambda_outint) ;
cout << endl;
yadayada. remove_if ( [ ] ( int n)
{ return n > 100 ; } ) ;
etcetera. remove_if ( [ ] ( int n)
{ return n > 200 ; } ) ;
cout << "Trimmed lists:\n" ;
for_each ( yadayada. begin ( ) , yadayada. end ( ) , lambda_outint) ;
cout << endl;
for_each ( etcetera. begin ( ) , etcetera. end ( ) , lambda_outint) ;
cout << endl;
return 0 ;
}