使用cin与矢量和类 [英] Using cin with vector and class
问题描述
使用vector.push_back函数在类中使用cin的最佳方法是什么?
我知道它是否是正常向量我可以这样做然后使用sentinel代码来提示结束
int myint;
vector< int> Ÿ;
cout<< vector integer<< ENDL;
while ((cin>> myint)&& myint!= 9999 )
y.push_back(myint);
for ( int i = 0 ; i < y.size(); i ++)
cout<< y =<< y [i]<<这是我的代码:
#include< iostream>
#include< fstream>
#include< vector>
using namespace std;
类账户{
protected:
int accno;
双倍余额;
public:
Account(int accno,double balance):accno(accno),balance(balance){};
double getBalance(){return balance;}
void setBalance(double balance){this-> balance = balance; }
int getAccno(){return accno;}
void setAccno(int accno){this-> accno = accno; }
无效存款(双倍金额){
余额=余额+金额;
}
};
class保存:公共账户{
public:
Saving():Account(accno,balance){};
Saving(int accno,double balance):Account(accno,balance){};
bool withdraw(double amount){
if(balance< amount){
cout<< 不充足的资金;
返回false;
}
else {
余额=余额 - 金额;
返回true;
}
}
void compute_interest(){
balance = balance * 1.05;
}
void print(){
cout<< ID:<< accno<< balance:<<余额<< ENDL;
}
朋友istream&运算符>>(istream& input,Saving& account){
cout<< ID: ;
输入>> account.accno;
cout<< 平衡:;
输入>>账户余额;
返回输入;
}
朋友ostream&运算符<<(ostream& output,const Saving& account){
output<< ID:<< account.accno<< balance:<< account.balance<< ENDL;
}
//朋友布尔运算符!=(保存a,int b)
};
int main()
{
保存储蓄[5];
cout<< 保存数组<< ENDL;
for(int i = 0; i< 5; i ++){
cin>>节省[I];
saving [i] .print();
cout<< ENDL;
}
保存myvec;
vector< Saving> savingV;
cout<< 保存矢量<< ENDL;
while((cin>> myvec)&&(myvec.getAccno()!= 9999)){
int i = 0;
savingV.push_back(myvec);
savingV [i] .print();
cout<< ENDL;
i ++;
}
返回0;
}
正如您在主要功能中看到的那样,我首先使用数组进行测试。
基本上我真正想做的事情使用cin with class(vector not array)是为了保持插入值,直到提示sentinel值或字符或字符串。
&我仍然无法设计运算符重载函数(比如!=)
这就是我想通过重载!=运算符来做的事情
while((cin>> myvec)&& myvec!= 9999);
但是idk是否可以完成。
我尝试过:
do {
int i = 0;
cin>> myvec;
savingV.push_back(myvec);
savingV [i] .print();
cout<< endl;
i ++;
} while(cin>> myvec&&((myvec.getAccno()!= 9999)||(myvec.getBalance ()!= 9999)));
解决方案
你基本上有正确的想法。你可以写一个规范的运算符!= as:
bool operator !=(保存const& lhs,保存const& rhs)
{
// 详细信息省略
}
然后声明一个sentinel值(例如 Saving sentinel(9999,-1.0)),或者你可以编写一个运算符,将 Saving 与 int 进行比较(或者任何其他类型):
bool operator !=(保存const& lhs, int rhs)
{
// 详细信息省略
}
任何一种方法都应该有效。
请注意,如果您声明 operator!= ,最好同时声明 operator == 集团kquote>
What are the best ways to use cin with class using vector.push_back function?
I know if it's normal vector I can do it like this and then use a sentinel code to prompt the end
int myint;
vector<int> y;
cout << "vector integer" << endl;
while ((cin >> myint) && myint != 9999)
y.push_back(myint);
for (int i = 0; i < y.size(); i++)
cout << "y = " << y[i] << endl;*
Here's my code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Account{
protected:
int accno;
double balance;
public:
Account(int accno, double balance) : accno(accno), balance(balance){};
double getBalance() {return balance;}
void setBalance(double balance){ this->balance=balance; }
int getAccno() {return accno;}
void setAccno(int accno){ this->accno=accno; }
void deposit(double amount){
balance = balance + amount;
}
};
class Saving : public Account{
public:
Saving () : Account(accno,balance){};
Saving (int accno, double balance) : Account(accno,balance){};
bool withdraw (double amount){
if (balance < amount){
cout << "Insufficient funds";
return false;
}
else{
balance = balance - amount;
return true;
}
}
void compute_interest(){
balance = balance * 1.05;
}
void print(){
cout << "ID: " << accno << " balance: " << balance << endl;
}
friend istream& operator >>(istream& input, Saving& account){
cout << "ID: ";
input >> account.accno;
cout << "balance: ";
input >> account.balance;
return input;
}
friend ostream& operator <<(ostream& output, const Saving& account){
output << "ID: " << account.accno << " balance: " << account.balance << endl;
}
//friend bool operator !=(Saving a, int b)
};
int main()
{
Saving saving[5];
cout << "saving array" << endl;
for (int i = 0; i < 5; i++){
cin >> saving[i];
saving[i].print();
cout << endl;
}
Saving myvec;
vector<Saving> savingV;
cout << "saving vector" << endl;
while ((cin >> myvec) && (myvec.getAccno() != 9999)){
int i = 0;
savingV.push_back(myvec);
savingV[i].print();
cout << endl;
i++;
}
return 0;
}
As you can see in the main function i tested first using an array.
Basically what I really want to do using cin with class(vector not array) is to keep inserting value until a sentinel value or character or string is prompted.
& I'm still having trouble designing operator overloading functions (like the !=)
This is what I thought of doing by overloading the "!=" operator
while ((cin >> myvec) && myvec != 9999);
But idk whether it can be done.
What I have tried:
do{
int i = 0;
cin >> myvec;
savingV.push_back(myvec);
savingV[i].print();
cout << endl;
i++;
} while (cin >> myvec && ((myvec.getAccno() != 9999) || (myvec.getBalance() != 9999)));
解决方案 You basically have the correct idea. You can either write a "canonical" operator != as:
bool operator !=(Saving const& lhs, Saving const& rhs)
{
// details omitted
}
and then declare a sentinel value (e.g. Saving sentinel(9999, -1.0)), or you can write an operator that compares Saving to int (or any other type):
bool operator !=(Saving const& lhs, int rhs)
{
// details omitted
}
Either method should work.
Note that if you declare operator !=, it is a good practice to also declare operator ==
这篇关于使用cin与矢量和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!