c ++ string类型颠倒
时间: 2023-11-02 11:07:42 浏览: 33
可以使用C++中的reverse函数来颠倒string类型的字符串。具体实现如下:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "hello world";
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}
```
输出结果为:dlrow olleh
相关问题
c语言+int转string
将int类型转换为string类型的方法有很多,以下是两种常见的方法:
1.使用sprintf函数将int类型转换为char类型数组,再将char类型数组转换为string类型。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int num = 123;
char str[10];
sprintf(str, "%d", num);
printf("The string is: %s\n", str); // 输出:The string is: 123
return 0;
}
```
2.使用snprintf函数将int类型转换为string类型。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int num = 123;
char str[10];
snprintf(str, sizeof(str), "%d", num);
printf("The string is: %s\n", str); // 输出:The string is: 123
return 0;
}
```
c ++ to_string怎么用、
C++ 的 `to_string` 函数用于将其他数据类型转换为字符串。它可以用于整数、浮点数等数据类型的转换。
下面是使用 `to_string` 函数的示例:
```cpp
#include <iostream>
#include <string>
int main() {
int num = 123;
double pi = 3.14159;
std::string numStr = std::to_string(num);
std::string piStr = std::to_string(pi);
std::cout << "Number as string: " << numStr << std::endl;
std::cout << "Pi as string: " << piStr << std::endl;
return 0;
}
```
在上面的示例中,我们将整数 `num` 和浮点数 `pi` 分别转换为字符串,并将其打印出来。
输出结果:
```
Number as string: 123
Pi as string: 3.141590
```
希望这能帮到你!如果还有其他问题,请随时问我。