cpp给string添加一个'1'在末尾
时间: 2023-12-03 22:05:05 浏览: 34
可以使用string类的append()函数,在字符串末尾添加一个字符'1',示例代码如下:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "hello";
str.append("1");
std::cout << str << std::endl;
return 0;
}
```
输出结果为:
```
hello1
```
相关问题
cpp string末尾添加字符
你可以使用 `push_back()` 函数向字符串末尾添加字符。例如,下面的代码将字符 'a' 添加到字符串 s 的末尾:
```cpp
std::string s = "hello";
s.push_back('a');
```
现在,字符串 s 的值为 "helloa"。你也可以使用 `+=` 运算符将字符添加到字符串末尾:
```cpp
std::string s = "hello";
s += 'a';
```
这个代码与上面的代码效果相同,都是将字符 'a' 添加到字符串 s 的末尾。
cpp在string字符串末尾添加'0'字符
你可以使用string类的push_back()函数将字符'0'添加到字符串的末尾。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello";
str.push_back('0');
std::cout << str << std::endl;
return 0;
}
```
输出结果为:Hello0。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)