Created
April 9, 2014 16:18
-
-
Save FranklinChen/10288187 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Illustrate disciplined C++ code | |
// http://www.cplusplus.com/reference/vector/vector/ | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
class Foo { | |
public: | |
virtual ~Foo() { | |
cout << "destroying foo" << endl; | |
} | |
}; | |
auto main() -> int { | |
try { | |
// Prevent memory leak. | |
const auto foo = make_shared<Foo>(); | |
const vector<int> v { 3, 1, 4 }; | |
// Note that v[i] does not do bounds checking, but v.at(i) does. | |
// http://www.cplusplus.com/reference/vector/vector/at/ | |
for (int i = 0; i < 4; i++) { | |
const auto x = v.at(i); | |
cout << "v at " << i << " is " << x << endl; | |
} | |
} | |
catch(const exception &e) { | |
cout << "caught exception: "<< e.what() << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: