开发者

Split array string in c++

I am new to cpp and have a situation in which I want to split array string

I have

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

Output >>

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

I wan开发者_JAVA技巧t

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

Please help me.Thanks


Assuming the type of point has public members x and y:

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

If the members are something else, say, X and Y (the uppercase), then use the uppercase instead (or whatever it is).

The reason why you code prints the output that way, because operator<< has been overloaded for the type of the point. Something like:

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

If you can search the above definition (or something similar) somewhere in your project source code, and then can change that to this:

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

then you wouldn't need to change the code in your for loop.


This has nothing to do with string splitting, what does points[1][k] actually return (i.e. it's type). Then look at how it has implemented the stream out operator (operator<<), and you'll see how the above is printed. This should give you a clue about the two individual values (i.e. fields of that *type), and you can simply access them and print them out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜