在 char 数组中定位双打 (C++/ROOT)
Locating doubles in a char array (C++/ROOT)
我已经解析了一些XML数据,我想要经纬度信息。我需要解析的行是这样的:
trkptlat="60.397015"lon="5.32299"
这是 char 数组中的一个元素,我如何 extract/parse 将数字加倍?请注意,随着数据的增加,数字精度会发生变化,因此我不能仅仅依靠挑选列索引。
这可以通过多种方式完成,提前警告完全未经测试的代码
使用安全版本的 sscan,参数看起来像这样
"\"trkptlat=\"%d\"lon=\"%d\""
务必检查 return 值的长度和错误。
将 std::find_first_of 与数字和点一起使用
auto start = find_first_of (haystack.begin(), haystack.end(), digits.begin(), digit.end());
auto end = find_first_of (it, haystack.end(), digitsdot.begin(), digitdot.end(),
[](char a, char b){ return a != b; });
double lat = atof(start); // somewhere there might be a version that returns how many chars read also.
// check for errors
etc.
进一步查看http://www.cplusplus.com/reference/algorithm/find_first_of/
如果你是那种值得信赖的人,你可以走一些你知道的捷径
"trkptlat="
将被添加到前面,因此您可以从
开始
auto start = haystack+preLen;
您正在使用 C 风格的字符数组作为字符串。因此,我假设您仍在使用 C。否则,您将 std::string
。 Ìn C++ 没有理由使用 char 数组代替 std::string
.
请参阅 C 型解决方案:
#include <cstring>
#include <cstdlib>
#include <cstdio>
int main() {
char data[] = "SomeTrashMoreTrashtrkptlat=\"60.397015\"lon=\"5.32299\"MoreTrashMoreTrash";
char firstDoubleIndicator[] = "trkptlat=\"";
char secondDoubleIndicator[] = "\"lon=\"";
double latitude = 0;
double longitude = 0;
char* startPosition = strstr(data, firstDoubleIndicator);
if (startPosition) {
latitude = std::atof(startPosition + std::strlen(firstDoubleIndicator));
}
startPosition = strstr(data, secondDoubleIndicator);
if (startPosition) {
longitude = std::atof(startPosition + std::strlen(secondDoubleIndicator));
}
std::printf("\nlatitude:\t%f\nlongitude:\t%f\n\n", latitude, longitude);
return 0;
}
我已经解析了一些XML数据,我想要经纬度信息。我需要解析的行是这样的:
trkptlat="60.397015"lon="5.32299"
这是 char 数组中的一个元素,我如何 extract/parse 将数字加倍?请注意,随着数据的增加,数字精度会发生变化,因此我不能仅仅依靠挑选列索引。
这可以通过多种方式完成,提前警告完全未经测试的代码
使用安全版本的 sscan,参数看起来像这样
"\"trkptlat=\"%d\"lon=\"%d\""
务必检查 return 值的长度和错误。
将 std::find_first_of 与数字和点一起使用
auto start = find_first_of (haystack.begin(), haystack.end(), digits.begin(), digit.end());
auto end = find_first_of (it, haystack.end(), digitsdot.begin(), digitdot.end(),
[](char a, char b){ return a != b; });
double lat = atof(start); // somewhere there might be a version that returns how many chars read also.
// check for errors
etc.
进一步查看http://www.cplusplus.com/reference/algorithm/find_first_of/
如果你是那种值得信赖的人,你可以走一些你知道的捷径
"trkptlat="
将被添加到前面,因此您可以从
开始auto start = haystack+preLen;
您正在使用 C 风格的字符数组作为字符串。因此,我假设您仍在使用 C。否则,您将 std::string
。 Ìn C++ 没有理由使用 char 数组代替 std::string
.
请参阅 C 型解决方案:
#include <cstring>
#include <cstdlib>
#include <cstdio>
int main() {
char data[] = "SomeTrashMoreTrashtrkptlat=\"60.397015\"lon=\"5.32299\"MoreTrashMoreTrash";
char firstDoubleIndicator[] = "trkptlat=\"";
char secondDoubleIndicator[] = "\"lon=\"";
double latitude = 0;
double longitude = 0;
char* startPosition = strstr(data, firstDoubleIndicator);
if (startPosition) {
latitude = std::atof(startPosition + std::strlen(firstDoubleIndicator));
}
startPosition = strstr(data, secondDoubleIndicator);
if (startPosition) {
longitude = std::atof(startPosition + std::strlen(secondDoubleIndicator));
}
std::printf("\nlatitude:\t%f\nlongitude:\t%f\n\n", latitude, longitude);
return 0;
}