You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++17中将特定格式字符串转换为T结构体数组?

C++17实现字符串到结构体数组的转换

首先明确结构体定义:

struct T
{
    int a;
    int b;
    int c;
};

要把类似 "1 2 3 | 3 1 2 | 2 2 1" 的字符串转换成std::vector<T>(或数组),可以利用C++17的特性简化实现,具体方案如下:

核心思路与代码实现

1. 按"|"分割主字符串

用C++17引入的std::string_view避免不必要的字符串拷贝,实现轻量分割:

#include <string_view>
#include <vector>

std::vector<std::string_view> split_by_pipe(std::string_view sv) {
    std::vector<std::string_view> result;
    size_t pos = 0;
    while (pos < sv.size()) {
        auto pipe_pos = sv.find('|', pos);
        // 提取当前分段并修剪前后空格
        auto segment = sv.substr(pos, pipe_pos == std::string_view::npos ? pipe_pos : pipe_pos - pos);
        size_t start = segment.find_first_not_of(' ');
        if (start != std::string_view::npos) {
            segment = segment.substr(start);
            size_t end = segment.find_last_not_of(' ');
            if (end != std::string_view::npos) {
                segment = segment.substr(0, end + 1);
            }
        }
        if (!segment.empty()) {
            result.push_back(segment);
        }
        if (pipe_pos == std::string_view::npos) break;
        pos = pipe_pos + 1;
    }
    return result;
}

2. 解析单个分段为T结构体

这里用到两个C++17关键特性:

  • std::from_chars:直接从字符序列解析数值,高效且无全局状态依赖
  • 结构化绑定:直接绑定解析结果,简化赋值逻辑

解析函数实现:

#include <charconv>
#include <stdexcept>

T parse_segment(std::string_view segment) {
    int a, b, c;
    const char* ptr = segment.data();
    const char* end = ptr + segment.size();

    // 解析第一个整数
    auto [ptr1, ec1] = std::from_chars(ptr, end, a);
    if (ec1 != std::errc()) throw std::invalid_argument("Invalid integer for a");
    ptr = ptr1;
    while (ptr < end && *ptr == ' ') ptr++;

    // 解析第二个整数
    auto [ptr2, ec2] = std::from_chars(ptr, end, b);
    if (ec2 != std::errc()) throw std::invalid_argument("Invalid integer for b");
    ptr = ptr2;
    while (ptr < end && *ptr == ' ') ptr++;

    // 解析第三个整数
    auto [ptr3, ec3] = std::from_chars(ptr, end, c);
    if (ec3 != std::errc()) throw std::invalid_argument("Invalid integer for c");

    return {a, b, c};
}

3. 整合转换逻辑

结合前两步,用范围for遍历分段,生成结构体数组:

#include <vector>
#include <string>

std::vector<T> string_to_T_array(const std::string& input) {
    std::vector<T> result;
    auto segments = split_by_pipe(input);
    for (auto seg : segments) {
        result.push_back(parse_segment(seg));
    }
    return result;
}

关键特性说明

  • std::string_view:全程避免字符串拷贝,大幅提升分割操作的性能
  • std::from_chars:类型安全的数值解析,比传统istringstream或sscanf更高效
  • 结构化绑定:将std::from_chars返回的迭代器和错误码直接绑定到变量,代码更简洁直观

测试示例

#include <iostream>

int main() {
    std::string input = "1 2 3 | 3 1 2 | 2 2 1";
    auto arr = string_to_T_array(input);
    for (const auto& t : arr) {
        std::cout << "a: " << t.a << ", b: " << t.b << ", c: " << t.c << "\n";
    }
    return 0;
}

输出结果:

a: 1, b: 2, c: 3
a: 3, b: 1, c: 2
a: 2, b: 2, c: 1

内容的提问来源于stack exchange,提问作者Irbis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 23:22:55