Advertisement
Kofa_Joh

07.07.2017

Jul 7th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <sstream>
  5. #include <string>
  6. #include <iostream>
  7. #include <cmath>
  8. #include <exception>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <iterator>
  12.  
  13. class CalcException : public std::exception
  14. {
  15. public:
  16.     char const* what() const override
  17.     {
  18.         return "Invalid argument";
  19.     }
  20. };
  21.  
  22. double CalcSqrt(double value)
  23. {
  24.     if (value < 0)
  25.     {
  26.         throw CalcException();
  27.     }
  28.     return sqrt(value);
  29. }
  30.  
  31. void Sort(int* array, int count)
  32. {
  33.  
  34. }
  35.  
  36. int main()
  37. {
  38.     /*std::string str = "type:student name:Ivanov rating:8";
  39.     std::istringstream is(str);
  40.     std::string buf;
  41.     while (!is.eof())
  42.     {
  43.         is >> buf;
  44.         std::istringstream is2(buf);
  45.         char part[1024];
  46.         is2.getline(part, 1024, ':');
  47.         std::cout << "key=" << part;
  48.         is2.getline(part, 1024, ':');
  49.         std::cout << ", value=" << part << std::endl;
  50.     }
  51.  
  52.     double val = 5;
  53.     try
  54.     {
  55.         double res = CalcSqrt(val);
  56.         std::cout << res << std::endl;
  57.     }
  58.     catch (std::exception& ex)
  59.     {
  60.         std::ostringstream os;
  61.         os << "Error in function CalcSqrt()" <<
  62.             ", argument: " << val << ", :" <<
  63.             ex.what();
  64.         std::cout << os.str() << std::endl;
  65.     }
  66.  
  67.     std::ostringstream os1;
  68.     os1 << 5 << "+" << 6 << "=" << (5 + 6);
  69.     std::cout << os1.str() << std::endl;
  70.  
  71.     std::istringstream is1("123");
  72.     int a;
  73.     is1 >> a;
  74.     std::cout << a + 1 << std::endl; */
  75.  
  76.     std::vector<int> v;
  77.     for (int i = 1; i < 10; ++i)
  78.     {
  79.         v.push_back(i);
  80.     }
  81.  
  82.     std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  83.  
  84.     std::vector<int> v1 = { 1, 19, 20 };
  85.     std::vector<int> v2(std::move(v1));
  86.     std::copy(v2.begin(), v2.end(),
  87.         std::ostream_iterator<int>(std::cout, " "));
  88.  
  89.     std::cout << std::endl;
  90.  
  91.     std::vector<int> v3(v.begin(), v.begin() + 5);   //random_access_iter
  92.     std::copy(v3.begin(), v3.end(),
  93.         std::ostream_iterator<int>(std::cout, " "));
  94.  
  95.     std::cout << std::endl;
  96.  
  97.     std::cout << v3.front() << " " << v3.back()
  98.         << std::endl;
  99.  
  100.     std::cout << v3.size() << " " << v3.capacity()
  101.         << std::endl;
  102.     v3.push_back(0);
  103.     std::cout << v3.size() << " " << v3.capacity()
  104.         << std::endl;
  105.  
  106.     v3.reserve(20);
  107.     v3.resize(15);
  108.     std::copy(v3.begin(), v3.end(),
  109.         std::ostream_iterator<int>(std::cout, " "));
  110.     std::cout << std::endl;
  111.     std::cout << v3.size() << " " << v3.capacity()
  112.         << std::endl;
  113.     std::cout << std::endl;
  114.  
  115.     std::vector<int>::reverse_iterator it = v3.rbegin();
  116.     for (; it != v3.rend(); ++it)
  117.     {
  118.         std::cout << *it << " ";
  119.     }
  120.     std::cout << std::endl;
  121.  
  122.     std::copy(v3.rbegin(), v3.rend(),
  123.         std::ostream_iterator<int>(std::cout, " "));
  124.     std::cout << std::endl;
  125.  
  126.     int* array = &v3[0]; //указатель на первый элемент вектра
  127.     int* array1 = &v3.front();
  128.     int* array2 = v3.data();
  129.     Sort(v3.data(), v3.size());
  130.  
  131.     v3.insert(v3.begin() + 1, 100);
  132.     std::copy(v3.begin(), v3.end(),
  133.         std::ostream_iterator<int>(std::cout, " "));
  134.     std::cout << std::endl;
  135.     std::vector<int>::iterator it1 = v3.insert(v3.begin() + 5, 5, 500);
  136.     std::copy(v3.begin(), v3.end(),
  137.         std::ostream_iterator<int>(std::cout, " "));
  138.     std::cout << std::endl;
  139.  
  140.     std::cout << std::distance(v3.begin(), it1)
  141.         << std::endl;
  142.     it1 = v3.erase(it1);
  143.     std::copy(v3.begin(), v3.end(),
  144.         std::ostream_iterator<int>(std::cout, " "));
  145.     std::cout << std::endl;
  146.  
  147.     v3.erase(it1, it1 + 5);
  148.     std::copy(v3.begin(), v3.end(),
  149.         std::ostream_iterator<int>(std::cout, " "));
  150.     std::cout << std::endl;
  151.  
  152.     for (std::vector<int>::iterator i = v3.begin(); i != v3.end();)
  153.     {
  154.         if ((*i % 2) == 0)
  155.         {
  156.             i = v3.erase(i);
  157.         }
  158.         else
  159.         {
  160.             ++i;
  161.         }
  162.     }
  163.  
  164.     std::copy(v3.begin(), v3.end(),
  165.         std::ostream_iterator<int>(std::cout, " "));
  166.     std::cout << std::endl;
  167.  
  168.     v3.insert(v3.begin(), 0);
  169.     v3.insert(v3.begin() + 2, 0);
  170.     v3.resize(10);
  171.     v3.insert(v3.end(), 5, 100);
  172.     std::copy(v3.begin(), v3.end(),
  173.         std::ostream_iterator<int>(std::cout, " "));
  174.     std::cout << std::endl;
  175.  
  176.     /*auto it4 = std::remove(v3.begin(), v3.end(), 0);
  177.     std::copy(v3.begin(), v3.end(),
  178.     std::ostream_iterator<int>(std::cout, " "));
  179.     std::cout << std::endl;
  180.  
  181.     std::cout << std::distance(v3.begin(), it4)
  182.     << std::endl;
  183.  
  184.     v3.erase(it4, v3.end());*/
  185.     v3.erase(std::remove(v3.begin(), v3.end(), 0), v3.end());
  186.     std::copy(v3.begin(), v3.end(),
  187.         std::ostream_iterator<int>(std::cout, " "));
  188.     std::cout << std::endl;
  189.  
  190.     std::vector<int> v4;
  191.     v4.reserve(100);
  192.     v4.insert(v4.begin(), { 1, 3, 4, 5 });
  193.     std::cout << v4.size() << " " << v4.capacity()
  194.         << std::endl;
  195.     v4.shrink_to_fit();
  196.     std::cout << v4.size() << " " << v4.capacity()
  197.         << std::endl;
  198.  
  199.     for (std::size_t i = 0; i < v4.size(); ++i)
  200.     {
  201.         std::cout << v4[i] << " ";
  202.     }
  203.  
  204.     std::vector<int> v5 = {1, 2, 7, 34, 7, 3, 40};
  205.     std::copy(v5.begin(), v5.end(),
  206.         std::ostream_iterator<int>(std::cout, " "));
  207.     std::cout << std::endl;
  208.  
  209.     auto lambda = [](int& el)
  210.     {
  211.         return (el % 2) == 0;
  212.     };
  213.  
  214.     auto it5 = std::remove_if(v5.begin(), v5.end(), lambda);
  215.     v5.erase(it5, v5.end());
  216.     std::copy(v5.begin(), v5.end(),
  217.         std::ostream_iterator<int>(std::cout, " "));
  218.  
  219.     return 0;
  220.  
  221. }
  222.  
  223. // https://pastebin.com/68miaX5g
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement