c++中拆分数组字符串
Split array string in c++
我是新来的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
成员x
和y
:
for( i = k = 0; i < points[1].size(); i++ )
{
cout << points[1][k].x << endl;
cout << points[1][k].y << endl;
}
如果成员是其他东西,例如,X
和Y
(大写),则使用大写(或其他)。
代码以这种方式打印输出的原因是因为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<<
)的,您将看到上面的内容是如何打印的。这应该给你一个关于两个单独的值(即*类型的字段)的线索,你可以简单地访问它们并打印出来。
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组