将文件数据存储到结构中的一个数组成员中

Store file data to one of the array members in a structure

本文关键字:一个 组成员 数组 数据 文件 存储 结构      更新时间:2024-04-28

我有一个文本文件:

3 2.50 15.00 1.20

2 26.70 5.30

5 40.00 3.50 2.90 71.20 5.30

1 3.86

4 232.30 39.29 9.30 94.32

第一条竖线是商品数量,后面是每个商品的价格。

我有一个结构:

struct Buyers
{
int iItemCount;
double arItemPrice[]; //array stores all items purchased by a buyer
};

问题:

  1. 如何将买家的所有商品价格存储到arItemPrice数组中

您需要使用一个动态增长的容器,比如买家结构中的std::vector

这是因为你事先不知道,一行有多少价格。此外,您需要了解,在文本文件中;元素的计数是冗余信息。我们可以看到,有多少价格在排队。并且std::vector也知道它的大小。因此,结构中的"itemCount"也是多余的。

使用iostream函数进行读取非常简单。我们重载了Buyer结构的提取器运算符。在这个过程中,我们首先读取一行,然后读取该行中的单个元素。

请参阅这里的许多可能的解决方案之一。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
// Define a struct for buyers. With number of prices and proces
struct Buyer {
unsigned int itemCount{};
// Becuase we do not know in advance, how many elements will be in the container, we use a vector
// This can grow dynamically
std::vector<double> itemPrice{};
};
// This will read one line of Buyer information into our Buyer struct
std::istream& operator >> (std::istream& is, Buyer& b) {
// Read excatly one line from the stream
if (std::string line{}; getline(is, line)) {
// Put the string into a std::istringstream for further extraction of data
std::istringstream iss(line);
// Initialize our internal vector to empty
b.itemPrice.clear();
// Read the item count
iss >> b.itemCount;
// Read in a loop all prices and append them to our vector
for (double price{}; iss >> price; b.itemPrice.push_back(price))   ;
}
return is;
}
// The source text file with data (is the same handling as an fstream). Like a file in memory.
std::istringstream textFile{ R"(3 2.50 15.00 1.20
2 26.70 5.30
5 40.00 3.50 2.90 71.20 5.30
1 3.86
4 232.30 39.29 9.30 94.32
)" };

int main() {
// We will save all buyers in this  dynamically growing vector
std::vector<Buyer> buyers{};
// Read all byuers in a loop
for (Buyer b{}; textFile >> b; buyers.push_back(b));
return 0;
}

添加了带有new和不带有std::vector的版本

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
// Define a struct for buyers. With number of prices and proces
struct Buyer {
unsigned int itemCount{};
double* itemPrice{};
~Buyer() { delete[] itemPrice; }
};
// This will read one line of Buyer information into our Buyer struct
std::istream& operator >> (std::istream& is, Buyer& b) {
// Read excatly one line from the stream
if (std::string line{}; getline(is, line)) {
// Put the string into a std::istringstream for further extraction of data
std::istringstream iss(line);
// Initialize our internal vector to empty
b = {};
// Read the item count
iss >> b.itemCount;
// Allocate new memory
b.itemPrice = new double[b.itemCount]();
// Read in a loop all prices and append them to our vector
for (unsigned int i{}; i < b.itemCount; ++i)
iss >> b.itemPrice[i];
}
return is;
}
// The source text file with data (is the same handling as an fstream). Like a file in memory.
std::istringstream textFile{ R"(3 2.50 15.00 1.20
2 26.70 5.30
5 40.00 3.50 2.90 71.20 5.30
1 3.86
4 232.30 39.29 9.30 94.32
)" };
int main() {
// Use a static array with some numbers
Buyer buyers[20];
// Read all buyers in a loop
unsigned int i = 0;
while (textFile >> buyers[i] && i<20) {
std::cout << "Line " << i + 1 << ". Count: " << buyers[i].itemCount << "n";
for (unsigned int k = 0; k < buyers[i].itemCount; ++k) std::cout << buyers[i].itemPrice[k] << "  ";
std::cout << "n";
++i;
}
// Release allocated memory
for (unsigned int k = 0; k < i-1; ++k) {
buyers[i].~Buyer();
}
return 0;
}
相关文章: