解析c++中坐标字符串的最佳方法
Best way to parse coordinates string in C++
我想知道在c++中解析相同string
中聚集在一起的坐标的最佳方法。
例子:
1,5
42.324234,-2.656264
结果应该是两个double
变量…
如果字符串的格式总是像x,y
,那么这应该足够了。
#include <string>
#include <sstream>
double x, y;
char sep;
string str = "42.324234,-2.656264";
istringstream iss(str);
iss >> x;
iss >> sep;
iss >> y;
使用while (std::getline(stream, line))
提取每行,然后用line
初始化std::istringstream
。然后你可以像这样提取:
double x, y;
if (line_stream >> x &&
line_stream.get() == ',' &&
line_stream >> y) {
// Extracted successfully
}
相关文章:
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 在C++中向零方向近似的最佳方法
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 检测win32服务创建和删除的最佳方法
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 将线程中的数据存储到全局容器的最佳方法?
- 将一系列整数放入类的最佳方法是什么?
- 在派生类中使用基类的私有成员变量的最佳方法
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 比较两个节点坐标的最佳方法是什么?
- 在nodejs中使用本机代码的最佳方法是什么?
- 将 pybind11 绑定标记为已弃用的最佳方法
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- C++中变量混叠的最佳方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?