Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. 1)
  2. #!/bin/bash
  3. # Renames all .C files to .cc
  4.  
  5. for name in *.C; do
  6. mv ${name} ${name%C}cc
  7. done
  8.  
  9. 2)
  10. int low = ..., int high = ...
  11. ostringstream ss;
  12. ss << "Please input a value between" << low << "and" << high; //packed the whole string inside ss
  13. string s = ss.str(); // output the string
  14.  
  15. 3)
  16. int n;
  17. while(true) {
  18. cout << "Enter Number" <<endl;
  19. string s;
  20. cin >> s; // use string, nothing make this fail until EOF
  21. istringstream ss{s}; // ss treats s as an input
  22. if (ss >> n) break; // this will succeed only if the stirng ss is a number
  23. cout << "That's not a number" << endl;
  24. }
  25.  
  26. 4)
  27. vec operator+(const vec &v1, const vec &v2) {
  28. vec v {v1.x + v2.x, v1.y + v2.y}};
  29. return v;
  30. }
  31.  
  32. vec operator*(const int k, const vec &v) {
  33. return {k*v1.x, k*v1.y}
  34. }
  35.  
  36. vec operator*(const vec &v, const int k) {
  37. return k * v1;
  38. }
  39.  
  40. struct Grade {
  41. int mygrade;
  42. }
  43.  
  44. ostream &operator<<(ostream &out, const Grade &g) {
  45. out << g.mygrade << “%”
  46. return out;
  47. }
  48.  
  49. istream &operator>>(istream &in, const Grade &g)
  50. {
  51. in>>g.mygrade;
  52. if(g.mygrade<0) g.mygrade=0;
  53. if(g.mygrade>100) g.mygrade=100;
  54. return in;
  55. }
  56.  
  57. 5) Copy swap idiom
  58. #include <utility>
  59. struct Node{
  60. void swap(Node &other) {
  61. using std::swap;
  62. swap(data, other.data);
  63. swap(next, other.next)'
  64. }
  65.  
  66. Node &operator(const Node &other){
  67. Node tmp{other}; // copy ctor, temp is stack allocated
  68. swap(tmp);
  69. return *this;
  70. }
  71.  
  72. };
  73.  
  74. 4)
  75. struct BT{
  76. int data;
  77. BT *l;
  78. BT *r;
  79. }
  80. BT::BT(const BT &other):data{other.data},{l{other.l?new BT(*other.l):nullptr},
  81. r{other.r?new BT(*other.r):nullptr}}{}
  82. BT &BT::operator=(const BT &other):data{other.data}
  83. {
  84. if(this==&other) return *this;
  85. BT *t1= other.l?new BT(other.l):nullptr;
  86. BT *t2= other.r?new BT(other.r):nullptr;
  87. delete l;
  88. delete r;
  89. left=t1;
  90. right=t2;
  91. return *this;
  92. }
  93. BT::~BT(){delete left;
  94. delete right;}
  95. BT::BT(BT &&other):data{other.data},left{other.left},right{other.right}{other.left=nullprt;
  96. other.right=nullptr;}
  97. BT &BT::operator=(BT &&other){
  98. std::swap(data,other.data)
  99. std::swap...
  100. '''
  101. return *this
  102. }
  103.  
  104. 5) Invariant Example : Node & List
  105. /// list.h
  106.  
  107. class List {
  108. struct Node; // private nested class
  109. Node *theList = nullptr;
  110. public:
  111. void addToFront(int n);
  112. int ith(int i);
  113. ~List();
  114. // ... etc etc
  115. };
  116.  
  117. // list.cc
  118. #include "list.h"
  119. struct List::Node {
  120. // nested class
  121. int data;
  122. Node *next;
  123. Node(int d, Node *n): { // MIL
  124.  
  125. }
  126. ~Node() {
  127. delete next;
  128. }
  129. };
  130.  
  131. void List::addToFront(int n) {
  132. theList = new Node(n, theList);
  133. }
  134.  
  135. int List::ith(int i) {
  136. Node *cur = theList;
  137. for (int j = 0; j < i; j++) {
  138. cur = cur->next;
  139. }
  140. return cur->data;
  141. }
  142.  
  143. List::~List() {
  144. delete theList;
  145. }
  146. Traversal O(n^2)
  147.  
  148. 6) Iterator Design Pattern O(N) traversal
  149. Class List{
  150. struct Node;
  151. Node *theList;
  152. public:
  153. class Iterators{
  154. Node *p;
  155. public:
  156. explicit Iterator(Node *p) : p{p}{}
  157. int operator*() const {
  158. return p->data;
  159. }
  160. Iterator& operator++() {
  161. p = p->next;
  162. return *this;
  163. }
  164. bool operator==(const Iterator &other){
  165. return p == other.p;
  166. }
  167. bool operator!=(const Iterator &others){
  168. return !(*this == other) note: call operator==
  169. }
  170. };//endclassIterator
  171.  
  172. Iterator begin(){ return Iterator{thelist};}
  173.  
  174. Iterator end() { return Iterator{nullptr}; }
  175. };
  176. int main() {
  177. List lst;
  178. lst addToFront(1);
  179. lst addToFront(2);
  180. int addToFront(3);
  181.  
  182. for (List::Iterator it = lst.begin(); it != lst.end(); ++it)
  183. {
  184. cout << *it << endl;
  185. }}
  186.  
  187. 7) Friend Example
  188. Class Vec{
  189. int x, y;
  190. public:
  191. friend std::ostream &operator<<(std::ostream &out, const Vec &v);}
  192.  
  193. 8) Throw Catch Fib:
  194. void fact(int n) {
  195. if (n == 0) throw 1;
  196. try {
  197. fact(n-1);
  198. }
  199. catch (int m) {
  200. throw (n * m);
  201. }
  202. }
  203.  
  204. int main() {
  205. int n;
  206. while (cin >> n) {
  207. try {
  208. fact(n);
  209. }
  210. catch (int m) {
  211. cout << m << endl;
  212. }
  213. }
  214. }
  215.  
  216. virtual Text &Text::operator=(const Book &other) {
  217. Text &temp = dynamic_cast<Text&>(other);
  218. Book::operator=(temp);
  219. topic = temp.topic;
  220. return *this;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement