Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)
- #!/bin/bash
- # Renames all .C files to .cc
- for name in *.C; do
- mv ${name} ${name%C}cc
- done
- 2)
- int low = ..., int high = ...
- ostringstream ss;
- ss << "Please input a value between" << low << "and" << high; //packed the whole string inside ss
- string s = ss.str(); // output the string
- 3)
- int n;
- while(true) {
- cout << "Enter Number" <<endl;
- string s;
- cin >> s; // use string, nothing make this fail until EOF
- istringstream ss{s}; // ss treats s as an input
- if (ss >> n) break; // this will succeed only if the stirng ss is a number
- cout << "That's not a number" << endl;
- }
- 4)
- vec operator+(const vec &v1, const vec &v2) {
- vec v {v1.x + v2.x, v1.y + v2.y}};
- return v;
- }
- vec operator*(const int k, const vec &v) {
- return {k*v1.x, k*v1.y}
- }
- vec operator*(const vec &v, const int k) {
- return k * v1;
- }
- struct Grade {
- int mygrade;
- }
- ostream &operator<<(ostream &out, const Grade &g) {
- out << g.mygrade << “%”
- return out;
- }
- istream &operator>>(istream &in, const Grade &g)
- {
- in>>g.mygrade;
- if(g.mygrade<0) g.mygrade=0;
- if(g.mygrade>100) g.mygrade=100;
- return in;
- }
- 5) Copy swap idiom
- #include <utility>
- struct Node{
- void swap(Node &other) {
- using std::swap;
- swap(data, other.data);
- swap(next, other.next)'
- }
- Node &operator(const Node &other){
- Node tmp{other}; // copy ctor, temp is stack allocated
- swap(tmp);
- return *this;
- }
- };
- 4)
- struct BT{
- int data;
- BT *l;
- BT *r;
- }
- BT::BT(const BT &other):data{other.data},{l{other.l?new BT(*other.l):nullptr},
- r{other.r?new BT(*other.r):nullptr}}{}
- BT &BT::operator=(const BT &other):data{other.data}
- {
- if(this==&other) return *this;
- BT *t1= other.l?new BT(other.l):nullptr;
- BT *t2= other.r?new BT(other.r):nullptr;
- delete l;
- delete r;
- left=t1;
- right=t2;
- return *this;
- }
- BT::~BT(){delete left;
- delete right;}
- BT::BT(BT &&other):data{other.data},left{other.left},right{other.right}{other.left=nullprt;
- other.right=nullptr;}
- BT &BT::operator=(BT &&other){
- std::swap(data,other.data)
- std::swap...
- '''
- return *this
- }
- 5) Invariant Example : Node & List
- /// list.h
- class List {
- struct Node; // private nested class
- Node *theList = nullptr;
- public:
- void addToFront(int n);
- int ith(int i);
- ~List();
- // ... etc etc
- };
- // list.cc
- #include "list.h"
- struct List::Node {
- // nested class
- int data;
- Node *next;
- Node(int d, Node *n): { // MIL
- }
- ~Node() {
- delete next;
- }
- };
- void List::addToFront(int n) {
- theList = new Node(n, theList);
- }
- int List::ith(int i) {
- Node *cur = theList;
- for (int j = 0; j < i; j++) {
- cur = cur->next;
- }
- return cur->data;
- }
- List::~List() {
- delete theList;
- }
- Traversal O(n^2)
- 6) Iterator Design Pattern O(N) traversal
- Class List{
- struct Node;
- Node *theList;
- public:
- class Iterators{
- Node *p;
- public:
- explicit Iterator(Node *p) : p{p}{}
- int operator*() const {
- return p->data;
- }
- Iterator& operator++() {
- p = p->next;
- return *this;
- }
- bool operator==(const Iterator &other){
- return p == other.p;
- }
- bool operator!=(const Iterator &others){
- return !(*this == other) note: call operator==
- }
- };//endclassIterator
- Iterator begin(){ return Iterator{thelist};}
- Iterator end() { return Iterator{nullptr}; }
- };
- int main() {
- List lst;
- lst addToFront(1);
- lst addToFront(2);
- int addToFront(3);
- for (List::Iterator it = lst.begin(); it != lst.end(); ++it)
- {
- cout << *it << endl;
- }}
- 7) Friend Example
- Class Vec{
- int x, y;
- public:
- friend std::ostream &operator<<(std::ostream &out, const Vec &v);}
- 8) Throw Catch Fib:
- void fact(int n) {
- if (n == 0) throw 1;
- try {
- fact(n-1);
- }
- catch (int m) {
- throw (n * m);
- }
- }
- int main() {
- int n;
- while (cin >> n) {
- try {
- fact(n);
- }
- catch (int m) {
- cout << m << endl;
- }
- }
- }
- virtual Text &Text::operator=(const Book &other) {
- Text &temp = dynamic_cast<Text&>(other);
- Book::operator=(temp);
- topic = temp.topic;
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement