C++ 如何将字符串类型的位置转换为坐标类型的位置?
在我的工作中,读取一个txt文件,其中的数据有车辆的地理位置。由于读取此文件时遇到问题,我希望将此信息作为字符串类型读取,但需要将位置信息存储为Coord类型。是否可以将字符串转换为坐标 错误:C++ 如何将字符串类型的位置转换为坐标类型的位置?,c++,veins,C++,Veins,在我的工作中,读取一个txt文件,其中的数据有车辆的地理位置。由于读取此文件时遇到问题,我希望将此信息作为字符串类型读取,但需要将位置信息存储为Coord类型。是否可以将字符串转换为坐标 错误: error: invalid operands to binary expression ('__istream_type' (aka 'basic_istream<char, std::char_traits<char> >') and 'veins::Coord') std
error: invalid operands to binary expression ('__istream_type' (aka 'basic_istream<char, std::char_traits<char> >') and 'veins::Coord')
std::ifstream file;
file.open("chFile.txt");
if(file.is_open()) {
int sector;
int ch;
Coord position;
while (file >> sector >> ch >> position) {
chTableStruct chData;
chData.ch = ch;
chData.position = position;
chTable.insert(std::pair<int,chTableStruct>(sector,chData));
}
file.close();
}
else {
std::cout << "The file chFile.txt cannot be opened! it exists?" << endl;
}
2 20 (401.467,223.4,0)
3 52 (201.446,223.4,0)
1 31 (201.461,623.4,0)
首先,对不起,我的英语很差 当我需要读取字符串并转换为Coord时,在Velses+OMNeT模拟器中,我会执行以下操作:
Coord MyClass::strToCoord(std::string coordStr){
//this 2 lines is will remove () if necessary
coordStr.erase(remove(coordStr.begin(), coordStr.end(), '('), coordStr.end()); //remove '(' from string
coordStr.erase(remove(coordStr.begin(), coordStr.end(), ')'), coordStr.end()); //remove ')' from string
char coordCh[30]; //30 or a sufficiently large number
strcpy(coordCh,coordStr.c_str());
double x = 0, y = 0, z = 0;
char *token = strtok(coordCh, ",");
x = stold(token);
token = strtok(NULL, ",");
y = stold(token);
token = strtok(NULL, ",");
z = stold(token);
cout << "x= " << x << ", y= " << y << ", z= " << z << endl; //comment this line to disable debugging
return Coord(x,y,z);
}
coordmyclass::strToCoord(std::string-coordStr){
//如有必要,这两条线路将被拆除()
擦除(删除(coordStr.begin(),coordStr.end(),“(”,coordStr.end());//从字符串中删除“(”)
coordStr.erase(从字符串中删除(coordStr.begin(),coordStr.end(),')),coordStr.end();//删除')
char coordCh[30];//30或足够大的数字
strcpy(coordCh,coordStr.c_str());
双x=0,y=0,z=0;
char*token=strtok(coordCh,“,”);
x=stold(令牌);
令牌=strtok(空,“”,“”);
y=stold(令牌);
令牌=strtok(空,“”,“”);
z=stold(令牌);
首先,对不起,我的英语很差
当我需要读取字符串并转换为Coord时,在Velses+OMNeT模拟器中,我会执行以下操作:
Coord MyClass::strToCoord(std::string coordStr){
//this 2 lines is will remove () if necessary
coordStr.erase(remove(coordStr.begin(), coordStr.end(), '('), coordStr.end()); //remove '(' from string
coordStr.erase(remove(coordStr.begin(), coordStr.end(), ')'), coordStr.end()); //remove ')' from string
char coordCh[30]; //30 or a sufficiently large number
strcpy(coordCh,coordStr.c_str());
double x = 0, y = 0, z = 0;
char *token = strtok(coordCh, ",");
x = stold(token);
token = strtok(NULL, ",");
y = stold(token);
token = strtok(NULL, ",");
z = stold(token);
cout << "x= " << x << ", y= " << y << ", z= " << z << endl; //comment this line to disable debugging
return Coord(x,y,z);
}
coordmyclass::strToCoord(std::string-coordStr){
//如有必要,这两条线路将被拆除()
擦除(删除(coordStr.begin(),coordStr.end(),“(”,coordStr.end());//从字符串中删除“(”)
coordStr.erase(从字符串中删除(coordStr.begin(),coordStr.end(),')),coordStr.end();//删除')
char coordCh[30];//30或足够大的数字
strcpy(coordCh,coordStr.c_str());
双x=0,y=0,z=0;
char*token=strtok(coordCh,“,”);
x=stold(令牌);
令牌=strtok(空,“”,“”);
y=stold(令牌);
令牌=strtok(空,“”,“”);
z=stold(令牌);
您的数据的格式是什么?显示输入的一部分。添加了新信息。似乎您需要接受字符串的“Coord”构造函数数据的格式是什么?显示输入的一部分。添加了新信息。似乎您需要接受字符串的“Coord”构造函数