将文件数据存储到结构中的一个数组成员中
Store file data to one of the array members in a structure
我有一个文本文件:
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
};
问题:
- 如何将买家的所有商品价格存储到
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;
}
相关文章:
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 是否可以同时声明一个类成员的常量/非常量?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 将C++浮点数组成员直接封送到 C#,而无需复制
- 用于对项目进行分组并将单个项目映射到其他组成员的数据结构
- 只对向量中的一个类成员进行排序,其他成员保持不变
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- C++ - 移动具有固定大小的 c 样式数组成员的类的构造函数
- 在C++中,是否可以"overload"作为另一个类成员的对象?
- C++内联数组成员统一初始化
- 有没有办法把字符串数组成员放在".structurevariable"前面?
- 为什么我需要一个类似于复合文字的临时构造来初始化我的std::数组成员
- 作为另一个类成员的对象数组指针的语法
- 在C++中只包含一个int数组成员的结构中填充
- 是否可以有一个std::vector of struct和一个fexible数组成员