auto/auto3.cc - Same - without std:: - with using namespace std. | Lecture 2 - slide 23 : 29 Program 2 |
// Same as above, with a using directive for the std namespace. Actually it illustrates a bad use of a using directive. #include <iostream> #include <vector> #include <cmath> // abs #include "point.h" using namespace std; // In this variant, we do not qualify each name from the standard namespace with std:: // Illustration of alternative return type - best if return type depends on parameters. auto find_diagonal_points(vector<Point> points) -> vector<Point>{ vector<Point> res; for(auto p: points) if(abs(p.getx())==abs(p.gety())) res.push_back(p); return res; } void without_auto(){ int i{5}; vector<Point> points = {Point{1,1}, Point{1,2}, Point{-1,1}, Point{-2,2}}; vector<Point> diagonal_points = find_diagonal_points(points); for(Point p: diagonal_points) cout << p << endl; } void with_auto(){ auto i = 5; // initialize auto's with =, not {}. auto points = vector<Point>{{Point{1,1}, Point{1,2}, Point{-1,1}, Point{-2,2}}}; auto diagonal_points = find_diagonal_points(points); for(auto p: diagonal_points) cout << p << endl; } int main () { without_auto(); cout << endl; with_auto(); }