[英]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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.