Advertisement
chevengur

Подготовительный курс | Урок 4: Кратко о главном — функция main

Aug 6th, 2023 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. ============
  2. ||main.cpp||
  3. ============
  4.  
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. // Реализуйте функцию FindInVector
  12.  
  13. int FindInVector(vector<string>word, string search_word){\
  14.     for(auto i = 0; i < word.size(); ++i){
  15.         if(word[i] == search_word){
  16.             return i;
  17.         }
  18.     }
  19.     return -1;  
  20. }
  21.  
  22. // Не меняйте следующие функции
  23. void PrintVector(vector<string> v) {
  24.     bool is_first = true;
  25.     for (string s : v) {
  26.         if (!is_first) {
  27.             cout << ", "s;
  28.         }
  29.         cout << s;
  30.         is_first = false;
  31.     }
  32. }
  33.  
  34. void TestFind(vector<string> haystack, string needle) {
  35.     int result = FindInVector(haystack, needle);
  36.    
  37.     if (result < 0 ) {
  38.         cout << needle << " not found in "s;
  39.         PrintVector(haystack);
  40.         cout << endl;
  41.     } else if (result < haystack.size() && haystack[result] == needle) {
  42.         cout << needle << " found at "s << result << endl;
  43.     } else {
  44.         cout << "Incorrect result"s << endl;
  45.     }
  46. }
  47.  
  48. int main(){
  49.     TestFind({"zero"s, "one"s, "two"s, "three"s, "four"s, "five"s}, "four"s);
  50.     TestFind({"one"s, "two"s, "three"s}, "four"s);
  51.     TestFind({"to"s, "be"s, "or"s, "not"s, "to"s, "be"s}, "be"s);
  52. }
  53.  
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55.  
  56. =================
  57. ||functions.cpp||
  58. =================
  59.  
  60.  
  61. #empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement