Advertisement
chevengur

Подготовительный курс | Урок 3: Получение результата из функции: return 2/2

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