双维数组读取错误

Double dimension array read error

本文关键字:取错误 读取 数组      更新时间:2023-10-16

我有一个双维std ::字符串阵列,我需要将其作为函数参数 sortstring 进行传递,但是当变量 student时,我会遇到一个运行时错误首先阅读。" 0"是通过COUT输出,而不是" 1"。我对我错的地方有任何想法吗?

这是我的代码:

#include <iostream>
#include <string>
void sortString(std::string **student, std::string **output, int size)
{
    std::string names[5];
    std::cout << "0" << std::endl;
    for (int i = 0 ; i < size ; i++)
        names[i] = student[i][0];
    std::cout << "1" << std::endl;
}
int main()
{
    std::string student1 [ ] = {"Joe Lime",         "15", "2019"};
    std::string student2 [ ] = {"Bob Green",        "3",  "2020"};
    std::string student3 [ ] = {"SallyAnne Green" , "1",  "2017"};
    std::string student4 [ ] = {"Annie Blue",       "10", "2020"};
    std::string student5 [ ] = {"Jose Lemon",       "25", "2016"};
    int const size = 5;
    std::string student [5][3] = {student1, student2, student3, student4, student5};
    std::string sortedByName[5][3];
    sortString((std::string**)student, (std::string**)sortedByName, size);
    return 0;
}

** -------------编辑-------------- **

我想做与一维数组相同的事情,所以我不明白为什么它不适用于二维数组

,例如,这有效:

#include <iostream>
int test(int *a)
{
    std::cout << a[0] << std::endl;
}
int main()
{
    int a[] = {1,2,3,4,5,6,7};
    test(a);
}
int test(int *a)
{
    std::cout << a[0] << std::endl;
}

您对数组,指针和字符串有很大的混乱。作为@quentin和 @molbdnilo指出了您的指出,您正在从bidemensional阵列的std ::字符串到指向字符串指针的指针进行C风格的转换,而数组都不是指针,也不是指针。

我的猜测是,您想根据他们的名字对所有学生进行分类,同时将其余的学生信息与相应的学生保持联系。

一些建议:

  1. 只要您可以使用std ::数组。
  2. ,请勿使用C风格数组。
  3. 要在代码中定义常数,创建一个常数变量,例如,在您想更改该常数值时,这可能涉及代码不同部分的多个更改,因为它可以写在多个位置。
  4. 您不需要在示例中使用指针。在这种情况下,它们没有意义。

您要实现的示例使用std :: sort函数:


#include <string>
#include <array>
#include <iostream>
#include <algorithm>
const unsigned NUMBER_OF_STUDENTS = 5;
const unsigned NUMBER_OF_STUDENT_DATA_FIELDS = 3;
using studentType = std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS>;
int main()
{
    std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS> student1 = {"Joe Lime",         "15", "2019"};
    std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS> student2 = {"Bob Green",        "3",  "2020"};
    std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS> student3 = {"SallyAnne Green" , "1",  "2017"};
    std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS> student4 = {"Annie Blue",       "10", "2020"};
    std::array<std::string, NUMBER_OF_STUDENT_DATA_FIELDS> student5 = {"Jose Lemon",       "25", "2016"};
    std::array<studentType, NUMBER_OF_STUDENTS> students = {student1, student2, student3, student4, student5};
    std::sort(students.begin(), students.end(), [](const studentType& student1, const studentType& student2) {
        // first string in the array is the name
        const std::string& nameStudent1 = student1.front(); 
        const std::string& nameStudent2 = student2.front();
        // we return if the name of student 1 is lexicographically smaller than the name of student 2
        return nameStudent1 < nameStudent2;
    });
    // Let's print the students to see we have order everything correctly
    for (const auto& student: students) // for each student
    {
        for (const auto& studentData : student) // for each field in the student string
        {
            std::cout << studentData << " ";
        }
        std::cout << "n"; // jump to the next line
    }
}

Annie Blue 10 2020
鲍勃·格林3 2020
Joe Lime 15 2019
Jose Lemon 25 2016
Sallyanne Green 1 2017

我稍微修复了您的代码,并使其正常工作:

#include <iostream>
#include <string>
void sortString(std::string student[][3], std::string output[][3], int size)
{
    std::string names[5];
    std::cout << "0" << std::endl;
    for (int i = 0; i < size; i++)
    {
        names[i] = student[i][0];
        std::cout << names[i] << "n";
    }
    std::cout << "1" << std::endl;
}
int main()
{
    int const size = 5;
    std::string students[5][3] = 
    { 
        { "Joe Lime", "15", "2019" },
        { "Bob Green", "3", "2020" },
        { "SallyAnne Green", "1", "2017" },
        { "Annie Blue", "10", "2020" },
        { "Jose Lemon", "25", "2016" }
    };
    std::string sortedByName[5][3];
    sortString(students, sortedByName, size);
    return 0;
}

,但我强烈建议您使用数组,向量和结构/类。按照矢量和阵列以及向量和构造的示例

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
void sortString(std::vector<std::array<std::string, 3>>& students)
{
    // for example: print all names with range base for loop
    for (const auto& s : students)
    {
        std::cout << s[0] << std::endl;
    }
    // for example: print all names with "normal" for loop
    for (std::size_t i = 0; i < students.size(); ++i)
    {
        std::cout << students[i][0] << std::endl;
    }
    // sort by name
    std::sort(std::begin(students), std::end(students), [](const std::array<std::string, 3>& a, const std::array<std::string, 3>& b){ return a[0] < b[0]; });
}
int main()
{
    int const size = 5;
    std::vector<std::array<std::string, 3>> students;
    students.push_back({  "Joe Lime", "15", "2019" });
    students.push_back({  "Bob Green", "3", "2020" });
    students.push_back({  "SallyAnne Green", "1", "2017" });
    students.push_back({  "SallyAnne Green", "1", "2017" });
    students.push_back({  "Jose Lemon", "25", "2016" });
    sortString(students);
    return 0;
}

结构:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Student
{
    std::string name;
    std::string dontKnow;
    std::string year;
};
void sortString(std::vector<Student>& students)
{
    // for example: print all names with range base for loop
    for (const auto& s : students)
    {
        std::cout << s.name << std::endl;
    }
    // for example: print all names with "normal" for loop
    for (std::size_t i = 0; i < students.size(); ++i)
    {
        std::cout << students[i].name << std::endl;
    }
    // sort by name
    std::sort(std::begin(students), std::end(students), [](const Student& a, const Student& b){ return a.name < b.name; });
}
int main()
{
    int const size = 5;
    std::vector<Student> students;
    students.push_back({ "Joe Lime", "15", "2019" });
    students.push_back({ "Bob Green", "3", "2020" });
    students.push_back({ "SallyAnne Green", "1", "2017" });
    students.push_back({ "SallyAnne Green", "1", "2017" });
    students.push_back({ "Jose Lemon", "25", "2016" });
    sortString(students);
    return 0;
}

希望您看到您的代码获得多少清洁