CppFinancialMaths 1699985217
CppFinancialMaths 1699985217
2.1 Prerequisites
• Basic understanding of C++ syntax and concepts.
1
std :: vector < double > stock_prices = {120.5 , 121.3 , 119.8};
for ( size_t i = 0; i < stock_prices . size () ; ++ i ) {
std :: cout << " Price : ␣ " << stock_prices [ i ] << std :: endl ;
}
2
2.8 Using Iterators for Traversal
What: Objects that point to elements in a container.
Why: Universal way to traverse containers like vectors, sets, maps.
Tip: Use iterators in loops for accessing/modifying elements.
Example:
for ( auto it = stock_prices . begin () ; it != stock_prices . end () ; ++ it ) {
std :: cout << " Price : ␣ " << * it << std :: endl ;
}
• ”MAP for Matching Assets with Prices”: Use std::map to correlate dates with prices.
• ”SMART pointers for SAFE management”: Smart pointers for automatic memory handling.
• ”SET for Selective Elements”: Use std::set for unique, ordered elements.
• ”LAMBDA for Lightweight Anonymous Blocks”: Lambda expressions for concise code snippets.
3
4 Basic Financial Mathematics Examples in C++
4.1 Calculating Average Price with Vector
std :: vector < double > prices = {50.5 , 51.2 , 52.3};
double sum = std :: accumulate ( prices . begin () , prices . end () , 0.0) ;
double average = sum / prices . size () ;
std :: cout << " Average ␣ Price : ␣ " << average << std :: endl ;