我正在撰寫一個程式,在該程式中,我必須將復數從控制臺讀取到包含Complex物件的向量中。為了做到這一點,我已經覆寫了>>運算子,但是我的向量在區間[1..vector.size()]而不是[0..vector.size()). 我希望它從零開始,但我無法弄清楚。
這是一個最小的可重現示例:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class Complex
{
private:
double real, im;
public:
Complex(double v_real = 0, double v_im = 0): real(v_real), im(v_im) {};
// getters
double getReal() const
{
return this->real;
}
double getImaginary() const
{
return this->im;
}
// setters
void setReal(double v_real)
{
this->real = v_real;
}
void setImaginary(double v_im)
{
this->im = v_im;
}
};
void operator>>(istream& i, Complex& c)
{
string line;
getline(i,line);
istringstream ss (line);
double temp_real;
double temp_im;
char oper;
ss >> temp_real;
ss >> oper;
ss >> temp_im;
if (oper == '-') temp_im *= -1;
char junk_i;
ss >> junk_i;
c.setReal(temp_real);
c.setImaginary(temp_im);
}
ostream& operator<<(ostream& o, Complex c)
{
if (c.getImaginary() > 0)
{
o<<c.getReal()<<' '<<c.getImaginary()<<'i'<<endl;
}
else
{
o<<c.getReal()<<c.getImaginary()<<'i'<<endl;
}
return o;
}
int main(){
cout << "How many numbers will you create?" << endl;
int num_of_complex;
cin >> num_of_complex;
vector<Complex> v;
Complex temp_c;
for (int i = 0; i < num_of_complex; i )
{
cin >> temp_c;
v.push_back(temp_c);
}
for (int i = 0; i < v.size(); i )
{
cout << v[i] << endl;
}
}
這是輸入/輸出:

uj5u.com熱心網友回復:
問題是您正在讀取第一個數字cin >> num_of_complex;,但這不會將游標移動到新行!這意味著下一次呼叫(即第一次呼叫您的覆寫>>)getline將只讀取一個空行并嘗試將任何內容轉換為復數。
您可以通過忽略輸入緩沖區中的所有內容來解決此問題,直到下一個換行符或讀取第一個數字后到緩沖區的末尾。這是通過以下方法完成的:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
完整的作業示例:
#include <iostream>
#include <vector>
#include <stream>
#include <limits>
using namespace std;
class Complex
{
private:
double real, im;
public:
Complex(double v_real = 0, double v_im = 0): real(v_real), im(v_im) {};
// Getters.
double getReal() const
{
return this->real;
}
double getImaginary() const
{
return this->im;
}
// Setters.
void setReal(double v_real)
{
this->real = v_real;
}
void setImaginary(double v_im)
{
this->im = v_im;
}
};
istream& operator>>(istream& i, Complex& c)
{
string line;
getline(i, line);
istringstream ss (line);
double temp_real;
double temp_im;
char oper;
ss >> temp_real;
ss >> oper;
ss >> temp_im;
if (oper == '-') temp_im *= -1;
char junk_i;
ss >> junk_i;
c.setReal(temp_real);
c.setImaginary(temp_im);
return i;
}
ostream& operator<<(ostream& o, Complex c)
{
if (c.getImaginary() > 0)
o << c.getReal() << ' ' << c.getImaginary() << 'i' << endl;
else
o << c.getReal() << c.getImaginary() << 'i' << endl;
return o;
}
int main()
{
cout << "How many numbers will you create?" << endl;
int num_of_complex;
cin >> num_of_complex;
// Ignore everything in the input buffer until a new line or the end
// of the buffer.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<Complex> v;
Complex temp_c;
for (int i = 0; i < num_of_complex; i )
{
cin >> temp_c;
v.push_back(temp_c);
}
for (int i = 0; i < v.size(); i )
{
cout << v[i] << endl;
}
}
uj5u.com熱心網友回復:
但是我的向量在 [1..vector.size()] 區間而不是 [0..vector.size()) 中被索引。我希望它從零開始,但我無法弄清楚。
從基于 1 的索引中減去 1 以獲得標準向量使用的基于 0 的索引。例子:
std::vector<int> v{5, 6, 7};
std::size_t index = 2; // should access value 6
std::cout << v[index - 1];
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442164.html
