【问题标题】:Solution to c2100 with iterator?用迭代器解决c2100?
【发布时间】:2015-09-14 03:30:40
【问题描述】:

所以这是我的错误程序:

// ConsoleApplication42.cpp : Defines the entry point for the console application.
//

    #include "stdafx.h"
    #include <iostream>
    #include <list>
    #include <cstdlib>
    #include <time.h>
    #include <algorithm>
    #include <string>
    #include <vector>

    using namespace std;


    const int numberOfStudents = 9;
    string names[numberOfStudents] = {"Abe","Billy","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac"};

    struct StudentInfo {
        string name;
        int grade;



        bool operator< (int grade){
            return grade < grade;
        }

        bool operator< (string name){
            return name < name;
        }
        };



    void populateStudentRecords(vector<StudentInfo>Students,vector<int>::iterator iter, int x){

        for(auto iter = Students.begin(); iter != Students.end(); ++iter){
            iter->name = names[x];
            iter->name.push_back(x);
            iter->grade = x++;
            x = x++;

        }



    }

    bool sortByName(const StudentInfo x, const StudentInfo y){
        return x.name < y.name;
    }

    bool sortByGrade(const StudentInfo x, const StudentInfo y){
        return x.grade < y.grade;
    }

    void displayRecords(vector<StudentInfo>Records,vector<int>::iterator iter){
        for(auto iter = Records.begin(); iter != Records.end(); ++iter){
            cout<<*iter->name<<" -"<<*iter->grade<<endl;
        }
    }

    void displayMaxAndMinGrade(vector<StudentInfo>Records,vector<int>::iterator iter){
        for(auto iter = Records.begin(); iter != Records.end(); ++iter){
            cout<<*iter->name<<" - " <<*iter->grade<<endl;
            iter = Records.end();
            cout<<*iter->name<<" - " <<*iter->grade<<endl;
        }
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        vector<StudentInfo>Records (numberOfStudents);
        vector<int>::iterator iter;


        populateStudentRecords(Records,iter,0);
        sort(Records.begin(),Records.end(),sortByName);
        displayRecords(Records,iter);
        sort(Records.begin(),Records.end(),sortByGrade);
        cout<<" "<<endl;
        displayMaxAndMinGrade(Records, iter);

        return 0;
    }

在 displayRecords 函数和 displayMaxAndMin 函数中,我在迭代器旁边有 * 符号。我希望计算机在向量中结构的每次出现中显示这些变量的值。但是,当我尝试构建程序时出现错误 c2100。我试图在不包含 * 符号的情况下运行程序,但这会显示每个变量的地址并导致崩溃。我该如何解决?谢谢。

【问题讨论】:

标签: c++ vector iterator


【解决方案1】:

您需要阅读一本关于使用 C++ 编程的介绍性书籍。这段代码充满了错误。你不知道如何使用指针/引用/迭代器。

  1. 从一本好书中了解复制、参考和指针。请点击此答案末尾的链接。
  2. 变量应限制在它们的使用范围内。当它们并不真正需要时,停止向您的函数传递这么多参数。
  3. 您同时使用了间接和取消引用运算符,而您应用运算符的类型只需要一个。这就是您出错的原因。
  4. 您在每个循环中将数组索引增加两次,这使其超出范围。
  5. 您正在使用容器的 end() 成员函数,并假设它返回指向容器中最后一个元素的迭代器。
  6. 您想打印最低和最高等级,但要循环遍历整个容器。

看在你的份上,我已经修复了最明显的错误:

#include <iostream>
#include <list>
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

const int numberOfStudents = 9;
string names[numberOfStudents] = { "Billy", "Abe","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac" };

struct StudentInfo
{
    string name;
    int grade;
};

void populateStudentRecords(vector<StudentInfo>& Students)
{
    int x{ 0 };
    for (auto iter = Students.begin(); iter != Students.end(); ++iter)
    {
        iter->name = names[x];
        iter->name.push_back(x);
        iter->grade = ++x;
    }
}

bool sortByName(const StudentInfo x, const StudentInfo y)
{
    return x.name < y.name;
}

bool sortByGrade(const StudentInfo x, const StudentInfo y)
{
    return x.grade < y.grade;
}

void displayRecords(vector<StudentInfo>Records)
{
    for (auto iter = Records.begin(); iter != Records.end(); ++iter)
    {
        cout << iter->name << " -" << iter->grade << endl;
    }
}

void displayMaxAndMinGrade(vector<StudentInfo>Records)
{
    auto iter = Records.begin();
    cout << iter->name << " - " << iter->grade << endl;
    iter = Records.end() - 1;
    cout << iter->name << " - " << iter->grade << endl;
}

int main()
{
    vector<StudentInfo>Records(numberOfStudents);

    populateStudentRecords(Records);
    sort(Records.begin(), Records.end(), sortByName);
    displayRecords(Records);
    sort(Records.begin(), Records.end(), sortByGrade);
    cout << " " << endl;
    displayMaxAndMinGrade(Records);

    return 0;
}

请帮自己一个忙,并访问以下链接:

The Definitive C++ Book Guide and List

【讨论】:

    猜你喜欢
    • 2013-08-12
    • 2022-10-20
    • 1970-01-01
    • 2021-12-11
    • 2016-03-19
    • 2023-04-08
    • 1970-01-01
    • 2021-01-29
    • 2013-07-22
    相关资源
    最近更新 更多