【发布时间】:2020-08-10 14:58:14
【问题描述】:
我有一个字符串向量,vector <string> shapes 保存这些坐标数据:
Shape1, [3, 2]
Shape1, [6, 7]
Shape2, [7, 12, 3], [-9, 13, 68]
Shape1, [10, 3]
Shape2, [30, -120, 3], [-29, 1, 268]
Shape3, [15, 32], [1, 5]
Shape4, [24, 31, 56]
我正在尝试从Shape1 和Shape3 和x、y、z 从Shape2 和Shape4 中计算出坐标x 和y。这是一个可重现的代码:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector <string> shapes;
shapes.push_back("Shape1, [3, 2]");
shapes.push_back("Shape1, [632, 73]");
shapes.push_back("Shape2, [7, 12, 3], [-9, 13, 68]");
shapes.push_back("Shape1, [10, 3]");
shapes.push_back("Shape2, [30, -120, 3], [-29, 1, 268]");
shapes.push_back("Shape3, [15, 32], [1, 5]");
shapes.push_back("Shape4, [24, 31, 56]");
for(int i = 0; i < shapes.size(); i++)
{
// attempt to extract x
size_t string_start = shapes[i].find(", [");
string extracted = shapes[i].substr(string_start + 3, 1);
cout << extracted << endl;
}
return 0;
}
就像现在一样,我当前的代码不能正确地cout x - 只有x 的第一个字符是cout。我应该如何处理x的长度?随后,我应该如何cout数据中的y和z?分隔符是,,但到处都有多个,。
【问题讨论】:
-
您可以通过以更合理的方式存储数据来减轻痛苦。
-
从你的 x 坐标,你可能想读到下一个
, -
@Amachi 关于我如何做到这一点的任何参考?
-
@user3118602 我摆弄了一下,为你的问题做了一个不完美的解决方案。你可能想推进它。
标签: c++ string vector substring