Programowanie Sylwester Arabas prowadzący ćwiczenia: Magdalena Kuich, Krzysztof Piasecki, Łukasz Dulny Wydział Fizyki Uniwersytetu Warszawskiego wykład VIII 15. kwietnia 2015 r.
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 }
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } Boost.units
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } Boost.units quantity<q,t> si::kilograms,...
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } Boost.units quantity<q,t> si::kilograms,... typ auto
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } Boost.units quantity<q,t> si::kilograms,... typ auto C++11
na ostatnim wykładzie... 1 #include <boost/units/systems/si.hpp> 2 #include <boost/units/systems/si/codata_constants.hpp> 3 #include <boost/units/io.hpp> 4 5 namespace bu = boost::units; 6 namespace si = boost::units::si; 7 8 // p = rho R_N2 T 9 bu::quantity<si::mass_density> rho( 10 bu::quantity<si::temperature> T, 11 bu::quantity<si::pressure> p 12 ) 13 { 14 auto M_N2 = 28 * 1e-3 * si::kilograms / si::moles; 15 auto R_N2 = si::constants::codata::r / M_N2; 16 return p / R_N2 / T; 17 } 18 19 int main() 20 { 21 std::cerr << "rho = " << rho( 22 300 * si::kelvins, 23 1000e2 * si::pascals 24 ) << std::endl; 25 } Boost.units quantity<q,t> si::kilograms,... typ auto C++11 ρ powietrza 1 kg m 3
temat na dziś & referencje
referencje: najprostszy przykład hello.cpp 1 int main() 2 { 3 int a = 2; 4 int &b = a; 5 b = 44; 6 return a; 7 } polecenia $ g++ ref.cpp $./a.out $ echo $? 44
referencje: najprostszy przykład hello.cpp 1 int main() 2 { 3 int a = 2; 4 int &b = a; 5 b = 44; 6 return a; 7 } polecenia $ g++ ref.cpp $./a.out $ echo $? 44 referencja nowa etykieta dla istniejącej zmiennej
referencje: najprostszy przykład hello.cpp 1 int main() 2 { 3 int a = 2; 4 int &b = a; 5 b = 44; 6 return a; 7 } polecenia $ g++ ref.cpp $./a.out $ echo $? 44 referencja nowa etykieta dla istniejącej zmiennej... gdzie to się może przydać???
referencje: zastosowania poprawienie czytelności kodu (np. lokalne referencje do elementów złożonych typów danych)
referencje: przykład (x, r, g, b) 1 #include <valarray> 2 #include <iostream> 3 4 int main() 5 { 6 std::valarray<int> rgb; 7 rgb.resize(3); 8 9 for (auto &x : rgb) // C++11 10 std::cin >> x; 11 12 int 13 &r = rgb[0], 14 &g = rgb[1], 15 &b = rgb[2]; 16 17 // por. Wikipedia: Luma (video) 18 std::cout 19 <<.2126*r +.7152*g +.0722*b 20 << std::endl; 21 } $./a.out 110 12 201 46.4806
referencje: przykład (x, r, g, b) 1 #include <valarray> 2 #include <iostream> 3 4 int main() 5 { 6 std::valarray<int> rgb; 7 rgb.resize(3); 8 9 for (auto &x : rgb) // C++11 10 std::cin >> x; 11 12 int 13 &r = rgb[0], 14 &g = rgb[1], 15 &b = rgb[2]; 16 17 // por. Wikipedia: Luma (video) 18 std::cout 19 <<.2126*r +.7152*g +.0722*b 20 << std::endl; 21 } $./a.out 110 12 201 46.4806 C++11 nowa składnia for!
referencje: zastosowania poprawienie czytelności kodu (np. lokalne referencje do elementów złożonych typów danych)
referencje: zastosowania poprawienie czytelności kodu (np. lokalne referencje do elementów złożonych typów danych) umożliwienie modyfikacji argumentów funkcji
referencje: przykład (arg) 1 #include <iostream> 2 3 void func_val(int arg) 4 { 5 arg += 44; 6 } 7 8 void func_ref(int &arg) 9 { 10 arg += 44; 11 } 12 13 int main() 14 { 15 int a = 0; 16 17 func_val(a); 18 std::cout << a << std::endl; 19 20 func_ref(a); 21 std::cout << a << std::endl; 22 23 // func_ref(44); // błąd! 24 } $./a.out 0 44
referencje: przykład (arg) 1 #include <iostream> 2 3 void func_val(int arg) 4 { 5 arg += 44; 6 } 7 8 void func_ref(int &arg) 9 { 10 arg += 44; 11 } 12 13 int main() 14 { 15 int a = 0; 16 17 func_val(a); 18 std::cout << a << std::endl; 19 20 func_ref(a); 21 std::cout << a << std::endl; 22 23 // func_ref(44); // błąd! 24 } $./a.out 0 44 po odkomentowaniu l. 23 func.cpp:23:3: error: no matching function for call to 'func_ref' func_ref(44); // błąd! ^~~~~~~~ func.cpp:8:6: note: candidate function not viable: expects an l-value for 1st argument void func_ref(int &arg) ^ 1 error generated.
referencje: zastosowania poprawienie czytelności kodu (np. lokalne referencje do elementów złożonych typów danych) umożliwienie modyfikacji argumentów funkcji
referencje: zastosowania poprawienie czytelności kodu (np. lokalne referencje do elementów złożonych typów danych) umożliwienie modyfikacji argumentów funkcji uniknięcie tworzenia kopii argumentów funkcji (wydajność!)
Dziękuję za uwagę!