Commit dbdd3296 authored by Guillaume Anciaux's avatar Guillaume Anciaux
Browse files

Merge branch 'master' of ssh://c4science.ch/source/sp4e into master

parents 44ec750f ab4bf09b
cmake_minimum_required(VERSION 2.6)
add_executable(stl
main.cc
)
set (CMAKE_CXX_STANDARD 17)
add_executable(algorithms
algorithms.cc
)
add_executable(memory
memory.cc
)
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
void print_vector(std::vector<double>& v) {
for (double x : v)
std::cout << x << " ";
std::cout << std::endl;
}
int main() {
unsigned int n = 20; // size of vector
std::vector<double> values(n); // create vector with n numbers
// Filling with zeros
std::fill(values.begin(), values.end(), 0);
print_vector(values);
// Applying std::iota
std::iota(values.begin(), values.end(), 1);
print_vector(values); // should print "1 2 3 4 5 ..."
// Squaring a value
auto square_it = [](double & x) { x *= x; };
double y = 3.;
square_it(y);
std::cout << "3*3 = " << y << std::endl; // should print 9.
// Squaring values of the vector
for (double & y : values)
square_it(y);
print_vector(values); // should print "1 4 9 16 25 ..."
// [resetting values]
std::iota(values.begin(), values.end(), 1);
// Squaring values using std::for_each
std::for_each(values.begin(), values.end(), square_it);
print_vector(values); // should print "1 4 9 16 25 ..."
// Summing the squares
double answer = n*(n+1)*(2*n+1)/6.;
double result = 0; // will contain the result
// capture the result variable by-reference
auto summation = [&result] (double & x) { result += x; };
// apply the summation function
std::for_each(values.begin(), values.end(), summation);
std::cout << result << " = " << answer << std::endl;
// Do the same with std::reduce
result = std::accumulate(values.begin(), values.end(), 0.);
std::cout << result << " = " << answer << std::endl;
// We need to invert the squares
auto inverse = [](double & x) { x = 1. / x; };
std::for_each(values.begin(), values.end(), inverse); // apply inverse
// Sum all squares in reverse order to avoid roundoff errors
result = std::accumulate(values.rbegin(), values.rend(), 0.);
double pi_estimate = std::sqrt(6. * result);
std::cout << pi_estimate << " ~ " << M_PI << std::endl;
return 0;
}
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
#include <array>
/* -------------------------------------------------------------------------- */
using Triplet = std::array<double, 3>;
/* -------------------------------------------------------------------------- */
inline std::ostream & operator <<(std::ostream & stream, Triplet & _this)
{
stream << _this[0] << " " << _this[1] << " " << _this[2];
return stream;
}
/* -------------------------------------------------------------------------- */
int main(int argc, char ** argv){
/*
Exercice 1
*/
if (argc != 2) {
std::cerr << "missing argument: your name" << std::endl;
return EXIT_FAILURE;
}
std::string myName = "Guillaume";
std::string argument = std::string(argv[1]);
if (myName == argument){
std::cout << "You have a nice name" << std::endl;
}
/*
Exercice 2
*/
std::ofstream f("toto.txt");
f << 1.5 << " " << 1.0 << " " << 2.0 << std::endl;
std::ifstream is("toto.txt");
std::string str;
if (is.is_open() == false) {
std::cerr << "cannot open file toto.txt" << std::endl;
throw;
}
getline(is, str);
std::stringstream sstr(str);
double x, y, z;
sstr >> x;
sstr >> y;
sstr >> z;
std::cout << x << " " << y << " " << z << std::endl;
/*
Exercice 3
*/
std::array<double, 3> point = { x, y, z};
for (auto & p: point) {
std::cout << p << " ";
}
std::cout << "\n";
std::vector<double> vect;
int npoints = 100;
for (int i = 0; i < npoints; ++i) {
double step = 2.*M_PI / npoints;
vect.push_back(step*i);
}
std::cout << vect.size() << std::endl;
std::vector< std::pair<double, double> > sin_vect;
for (auto & pi: vect) {
sin_vect.push_back(std::make_pair(pi, sin(pi)) );
}
std::ofstream of("sin_pi.txt");
for (auto & pairs: sin_vect){
of << std::scientific << std::setprecision(20)
<< pairs.first << " " << pairs.second << std::endl;
}
/*
Exercice 5
*/
std::map<std::string,Triplet> map;
Triplet origin_coordinates = {0., 0., 0.};
//Triplet origin_coordinates(0.,0.,0.);
map["sun"] = origin_coordinates;
std::map<std::string,Triplet>::iterator end3 = map.end();
//origin_coordinates.coords[0] = 1.;
origin_coordinates[0] = 1.;
std::map<std::string,Triplet>::iterator it3 = map.find("earth");
if (it3 == end3) map["earth"] = origin_coordinates;
std::set<std::string> keys;
std::cout << std::endl << "The complete map content is:" << std::endl;
for (auto & planet: map ){
auto & key = planet.first;
auto & values = planet.second;
std::cout << key << std::endl;
std::cout << values << std::endl;
keys.insert(key);
}
std::map<std::string,Triplet> map2;
Triplet mercury_coordinates = {0.25, 0., 0.};
map2["mercury"] = mercury_coordinates;
Triplet earth_coordinates = {1.25, 0., 0.};
map2["earth"] = earth_coordinates;
Triplet jupiter_coordinates = {5.25, 0., 0.};
map2["jupiter"] = jupiter_coordinates;
Triplet sun_coordinates = {0., 0., 0.};
map2["sun"] = sun_coordinates;
std::set<std::string> keys2;
std::cout << std::endl << "The complete map 2 content is:" << std::endl;
for (auto & planet: map2) {
auto & key = planet.first;
auto & values = planet.second;
std::cout << key << std::endl;
std::cout << values << std::endl;
keys2.insert(key);
}
std::set<std::string> intersect;
std::set_intersection( keys.begin(), keys.end(), keys2.begin(), keys2.end(),
std::inserter( intersect, intersect.begin() ) );
std::map<std::string, Triplet> map_intersect;
for(auto & key: intersect) {
map_intersect[key] = map2[key];
}
std::cout << std::endl << "The complete intersected map content is:" << std::endl;
for(auto & planet: map_intersect) {
std::cout << planet.first << std::endl;
std::cout << planet.second << std::endl;
}
return EXIT_SUCCESS;
}
#include <memory>
#include <vector>
#include <iostream>
void question1_stack() {
int values[10];
for (int& v : values)
v = 0;
for (int i = 0; i < 10; ++i)
std::cout << values[i] << " ";
std::cout << std::endl;
}
void question1_heap() {
int * values = new int[10];
// We can't use range-for with heap allocated arrays
// the best solution would use a std::vector
for (int i = 0; i < 10; ++i)
values[0] = 0;
for (int i = 0; i < 10; ++i)
std::cout << values[i] << " ";
std::cout << std::endl;
// The memory should be deallocated, otherwise we
// have a dangling pointer
delete[] values;
}
std::vector<int>* stack_allocation(int n) {
std::vector<int> values(n);
return &values; // good compilers show a warning here
}
std::vector<int>* heap_allocation(int n) {
std::vector<int>* values = new std::vector<int>(n);
return values;
}
void question2() {
std::vector<int> * stack_values = stack_allocation(10);
std::vector<int> * heap_values = heap_allocation(10);
// stack_values->size() should fail because memory was freed
// at the end of the function stack_allocation
std::cout << stack_values->size() << ", " << heap_values->size() << std::endl;
// We still need to delete
delete heap_values;
}
void question1_bis() {
std::array<int, 10> stack_values;
for (int& v : stack_values)
v = 0;
std::vector<int> heap_values(10);
for (int& v : heap_values)
v = 0;
// no need to delete, everything is managed
}
std::unique_ptr<std::vector<int>> unique_heap_allocation(int n) {
return std::make_unique<std::vector<int>>(n);
}
void question2_bis() {
std::unique_ptr<std::vector<int>> heap_vector = unique_heap_allocation(10);
std::cout << heap_vector->size() << std::endl;
// no need to delete: the unique pointer will deallocate everything at
// the end of the function
}
void question3() {
auto unique = std::make_unique<std::vector<int>>(10);
std::unique_ptr<std::vector<int>> other = nullptr;
// the move operation will make "unique" invalid
// so that there is ONLY ONE pointer to the resource
// hence "unique_ptr"
other = std::move(unique);
// memory will be deallocated nicely by the pointer "other"
}
void question3_bis() {
auto shared = std::make_shared<std::vector<int>>(10);
std::shared_ptr<std::vector<int>> other = nullptr;
// This works because they are shared pointer
other = shared;
// memory will be deallocated nicely
}
int main() {
question1_stack();
question1_heap();
//question2(); <-- this causes an error
question1_bis();
question2_bis();
question3();
question3_bis();
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment