Difference between revisions of "Effective Analysis Programming Part 1"
From Gridkaschool
(→Standard Template Library) |
|||
Line 1: | Line 1: | ||
==Literature on C++== |
==Literature on C++== |
||
− | + | ===books=== |
|
* The C++ Programming Language, Bjarne Stroustrup |
* The C++ Programming Language, Bjarne Stroustrup |
||
* Effective C++, Scott Meyers |
* Effective C++, Scott Meyers |
||
Line 9: | Line 9: | ||
* Exceptional C++, Herb Sutter |
* Exceptional C++, Herb Sutter |
||
* More Exceptional C++, Herb Sutter |
* More Exceptional C++, Herb Sutter |
||
− | + | ===links=== |
|
* http://www.stroustrup.com/bs_faq2.html |
* http://www.stroustrup.com/bs_faq2.html |
||
* http://www.cplusplus.com/reference/ |
* http://www.cplusplus.com/reference/ |
||
Line 16: | Line 16: | ||
==Standard Template Library== |
==Standard Template Library== |
||
− | + | ===map=== |
|
+ | ''exercise 1'' |
||
#include <map> |
#include <map> |
||
#include <iostream> |
#include <iostream> |
||
Line 50: | Line 51: | ||
* What is the output of this code, i.e. which constructors get called? |
* What is the output of this code, i.e. which constructors get called? |
||
* What is the advantage of insert? |
* What is the advantage of insert? |
||
+ | ''exercise 2'' |
||
+ | #include <map> |
||
+ | #include <iostream> |
||
+ | #include <complex> |
||
+ | #include <cmath> |
||
+ | |||
+ | using std::map; |
||
+ | using std::cout; |
||
+ | using std::endl; |
||
+ | using std::complex; |
||
+ | |||
+ | namespace gks { |
||
+ | struct complex_compare { |
||
+ | bool operator()(complex<double> const& a, complex<double> const& b) { |
||
+ | return std::real(a)!=std::real(b) ? std::real(a)<std::real(b) : std::imag(a)<std::imag(b); |
||
+ | } |
||
+ | }; |
||
+ | |||
+ | /* |
||
+ | exponential of complex number; caches results in map |
||
+ | */ |
||
+ | complex<double> exp(complex<double> const& x) { |
||
+ | static map<complex<double>,complex<double>,complex_compare> cache; |
||
+ | map<complex<double>,complex<double>,complex_compare>::iterator it=cache.find(x); |
||
+ | return it==cache.end() ? cache[x]=std::exp(x) : it->second; |
||
+ | } |
||
+ | |||
+ | } |
||
+ | |||
+ | int main() { |
||
+ | complex<double> z1 = gks::exp(complex<double>(0.,1.)); |
||
+ | complex<double> z2 = gks::exp(complex<double>(1.,1.)); |
||
+ | complex<double> z3 = gks::exp(complex<double>(0.,1.)); |
||
+ | complex<double> z4 = gks::exp(complex<double>(0.,-1.)); |
||
+ | |||
+ | cout<<z1<<endl; |
||
+ | cout<<z2<<endl; |
||
+ | cout<<z3<<endl; |
||
+ | cout<<z4<<endl; |
||
+ | |||
+ | return 0; |
||
+ | } |
Revision as of 09:02, 16 August 2013
Literature on C++
books
- The C++ Programming Language, Bjarne Stroustrup
- Effective C++, Scott Meyers
- More Effective C++: 35 New Ways to Improve Your Programs and Designs
- Modern C++ Design, Andrei Alexandrescu
- The C++ Standard Library, Nicolai M. Josuttis
- C++ Templates, David Vanevoorde, Nicolai M. Josuttis
- Exceptional C++, Herb Sutter
- More Exceptional C++, Herb Sutter
links
- http://www.stroustrup.com/bs_faq2.html
- http://www.cplusplus.com/reference/
- http://www.parashift.com/c++-faq/
- http://herbsutter.com/gotw/
Standard Template Library
map
exercise 1
#include <map> #include <iostream> using std::map; using std::cout; using std::endl; struct A { A(): _p(0) { cout<<"A::A()"<<endl; } A(int p): _p(p) { cout<<"A::A(int p)"<<endl; } A& operator=(A const& other) { _p=other._p; cout<<"A& A::operator=A const& other)"<<endl; return *this; } int _p; }; int main() { map<int,A> m; m[1]=A(42); m.insert(std::make_pair(4,A(11))); for(std::pair<int,A> p: m) { cout<<"m["<<p.first<<"]: "<<p.second._p<<'\n'; } return 0; }
- What is the output of this code, i.e. which constructors get called?
- What is the advantage of insert?
exercise 2
#include <map> #include <iostream> #include <complex> #include <cmath> using std::map; using std::cout; using std::endl; using std::complex; namespace gks { struct complex_compare { bool operator()(complex<double> const& a, complex<double> const& b) { return std::real(a)!=std::real(b) ? std::real(a)<std::real(b) : std::imag(a)<std::imag(b); } }; /* exponential of complex number; caches results in map */ complex<double> exp(complex<double> const& x) { static map<complex<double>,complex<double>,complex_compare> cache; map<complex<double>,complex<double>,complex_compare>::iterator it=cache.find(x); return it==cache.end() ? cache[x]=std::exp(x) : it->second; } } int main() { complex<double> z1 = gks::exp(complex<double>(0.,1.)); complex<double> z2 = gks::exp(complex<double>(1.,1.)); complex<double> z3 = gks::exp(complex<double>(0.,1.)); complex<double> z4 = gks::exp(complex<double>(0.,-1.)); cout<<z1<<endl; cout<<z2<<endl; cout<<z3<<endl; cout<<z4<<endl; return 0; }