Advertisement
bwukki

Untitled

Mar 18th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. //WOO FINAL EXAM NOTES!
  6. //Try to use const whenever appropriate, pass by const reference (const vector<string>&) when throwing vectors around, maybe use size_t for stuff?
  7. using namespace std;
  8.  
  9. class Foo {
  10. public:
  11.     Foo()
  12.     : i{1}, names{vector<string>{"Whatever"}}{
  13.         /*empty body*/
  14.     }
  15.  
  16.     explicit Foo(int x)
  17.     : i{x}{
  18.         /*empty body*/
  19.     }
  20.  
  21.     Foo (int x, const vector<string>& s )
  22.     : i{x}, names{s} {
  23.         if (i<0) {
  24.             i = 0;
  25.         }
  26.         if (names.size() < 1) {
  27.             names.push_back("whatever");
  28.         }
  29.  
  30.     }
  31.     void reality() {
  32.         cout << "i = " << i << " names size: " << names.size() << endl;
  33.         for (i : names) {
  34.             cout << i;
  35.         }
  36.         cout << endl;
  37.     }
  38. private:
  39.     int i{};
  40.     vector<string> names{};
  41. };
  42.  
  43. void testFoo() {
  44.     Foo a{};
  45.     Foo b{3};
  46.     Foo c{5,vector<string>{"abc","def"}};
  47.     a.reality();
  48.     b.reality();
  49.     c.reality();
  50.     vector<Foo> vf1{a,b,c};
  51.     vector<Foo> vf2{Foo{}, Foo{1}, Foo{2, vector<string>{"aaaaylaamo","b","c"}}};
  52.     cout << "============================" << endl;
  53.     for (i : vf2) {
  54.         i.reality();
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     testFoo();
  61.  
  62.     cout << "Hello world!" << endl;
  63.     return 0;
  64. }
  65.  
  66. //Additionally, here's some loop stuff in case you forget:
  67.  
  68. #include <iostream>
  69. #include <vector>
  70.  
  71. using namespace std;
  72.  
  73. int sumInts(vector<int> input) {
  74.   int theSum{0};
  75.   for (int i : input) {
  76.     theSum = theSum+i;
  77.   }
  78.  
  79.   return theSum;
  80. }
  81.  
  82. void readInts(vector<int> &input) {
  83.   int numOfIns{0};
  84.   int numIn{0};
  85.   cin >> numOfIns;
  86.   for (int i = 0; i < numOfIns; i++ ) {
  87.     cin >> numIn;
  88.     input.push_back(numIn);
  89.   }
  90. }
  91.  
  92. int main() {
  93.   vector<int> vals;
  94.   readInts(vals);
  95.   cout << sumInts(vals);
  96. }
  97.  
  98. //and finally here's some string stuff
  99.  
  100. #include <iostream>
  101. #include <string>
  102. #include <vector>
  103.  
  104. using namespace std;
  105.  
  106.  
  107. vector<string> getinput(){
  108.     int numstrings{0};
  109.     cin >> numstrings;
  110.     string input{};
  111.     vector<string> inputs{};
  112.     int i{0};
  113.     while (i < numstrings) {
  114.         cin >> input;
  115.         inputs.push_back(input);
  116.         i++;
  117.     }
  118.  
  119.     return inputs;
  120. }
  121.  
  122. void print(string input) {
  123.     cout << input << endl;
  124. }
  125.  
  126. int testinput(string input) {
  127.     int i{2};
  128.     char a{};
  129.     char b{0};
  130.     char c{0};
  131.     int sum{0};
  132.     if (input.length() < 3) {
  133.             return 0;
  134.     }
  135.     while (i < input.length()) {
  136.         a = input[i-2];
  137.         b = input[i-1];
  138.         c = input[i];
  139.         //cout << "a = " << a << " | b = " << b << " | c = " << c << endl;
  140.         if (a == b && a == c) {
  141.             i++;
  142.             sum++;
  143.         }
  144.         i++;
  145.     }
  146.     return sum;
  147. }
  148.  
  149. int main() {
  150.     vector<string> b{getinput()};
  151.     for (string i : b) {
  152.         cout << testinput(i) << endl;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement