#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution
{
public:
bool reverse(string &str )
{
char tmp;
if(str.empty())
return false;
auto s = &str[0];
auto p = str.end()-1;
while(s<p)
{
tmp = *s;
*s = *p;
*p = tmp;
s++;
p--;
}
/*
int len = str.length();
for(int i=0;i<int(len/2);i++)
{
tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}*/
return true;
}
};
int main()
{
string str;
Solution tg;
getline(cin,str);
auto ss=&str[0];
auto tt=str.begin();
cout << *ss << endl;
tg.reverse(str);
cout<<str<<endl;
system("pause");
#include<vector>
#include<string>
using namespace std;
class Solution
{
public:
bool reverse(string &str )
{
char tmp;
if(str.empty())
return false;
auto s = &str[0];
auto p = str.end()-1;
while(s<p)
{
tmp = *s;
*s = *p;
*p = tmp;
s++;
p--;
}
/*
int len = str.length();
for(int i=0;i<int(len/2);i++)
{
tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}*/
return true;
}
};
int main()
{
string str;
Solution tg;
getline(cin,str);
auto ss=&str[0];
auto tt=str.begin();
cout << *ss << endl;
tg.reverse(str);
cout<<str<<endl;
system("pause");
}
总结:str.begin() &str[0] 都可以获得字符串首地址
用c语言实现就得遍历获得末尾地址;str.end();