// 第一种std::stringreverse(std::stringstr) { std::stringres("");for(int i =str.size() -1; i >=0; i--) { res +=str[i]; }returnres; }// 第二种intmain(void) { std::stringstr("abcde"); int i =0, j =str.size() -1;while
{usingnamespacestd;//1KW 字符串反序函数测试,分别测试同样算法,string 和 C风格字符串的区别stringstr ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";for(inti =0; i !=10000001; i++)//STL_Reverse(str);//0.313秒//good_Reverse(str);//0.875秒//Reverse(str);//1.063秒bad_Reverse(str);//7.016秒cout...
string s3(s2); // 作用同上 string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中 string s5("hello world"); // 作用同上 string s6(6,'a'); // 初始化s6为:aaaaaa string s7(s6, 3); // s7 是从 s6 的下标 3 开始的字符拷贝 string s8(s...
// C++ program to get reverse of a const string #include<bits/stdc++.h> usingnamespacestd; // Function to reverse string and return // reverse string pointer of that char*reverseConstString(charconst*str) { // find length of string intn=strlen(str); // create a dynamic pointer char ...
using namespace std; int main(){ string str = "nhooo"; reverse(str.begin(), str.end()); cout << str; return 0; } 给定字符串的反向打印- #include <bits/stdc++.h> using namespace std; void reverse(string str){ for (int i=str.length()-1; i>=0; i--) ...
using namespace std; int main() { string str = "52cxydh"; // Reverse str[begin..end] reverse(str.begin(), str.end()); cout << str; return 0; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 输出 hdyxc25 ...
是c++里的 include <algorithm> using namespace std;int main(){ string s= "hello";reverse(s.begin(),s.end());cout<<s<<endl;return 0;}
1 #include <string> 2 using namespace std; string对象的输入方式: cin\getline 1 #include <iostream> 2 #include <string> 3 4 int main() 5 { 6 string s1, s2; 7 cin >> s1; 8 getline(cin, s2); 9 10 return 0; 11 } 二、C字符串相关操作 ...
using namespace std; int main() { string num; cout << "请输入一个数字:"; cin >> num; reverse(num.begin(), num.end()); cout << "逆序输出的数字为:" << num << endl; return 0; } python num = input("请输入一个数字:") reverse_num = num[::-1] print("...
#include<stdio.h>#include<string.h>voidreverse(char*s){char*start=s;//指向第一个字符的位置:...