c++整体输出string类字符串
时间: 2024-01-04 10:03:27 浏览: 32
在C++中,可以使用cout来整体输出string类字符串。下面是一个示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
cout << str << endl; // 输出:Hello, World!
return 0;
}
```
在上面的代码中,我们使用cout来输出string类字符串str,然后使用endl来换行。通过这种方式,我们可以整体输出string类字符串。
相关问题
c++ string类倒序输出字符串
可以使用C++ STL中的reverse函数来倒序输出字符串。例如:
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str = "hello world";
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}
```
输出结果为: "dlrow olleh"。
c++ string类 字符串小写
你可以使用 C++ 标准库中的 `transform` 算法和 `tolower` 函数来将字符串中的所有字符转换为小写。以下是一个示例代码:
```cpp
#include <algorithm>
#include <cctype>
#include <string>
int main() {
std::string str = "Hello World!";
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c){ return std::tolower(c); });
// 现在 str 中的内容变为 "hello world!"
return 0;
}
```
在这个代码中,`transform` 算法会遍历字符串 `str` 中的每个字符,并将其作为参数传递给 lambda 函数。该 lambda 函数使用 `tolower` 函数将字符转换为小写,并返回转换后的字符。`transform` 算法会将返回值写入到 `str` 中原来的位置,从而实现将整个字符串转换为小写。