实现翻转数组,字符串,向量!
1翻转数组
#include <algorithm>
reverse(a, a+n);
示例代码,
#include <iostream>
#include <algorithm>
using namespace std;
void MyShow(int a[], int n)
{
for(int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
MyShow(a, 5);
reverse(a, a+5);
MyShow(a, 5);
return 0;
}
输出为,
1 2 3 4 5
5 4 3 2 1
2翻转字符串
reverse(str.begin(), str.end());
示例代码为,
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str = "abcdefg";
cout << str << endl;
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}
输出为,
abcdefg
gfedcba
3翻转向量
reverse(vec.begin(), vec.end());
示例代码为,
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void MyShow(vector<int> num)
{
for(int i = 0; i < num.size(); i++)
cout << num[i] << ' ';
cout << endl;
}
int main()
{
vector<int> vec = {1, 2, 3, 4, 5};
MyShow(vec);
reverse(vec.begin(), vec.end());
MyShow(vec);
return 0;
}
输出为,
1 2 3 4 5
5 4 3 2 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)