# 我如何将向量中的值发送给类？

[英]How would I send values in a vector to a class?

excel 电子表格是一份月度预算报告，用户必须在其中设置他使用的每项服务的成本/收入，比方说，水、煤气、电、amazon.com 等。

``````double value;
cin >> value;
do {
passToVector(value);
cin >> value;
} while(value != 0);
``````

passToVector 函数然后将值存储在 expensesList 向量中：

``````void passToVector(double value){
vector<double> expensesList;
expensesList.push_back(value);
};
``````

``````class Expenses {
private:
double percentage = 0.00;
double mortgage = 0.00;
double electric = 0.00;
double gas = 0.00;
double water = 0.00;
public:
// Set percentage each person brings
void setPercentage(double per) { percentage = per; }
double getPercentage() const { return percentage / 100; }
// Calculate Total Expenses by Person
void calculatePersonExpenses() {
double result = mortgage * getPercentage();
double result2 = electric * getPercentage();
double result3 = gas * getPercentage();
double result4 = water * getPercentage();

cout << "Mortgage:                      \$" << fixed << setprecision(2) << result << endl;
cout << "Electricity:                   \$" << fixed << setprecision(2) << result2 << endl;
cout << "Gas(Home):                     \$" << fixed << setprecision(2) << result3 << endl;
cout << "Water:                         \$" << fixed << setprecision(2) << result4 << endl;
}
};
``````

``````void passToVector(double value){
vector<double> expensesList;
expensesList.push_back(value);
};
``````

``````#include <string_view>
#include <iostream>

template<typename T>
T ReadCin(std::string_view const& str = "") {
if (!str.empty()) std::cout << str;
T retVal;
std::cin >> retVal;
return retVal;
}

class Expenses {
private:
double percentage{ 0 };
double mortgage{ 0 };
double electric{ 0 };
double gas{ 0 };
double water{ 0 };
public:
Expenses() = default;
Expenses(double percentage, double mortgage, double electric, double gas, double water)
:percentage(percentage), mortgage(mortgage), electric(electric), gas(gas), water(water)
{}

static Expenses NewItem() { return {
}
};

#include <vector>

int main() {
std::vector<Expenses> expensesList;
expensesList.push_back(Expenses::NewItem());
}
``````

``````class Expenses {
private:
std::vector<double> expensesList {};
//[...etc]
public:
void AddExpense(double value) noexcept {
expenseList.push_back(value);
}
``````