109k views
3 votes
23.21 lab: reverse vector complete reverse() function that returns a new character vector containing all contents in the input argument reversed. ex: if the input vector is: ['a', 'b', 'c'] then the returned vector will be: ['c', 'b', 'a'] 424384.2897294.qx3zqy7

User Butifarra
by
5.2k points

1 Answer

1 vote

#include <bits/stdc++.h>

//Defining our vector.

std::vector<char> idx;

int main(int argc, char* argv[]) {

//We will fill our vector with input from the user.

std::cout << "How many characters: ";

int a; std::cin >> a;

for(int i=0;i<a;i++) {

char temp;

std::cin >> temp;

idx.push_back(temp);

}

//Reversing.

reverse(idx.begin(), idx.end());

//Clearing the window first

system("clear");

//Print the reversed vector.

std::cout << "Reversed: [";

for(auto const& i: idx) {

if(i==idx.at(a-1)) std::cout << i;

else std::cout << i << ", ";

}

std::cout << "]\\";

return 0;

}

User Saroekin
by
5.1k points