C++:为什么我的结构 vector 充当一个结构?

标签 c++ vector struct

我正在研究 Accelerated C++,并决定弄乱其中定义的结构之一。这样做时,我遇到了一个问题:创建这些结构的 vector 并修改每个结构中的元素似乎会修改所有结构中的元素。

我意识到这可能意味着我已将 vector 中的所有结构初始化为单个内存地址处的结构,但我使用 .push_back() 方法将“虚拟”结构插入到 vector 中。我的印象是 .push_back() 推送其参数的拷贝,有效地创建了一个新结构。

这是该结构的 header :

#ifndef _STUDENT_INFO__CHAPTER_9_H
#define _STUDENT_INFO__CHAPTER_9_H

#include <string>
#include <iostream>
#include <vector>

class Student_info9{
public:
    Student_info9(){homework = new std::vector<double>;};
    Student_info9(std::istream& is);

    std::string getName() const {return name;};
    double getMidterm() const {return midterm;};
    double getFinal() const {return final;};
    char getPassFail() const {return passFail;};

    std::vector<double> *getHw(){return homework;};

    void setName(std::string n) {name = n;};
    void setMidterm(double m) {midterm = m;};
    void setFinal(double f) {final = f;};


private:
    std::string name;
    double midterm;
    double final;
    char passFail;

    std::vector<double> *homework;
};


#endif  /* _STUDENT_INFO__CHAPTER_9_H */

这是我正在胡闹的代码(请原谅过多的打印语句……一段时间尝试调试的结果:)):

vector<Student_info9> did9, didnt9;

bool did_all_hw9(Student_info9& s)
{
    vector<double>::const_iterator beginCpy = s.getHw()->begin();
    vector<double>::const_iterator endCpy = s.getHw()->end();
    return(find(beginCpy, endCpy, 0) == s.getHw()->end());
}

void fill_did_and_didnt9(vector<Student_info9> allRecords)
{
    vector<Student_info9>::iterator firstDidnt = partition(allRecords.begin(), allRecords.end(), did_all_hw9);


    vector<Student_info9> didcpy(allRecords.begin(), firstDidnt);


    did9 = didcpy;

    vector<Student_info9> didntcpy(firstDidnt, allRecords.end());
    didnt9 = didntcpy;


}

int main(int argc, char** argv) {

    vector<Student_info9> students;

    Student_info9 record;

    for(int i = 0; i < 5; i++)
    {
        students.push_back(record);
    }

    for(int i = 0; i < students.size(); i++)
    {
        students[i].setMidterm(85);
        students[i].setFinal(90);

        students[i].getHw()->push_back(90);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
        students[i].getHw()->push_back(80);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;
        students[i].getHw()->push_back(70);
        std::cout << "student[" << i << "]'s homework vector size is " << students[i].getHw()->size() << std::endl;

        std::cout << "Just pushed back students[" << i << "]'s homework grades" << std::endl;

        if(i == 3)
            students[i].getHw()->push_back(0);
    }

    std::cout << "student[3]'s homework vector size is " << students[3].getHw()->size() << std::endl;

    for(vector<double>::const_iterator it = students[3].getHw()->begin(); it != students[3].getHw()->end(); it++)
        std::cout << *it << " ";

    std::cout << std::endl;

    std::cout << "students[3] has " << ( ( find(students[3].getHw()->begin(),students[3].getHw()->end(), 0) != students[3].getHw()->end()) ? "atleast one " : "no " )
            << "homework with a grade of 0" << std::endl;

    fill_did_and_didnt9(students);


    std::cout << "did9's size is: " << did9.size() << std::endl;
    std::cout << "didnt9's size is: " << didnt9.size() << std::endl;

}

正如您从打印语句中看到的那样,家庭作业成绩似乎只添加到一个 Student_info9 对象,其拷贝似乎填充了整个 vector 。我的印象是,如果您要在单个对象上使用 .push_back() 的连续拷贝,它会创建该对象的拷贝,每个拷贝具有不同的内存地址。

我不确定这是否是问题的根源,但希望有人能为我指明正确的方向。

谢谢。

最佳答案

当您将 StudentInfo 插入 vector 时,它确实被复制了,所以这不是问题所在。问题是包含作业成绩的 vector 。由于您只在 StudentInfo 中存储指向该 vector 的指针,因此当您复制 StudentInfo 时,只会复制指针,而不是 vector 。换句话说,您有许多不同的 StudentInfo,它们都具有指向相同家庭作业 vector 的指针。

要解决这个问题,您应该定义一个复制构造函数来负责复制作业 vector 。

关于C++:为什么我的结构 vector 充当一个结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4748585/

相关文章:

c++ - 在构造函数成员初始化之前调用成员函数的语法

c++ - 如何指定继承类的构造函数定义?

c++ 将 vector<pair<double,double>> 转换为 double*, double*?

c++ - 如果私有(private)变量匹配,则从 vector 中删除对象

matlab - 错误类型为 'min'的输入参数的未定义函数或方法 'struct'

c++ - 我需要删除一个 vector 吗?

c++ - include <sstream> 在 Xcode 9.2 中不起作用

vector - 我应该在Rust中的稀疏Vec或HashMap中存储具有大孔的序列中的无序值吗?

c - 访问/释放动态分配的结构数组时出现不需要的行为

c# - C# 中的结构与类 - 请解释行为