Caminhoneiro

Comparing loops

Jul 4th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. // Comparing.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <algorithm>
  7. #include <numeric>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     vector<int> a{ 1, 2, 3, 4 , 5};
  15.     vector<int> b{ 1, 2, 0, 4 };
  16.  
  17.     bool same = a.size()==b.size();
  18.     for (size_t i = 0; i < a.size() && same; i++)
  19.     {
  20.         if (a[i] != b[i])
  21.         {
  22.             same = false;
  23.         }
  24.     }
  25.  
  26.     same = equal(begin(a), end(a), begin(b), end(b));
  27.    
  28.     auto firstchange = mismatch(begin(a), end(a), begin(b));
  29.  
  30.     int avalue = *(firstchange.first);  //Because both are iterators you put put the star before them
  31.     int bvalue = *(firstchange.second);
  32.     auto distance = firstchange.first - begin(a);
  33.  
  34.     int total = 0;
  35.     for (int i : a)
  36.     {
  37.         total += i;
  38.     } //Get the total of values on the vector
  39.  
  40.     //total = accumulate(begin(a), end(a), 0);//Get the total of values on the vector
  41.     total = accumulate(begin(a), end(a), 0,
  42.         [](int total, int i) {if (i % 2 == 0) return total + i; return total; }); //The if(i % 2 == 0) check if the number is even or not (Even = par) //THen return only the even numbers, otherwise will return the original total
  43.     total = accumulate(begin(a), end(a), 1,
  44.         [](int total, int i) {return total * i; });//Multiply the sequence
  45.  
  46.     vector<string> words{ "one","two","three" };
  47.     auto allwords = accumulate(begin(words), end(words), string{});
  48.     int length = allwords.size();
  49.     allwords = accumulate(begin(words), end(words), string{"Words:"}, [](const string& total, string& s) {return total + " " + s; });
  50.     length = allwords.size();
  51.  
  52.     string s = accumulate(begin(a), end(a), string{"The numbers are:"},
  53.         [](const string& total, int i) {return total + " " + to_string(i); });
  54.  
  55.     b = a;
  56.     for (auto it = begin(b); it != end(b); it++)
  57.     {
  58.         *it = 0;
  59.     }
  60.  
  61.     for (auto& i : b) //The auto& with the reference to acctually change the value on b and not create something just for the loop
  62.     {
  63.         i = 1;
  64.     }
  65.  
  66.     for_each(begin(b), end(b), [](int& elem) {elem = 2; });//Int reference on the same way to make the change on the above as well
  67.  
  68.     b = a;
  69.     auto firstthree = find(begin(b), end(b), 3);
  70.     for_each(firstthree, end(b), [](int& elem) {elem = 0; });
  71.  
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment