c++ reverse int code example
Example 1: c++ reverse vector
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
vector<int> nums{4,1,2,1,2};
reverse(nums.begin(), nums.end());
return 0;
}
Example 2: reversing numbers in C++
#include <iostream>
#include <string>
using namespace std;
int reverse(int number);
int main() {
int x = 18475;
cout << "18475 reversed is " << reverse(18475) << endl;
}
int reverse(int number) {
int result = 0;
while (number > 0) {
result = result * 10 + number % 10;
number /= 10;
}
return result;
}
Example 3: reverse c++
reverse(str.begin(),str.end());
Example 4: reverse() in c++
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int>a = {11,22,33,44,99,55};
reverse(a.begin(), a.end());
auto it = a.begin();
for(it= a.begin(); it!=a.end(); it++){
cout << *it << ' ';
}
}
Example 5: reverse iterator c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int>::reverse_iterator r_iter;
for (r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) {
std::cout << *r_iter << std::endl;
}
return 0;
}