将字符串分离为 x 和 y 数组
separation of string into arrays of x and y
我正在尝试接收这样的一串点(2,4(,(5,8(,(12,7(,(15.54,3.65(并将其分为x数组和y数组,请帮助初学者,我已经尝试过了,这是灾难性的
string polygons;
int i = 0;
int length = polygons.length();
string x[10000];
int index = 0;
int k = 0;
getline(cin, polygons);
for (i = 0; i < length; i++)
{
if (polygons[i] == '(')
{
k = polygons.substr(i + 1, 20).find_first_of(",");
x[index] = polygons.substr(i + 1, ((k + i) - (i + 1)));
index++;
}
}
int a = 0;
string y[10000];
int index2 = 0;
int c = 0;
for (a = 0; a < length; a++)
{
if (polygons.substr(a) == ",")
{
c = polygons.substr(a + 1, 20).find_first_of(")");
}
y[index2] = polygons.substr(a + 1, ((c + a) - (a + 1)));
index2++;
}
要将点从一个字符串转换为具有数值的点,您可以使用 .find_first_of()
和 std::stod()
。这是我将点字符串转换为数值的代码。
#include <string>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
using std::cout;
using std::endl;
class Point
{
public:
Point(double x= 0, double y= 0) : m_x(x), m_y(y) {}
inline double getX() const { return m_x; }
inline double getY() const { return m_y; }
private:
double m_x, m_y;
};
vector<Point> strPoints2NumPoints(const string& strPoints)
{
vector<Point> points;
for (int i(0); i < strPoints.size(); ++i){
if ( strPoints[i] == '(' ){
// extract X & Y values as string
string temp = strPoints.substr(i+1, strPoints.find_first_of(")",i)-i-1 );
// convert X as strig to double
double X = std::stod( temp.substr(0,temp.find(',')) );
// convert Y as string to double
double Y = std::stod( temp.substr(temp.find(',')+1) );
points.push_back(Point(X,Y));
}
}
return points;
}
int main(int argc, char* argv[])
{
string strPoints("(2,4),(5,8),(12,7),(15.54,3.65)");
vector<Point> numPoints;
cout << "Points as string: " << strPoints << endl;
numPoints = strPoints2NumPoints(strPoints);
cout << "Points as numbers: " << endl;
for(int i(0); i < numPoints.size(); ++i ){
cout << "Point:<" << numPoints[i].getX() << "," << numPoints[i].getY() << ">" << endl;
}
return 0;
}
上述代码的输出为
Points as string: (2,4),(5,8),(12,7),(15.54,3.65)
Points as numbers:
Point:<2,4>
Point:<5,8>
Point:<12,7>
Point:<15.54,3.65>
根据您的代码和逻辑,我已经编辑了代码并处理了错误:
string polygons;
int i = 0;
string x[10000];
int index = 0;
int k = 0;
getline(cin, polygons);
int length = polygons.length();
for (i = 0; i < length; i++)
{
if (polygons[i] == '(')
{
// k = polygons.substr(i + 1, 20).find_first_of(",");
k = polygons.find_first_of(",", i);
// x[index] = polygons.substr(i + 1, ((k + i) - (i + 1)));
x[index] = polygons.substr(i + 1, k - i - 1);
index++;
}
}
int a = 0;
string y[10000];
int index2 = 0;
int c = 0;
for (a = 0; a < length; a++)
{
/*
if (polygons.substr(a) == ",")
{
c = polygons.substr(a + 1, 20).find_first_of(")");
}
y[index2] = polygons.substr(a + 1, ((c + a) - (a + 1)));
index2++;
*/
if (polygons[a] == ',' && polygons[a + 1] != '(')
{
k = polygons.find_first_of(")", a);
y[index2] = polygons.substr(a + 1, k - a - 1);
index2++;
}
}
这是我的代码,只是改进和简化你的代码风格:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string Polygons;
getline(std::cin, Polygons);
std::vector<std::string> XCoords;
for (int i = 0; i < Polygons.length(); ++i)
{
if (Polygons[i] == '(')
{
auto CommaIndex = Polygons.find_first_of(",", i);
XCoords.push_back(Polygons.substr(i + 1, CommaIndex - i - 1));
}
}
std::vector<std::string> YCoords;
for (int i = 0; i < Polygons.length(); ++i)
{
if (Polygons[i] == ',' && Polygons[i + 1] != '(')
{
auto Index = Polygons.find_first_of(")", i);
YCoords.push_back(Polygons.substr(i + 1, Index - i - 1));
}
}
return 0;
}
希望对您有所帮助。
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13