为什么我不能将对象放入向量 C++ 的开头

Why can't I put an Object into the start of a vector C++

我正在编写一个程序,在该程序中我必须从控制台将复数读入一个向量,其中包含 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;
    }
}

这里是 input/output:

but my vector gets indexed in the [1..vector.size()] interval instead of [0..vector.size()). I want it to go from zero, but I can't figure it out.

从基于 1 的索引中减去 1 以获得标准向量使用的基于 0 的索引。示例:

std::vector<int> v{5, 6, 7};
std::size_t index = 2; // should access value 6
std::cout << v[index - 1];

问题是您使用 cin >> num_of_complex; 读取第一个数字,但是 this does not move the cursor to a new line!这意味着使用 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;
    }
}