双维数组读取错误

时间:2018-02-15 12:05:09

标签: c++ c++11

我有一个双维std :: string数组,我需要将其作为函数 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;
}

2 个答案:

答案 0 :(得分:1)

你对数组,指针和字符串有很大的困惑。作为@Quentin和  @molbdnilo指出你,你正在进行从std :: strings的二维数组到指向字符串指针的指针的C风格转换,并且数组既不是指针也不是指针都是数组。

我的猜测是,您希望根据姓名对所有学生进行排序,同时保留与相应学生相关的其余学生信息。

一些建议:

  1. 只要可以使用std :: array,就不要使用C风格的数组。
  2. 要在代码中定义常量,创建一个常量变量,不要写,例如,5作为大小,当你想要改变那个常量值时,这可能涉及代码不同部分的多个变化,因为它可以写在多个地方。
  3. 您不需要在示例中使用指针。在这种情况下,它们没有意义。
  4. 您尝试实现的example使用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
      Bob Green 3 2020
      Joe Lime 15 2019年   Jose Lemon 25 2016年
      SallyAnne Green 2017年1月

答案 1 :(得分:1)

我修改了你的代码并让它工作:

#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;
}

with struct:

#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;
}

我希望你看到你的代码有多清洁