// Compile with -std=c++11 #include #include using namespace std; class pascal_triangle{ vector> data; // This is the actual data void print_row(vector dummy){ for (vector::iterator i = dummy.begin(); i != dummy.end(); ++i) cout<<*i<<" "; cout< 0){ // if the argument is 0 or negative exit immediately vector row; data.resize(dummy); // Theoretically this should work faster than consecutive push_back()s // The first row row.resize(1); row.at(0) = 1; data.at(0) = row; // The second row if (data.size() > 1){ row.resize(2); row.at(0) = 1; row.at(1) = 1; data.at(1) = row; } // The other rows if (data.size() > 2){ for (int i = 2; i < data.size(); i++){ row.resize(i + 1); // Theoretically this should work faster than consecutive push_back()s row.front() = 1; for (int j = 1; j < row.size() - 1; j++) row.at(j) = data.at(i - 1).at(j - 1) + data.at(i - 1).at(j); row.back() = 1; data.at(i) = row; } } } } ~pascal_triangle(){ for (vector>::iterator i = data.begin(); i != data.end(); ++i) i->clear(); // I'm not sure about the necessity of this loop! data.clear(); } void print_row(int dummy){ if (dummy < data.size()) for (vector::iterator i = data.at(dummy).begin(); i != data.at(dummy).end(); ++i) cout<<*i<<" "; cout< get_row(int dummy){ vector result; if (dummy < data.size()) result = data.at(dummy); return result; } }; int main(){ int n; cout<>n; pascal_triangle myptri(n); cout<