c++中拆分数组字符串

Split array string in c++

本文关键字:字符串 数组 拆分 c++      更新时间:2023-10-16

我是新来的cpp,有一种情况,我想分割数组字符串

我有

    for( i = k = 0; i < points[1].size(); i++ )
    {
        cout << points[1][k];
    }

输出>>

    [390.826, 69.2596]
    [500.324, 92.9649]
    [475.391, 132.093]
    [5.60519e-44, 4.62428e-44]

I want

    390.826
    69.2596
    500.324
    92.9649
    475.391
    132.093
    5.60519e-44
    4.62428e-44

请帮帮我。由于

假设点的类型有public成员xy:

for( i = k = 0; i < points[1].size(); i++ )
{
    cout << points[1][k].x << endl;
    cout << points[1][k].y << endl;
}

如果成员是其他东西,例如,XY(大写),则使用大写(或其他)。

代码以这种方式打印输出的原因是因为operator<<已经重载了点的类型。比如:

std::ostream & operator<<(std::ostream & out, const point &p)
{
    return out << "[" << p.x << "," << p.y << "]n"; 
}

如果您可以在项目源代码的某个地方搜索上述定义(或类似的东西),然后可以将其更改为:

std::ostream & operator<<(std::ostream & out, const point &p)
{
    return out << p.x << "n" << p.y << "n"; 
}

则不需要更改for循环中的代码。

这与字符串分割无关,points[1][k]实际返回的是什么(即类型)。然后看看它是如何实现流输出操作符(operator<<)的,您将看到上面的内容是如何打印的。这应该给你一个关于两个单独的值(即*类型的字段)的线索,你可以简单地访问它们并打印出来。