C 如何将文件解析到结构的向量中
c++ How to parse a file into vector of structs
我有一个带有数据格式的文件,如下所示:
a 1.000 -1.000 1.000
b 7.89 4.56 2.46
C 50 20 10
我开始编写一些代码来解析文件并将数据存储在结构的向量中,但是我不确定如何完成此操作。
struct Coordinates
{
double x;
double y;
double z;
}
vector<Coordinates> aVec;
vector<Coordinates> bVec;
vector<Coordinates> cVec;
ifstream exampleFile;
exampleFile.open("example.txt");
// parse file
while(getline(exampleFile, str))
{
if(str[0] == "a")
{
Coordinates temp;
temp.x = firstNum; // this is where I'm stuck
temp.y = secondNum;
temp.z = thirdNum;
vertVec.push_back(temp);
}
if(str[0] == "b")
{
Coordinates temp;
temp.x = firstNum;
temp.y = secondNum;
temp.z = thirdNum;
vertVec.push_back(temp);
}
if(str[0] == "c")
{
Coordinates temp;
temp.x = firstNum;
temp.y = secondNum;
temp.z = thirdNum;
vertVec.push_back(temp);
}
}
首先说明文件表示为流。
流只是您可以阅读的内容。因此,您需要为您的结构编写流操作员,以便可以阅读它们。
std::ifstream file("Data"); // This represents a file as a stream
std::cin // This is a stream object that represents
// standard input (usually the keyboard)
他们都从std::istream
继承。因此,当传递给使用流的函数时,它们都相同。
int value;
std::cin >> value; // The >> operator reads from a stream into a value.
首先编写结构,以便知道如何从流中读取自身。
struct Coordinates
{
double x;
double y;
double z;
// This is an input function that knows how to read 3
// numbers from the input stream into the object.
friend std::istream& operator>>(std::istream& str, Coordinates& data)
{
return str >> data.x >> data.y >> data.z;
}
}
现在编写一些读取类型坐标对象的代码。
int main()
{
// Even if you only have a/b/c using a map to represent these
// values is better than having three different vectors
// as you can programmatically refer to the different vectors
std::map<char, std::vector<Coordinates>> allVectors;
char type;
Coordinates value;
// Read from the stream in a loop.
// Read the type (a/b/c)
// Read a value (type Coordinates)
while(std::cin >> type >> value)
{
// If both reads worked then
// select the vector you want and add the value to it.
allVectors[type].push_back(value);
}
}
而不是使用
if(str[0] == "a")
{
Coordinates temp;
temp.x = firstNum; // this is where I'm stuck
temp.y = secondNum;
temp.z = thirdNum;
vertVec.push_back(temp);
}
使用以下内容:
// Construct a istringstream from the line and extract the data from it
std::istringstream istr(str);
char token;
Coordinates temp;
if ( istr >> token >> temp.x >> temp.y >> temp.z )
{
// Use the data only if the extractions successful.
if ( token == 'a' ) // Please note that it's not "a"
{
// Do the right thing for a
}
// Add other clauses.
}
else
{
// Deal with the error.
}
相关文章:
- 将结构向量排序为子组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- C++对结构向量进行排序
- 在C++中对结构向量进行排序时出现问题
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 搜索并替换结构向量中的值
- 在结构向量中查找最接近的值
- 清除结构向量时会发生什么?
- 如何从 node-ffi 调用 c++ 中以结构向量作为参数的方法?
- 对结构向量进行两次排序
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 如何二进制搜索结构向量并在适当的索引处插入
- 结构向量中自定义结构函数的内存使用
- C++11-在结构向量中修改结构的成员
- 我正在尝试找到一个特定的结构成员,该成员是使用 lambda 函数的结构向量
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员