迹忆客 专注技术分享

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

在 C++ 中将字符串转换为整数数组

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

本文将介绍有关如何在 C++ 中将字符串转换为整数数组的多种方法。


使用 std::getlinestd::stoi 函数在 C++ 中将 string 转换为 int 数组

std::stoi 用于将字符串值转换为带符号的整数,它采用一个类型为 std::string 的强制性参数。可选地,该函数可以接受 2 个附加参数,其中第一个可用于存储最后一个未转换字符的索引。第三个参数可以选择指定输入的数字基数。注意,我们假设用逗号分隔的数字作为输入字符串,例如 .csv 文件。因此,我们使用 getline 函数来解析每个数字,然后将该值传递给 stoi。通过在每次迭代中调用 push_back 方法,我们还将每个数字存储在 std::vector 容器中。

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";
    vector<int> numbers;
    int num;

    stringstream text_stream(text);
    string item;
    while (std::getline(text_stream, item, ',')) {
        numbers.push_back(stoi(item));
    }

    for (auto &n : numbers) {
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

输出:

125
44
24
5543
111

在 C++ 中使用 std::string::findstd::stoi 函数将 string 转换为 int 数组

另外,我们可以利用 std::string 类的 find 内置方法来检索逗号分隔符的位置,然后调用 substr 函数来获取数字。然后,将 substr 结果传递到 stoi,该 stoi 本身链接到 push_back 方法,以将数字存储到 vector 中。请注意,while 循环后有一行是提取序列中最后一个数字所必需的。

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";
    vector<int> numbers;

    size_t pos = 0;
    while ((pos = text.find(',')) != string::npos) {
        numbers.push_back(stoi(text.substr(0, pos)));
        text.erase(0, pos + 1);
    }
    numbers.push_back(stoi(text.substr(0, pos)));

    for (auto &n : numbers) {
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

输出:

125
44
24
5543
111

使用 std::copystd::remove_if 函数在 C++ 中将 string 转换为 int 数组

提取整数的另一种方法是将 std::copy 算法与 std::istream_iteratorstd::back_inserter 结合使用。此解决方案将字符串值存储到向量中,并将其输出到 cout 流,但是可以轻松添加 std::stoi 函数以将每个元素转换为 int 值。请注意,以下示例代码仅将数字存储为字符串值。

#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;
using std::stoi;

int main(int argc, char *argv[]) {
    string text = "125, 44, 24, 5543, 111";

    vector<string> nums;
    std::istringstream iss(text);
    copy(std::istream_iterator<string>(iss),
         std::istream_iterator<string>(),
                 std::back_inserter(nums));


    for (auto &n : nums) {
        n.erase(std::remove_if(n.begin(), n.end(), ispunct), n.end());
        cout << n << endl;
    }

    exit(EXIT_SUCCESS);
}

输出:

125
44
24
5543
111

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便