迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中向字符串添加整数

作者:迹忆客 最近更新:2023/04/08 浏览次数:

本文将讲解几种在 C++ 中向字符串添加整数的方法。


使用+= 运算符和 std::to_string 函数将整型添加到字符串中

std::string 类支持使用++= 等核心运算符进行最常见的连接形式。在下面的例子中,我们演示了后一种,因为它是最佳解决方案。在将 int 值追加到字符串的末尾之前,应该将 int 值转换为相同的类型,这可以通过 std::to_string 函数调用来实现。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;

    cout << app_str << endl;
    app_str += to_string(number);
    cout << app_str << endl;

    return EXIT_SUCCESS;
}

输出:

This string will be appended to
This string will be appended to 12345

上述方法也与浮点数兼容,如下面的代码示例所示。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    float fnumber = 12.345;

    cout << app_str << endl;
    app_str += to_string(fnumber);
    cout << app_str << endl;

    return EXIT_SUCCESS;
}

输出:

This string will be appended to
This string will be appended to 12.345000

使用 std::stringstream 将整型添加到字符串中

stringstream 可以将多种输入类型并以字符串格式存储。它提供了易于使用的操作符和内置的方法来将构建的字符串重定向到控制台输出。

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;
using std::stringstream;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;
    stringstream tmp_stream;

    cout << app_str << endl;
    tmp_stream << app_str << number;
    cout << tmp_stream.str() << endl;

    return EXIT_SUCCESS;
}

输出:

This string will be appended to
This string will be appended to 12345

使用 append() 方法将整型添加到字符串中

append()std::basic_string 类的一个成员函数,它可以根据参数的指定进行多种类型的追加操作。在最简单的形式下,当传递一个字符串参数时,它将追加到调用该方法的对象上。作为另一种形式,它可以接受一个单一的字符和整数,代表给定字符的追加计数。我们可以在手册中看到完整的参数列表。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;

    cout << app_str << endl;
    app_str.append(to_string(number));
    cout << app_str;

    return EXIT_SUCCESS;
}

输出:

This string will be appended to
This string will be appended to 12345

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便