废话不多说,直接上代码
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main() {
std::fstream file;
file.open("C:/Users/537/Desktop/test.txt", std::ios::in); // Replace "/path/to/file.txt" with the actual file path
if (!file.is_open()) {
std::cout << "File open failed." << std::endl;
return 0;
}
std::vector<std::string> data;
std::string line;
while (std::getline(file, line)) {
data.push_back(line);
}
file.close();
std::vector<std::vector<float>> positions;
for (const auto& item : data) {
std::string str = item;
std::string delimiter = ",";
size_t pos = 0;
std::vector<float> position;
while ((pos = str.find(delimiter)) != std::string::npos) {
std::string token = str.substr(1, pos - 1);
str.erase(0, pos + delimiter.length());
position.push_back(std::stof(token));
}
// Extract the last value
std::string lastToken = str.substr(0, str.length() - 1);
position.push_back(std::stof(lastToken));
positions.push_back(position);
}
// Print the positions
for (const auto& position : positions) {
for (const auto& coordinate : position) {
std::cout << coordinate << " ";
}
std::cout << std::endl;
}
// Output the specified array's spatial position
int arrayIndex;
std::cout << "Enter the array index to output the spatial position: ";
std::cin >> arrayIndex;
std::cout << positions[0][1];
std::cout << positions[1][1];
std::cout << positions[2][1];
return 0;
}
代码比较简单,只需把上述保存识别目标三维空间的.txt文件地址换成你的,或者你需要一些其他功能,可以稍作修改即可