Advertisement
AshfaqFardin

Arrays - DS

Jul 8th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7. vector<string> split(const string &);
  8.  
  9. /*
  10.  * Complete the 'reverseArray' function below.
  11.  *
  12.  * The function is expected to return an INTEGER_ARRAY.
  13.  * The function accepts INTEGER_ARRAY a as parameter.
  14.  */
  15.  
  16. vector<int> reverseArray(vector<int> a) {
  17.     int array_size = a.size();
  18.     int mid_point = a.size() / 2;
  19.     for(int i = 0; i < mid_point; i++){
  20.         int temp = a[i];
  21.         a[i] = a[array_size - i - 1];
  22.         a[array_size - i - 1] = temp;
  23.     }
  24.    
  25.     return a;
  26. }
  27.  
  28. int main()
  29. {
  30.     ofstream fout(getenv("OUTPUT_PATH"));
  31.  
  32.     string arr_count_temp;
  33.     getline(cin, arr_count_temp);
  34.  
  35.     int arr_count = stoi(ltrim(rtrim(arr_count_temp)));
  36.  
  37.     string arr_temp_temp;
  38.     getline(cin, arr_temp_temp);
  39.  
  40.     vector<string> arr_temp = split(rtrim(arr_temp_temp));
  41.  
  42.     vector<int> arr(arr_count);
  43.  
  44.     for (int i = 0; i < arr_count; i++) {
  45.         int arr_item = stoi(arr_temp[i]);
  46.  
  47.         arr[i] = arr_item;
  48.     }
  49.  
  50.     vector<int> res = reverseArray(arr);
  51.  
  52.     for (size_t i = 0; i < res.size(); i++) {
  53.         fout << res[i];
  54.  
  55.         if (i != res.size() - 1) {
  56.             fout << " ";
  57.         }
  58.     }
  59.  
  60.     fout << "\n";
  61.  
  62.     fout.close();
  63.  
  64.     return 0;
  65. }
  66.  
  67. string ltrim(const string &str) {
  68.     string s(str);
  69.  
  70.     s.erase(
  71.         s.begin(),
  72.         find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  73.     );
  74.  
  75.     return s;
  76. }
  77.  
  78. string rtrim(const string &str) {
  79.     string s(str);
  80.  
  81.     s.erase(
  82.         find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  83.         s.end()
  84.     );
  85.  
  86.     return s;
  87. }
  88.  
  89. vector<string> split(const string &str) {
  90.     vector<string> tokens;
  91.  
  92.     string::size_type start = 0;
  93.     string::size_type end = 0;
  94.  
  95.     while ((end = str.find(" ", start)) != string::npos) {
  96.         tokens.push_back(str.substr(start, end - start));
  97.  
  98.         start = end + 1;
  99.     }
  100.  
  101.     tokens.push_back(str.substr(start));
  102.  
  103.     return tokens;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement