我有一个XML字符串,如下所示:
import {Input} from "./Input";
/* InputLabel.ts */
export class InputLabel extends Input {
readonly label: string;
constructor(label:string, inputType: Input.INPUT_TYPE) {
super(inputType);
this.label = label;
}
}
我试图将每对数字放入< thing TYPE="array" UNITS="meters">1.0,1.3,1.2,1.7,1.4,1.9< /thing>
。完成后应该看起来像这样:
&LT; (1.0,1.3),(1.2,1.7),(1.4,1.9)&gt;
我知道我可以这样做的一种方法是搜索字符串中的每个逗号以查找单个数字并创建子字符串,然后将子字符串转换为double并填充对中的一个数字。然而,这似乎是完成此任务的过于复杂的方式。有没有一种简单的方法可以做到这一点,也许是使用std::vector< std::pair< double,double > >
?提前谢谢!
答案 0 :(得分:0)
如何使用<?php
switch($_REQUEST['page']) {
case "about":
include "about.php";
break;
case "mission":
include "mission.php";
break;
default:
include "homepage.php";
}
?
getline()
另一个解决方案可以将逗号放在#include <vector>
#include <sstream>
#include <iostream>
int main()
{
std::vector<std::pair<double, double>> vpss;
std::string str1, str2;
std::istringstream iss("1.0,1.3,1.2,1.7,1.4,1.9");
while ( std::getline(iss, str1, ',') && std::getline(iss, str2, ',') )
{
std::cout << str1 << ", " << str2 << std::endl;
vpss.emplace_back(std::stod(str1), std::stod(str2));
}
return 0;
}
变量
char
答案 1 :(得分:0)
您可以使用正则表达式捕获所需的数据,并使用std::stringstream
来解析它
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <regex>
#include <utility>
using namespace std;
int main()
{
auto xml_str = R"(< thing TYPE="array" UNITS="meters">1.0,1.3,1.2,1.7,1.4,1.9< /thing>)"s;
auto r = regex(R"(>(\d.*\d)<)"s);
auto m = smatch{};
auto vec = vector<pair<double, double>>{};
// Determine if there is a match
auto beg = cbegin(xml_str);
while (regex_search(beg, cend(xml_str), m, r)) {
// Create a string that holds the 1st capture group
auto str = string(m[1].first, m[1].second);
auto ss = stringstream{str};
auto token1 = ""s;
auto token2 = ""s;
auto d1 = double{};
auto d2 = double{};
// Parse
while (getline(ss, token1, ',')) {
getline(ss, token2, ',');
d1 = stod(token1);
d2 = stod(token2);
vec.emplace_back(make_pair(d1, d2));
}
// Set new start position for next search
beg = m[0].second;
}
// Print vector content
auto count = 1;
for (const auto& i : vec) {
if (count == 3) {
cout << "(" << i.first << ", " << i.second << ")\n";
count = 1;
}
else {
cout << "(" << i.first << ", " << i.second << "), ";
count++;
}
}
}
(与-std=c++14
编译)
输出:
(1, 1.3), (1.2, 1.7), (1.4, 1.9)