Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于用户输入的二维动态数组_C++_Arrays_Visual C++_Multidimensional Array_Dynamic - Fatal编程技术网

C++ 基于用户输入的二维动态数组

C++ 基于用户输入的二维动态数组,c++,arrays,visual-c++,multidimensional-array,dynamic,C++,Arrays,Visual C++,Multidimensional Array,Dynamic,情景: 从文件中读取数字并相应地创建动态二维数组 数据文件的第一行表示房间,其余行表示房间中的人数 例如: 4 4 6 5 3 4. 4. 6. 5. 3. 总共4个房间,第一个房间有4个人,第二个房间有6个人 到目前为止,这是我的代码,我如何检查我是否创建了大小正确的动态数组 #include <iostream> #include <fstream> #include <string> #include <sstream> using nam

情景: 从文件中读取数字并相应地创建动态二维数组 数据文件的第一行表示房间,其余行表示房间中的人数

例如:

4 4 6 5 3 4. 4. 6. 5. 3. 总共4个房间,第一个房间有4个人,第二个房间有6个人

到目前为止,这是我的代码,我如何检查我是否创建了大小正确的动态数组

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream readFirstLine("data.txt");
    ifstream readData("data.txt");

    string line;

    int numRoom, numPerson = 0;

    int i = -1;

    while (getline(readFirstLine, line))
    {
        istringstream linestream(line);

        if (i == -1)
        {
            linestream >> numRoom;
            cout << "numRoom:" << numRoom << endl;

            break;
        }

    }

    readFirstLine.close();

    int** numRoomPtr = new int*[numRoom];

    while (getline(readData, line))
    {
        istringstream linestream(line);

        if (i == -1)
        {

        }
        else
        {
            linestream >> numPerson;
            numRoomPtr[i] = new int[numPerson];

            cout << "i:" << i << endl;
            cout << "numPerson:" << numPerson<< endl;
        }


        i++;
    }

    readData.close();




    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream readFirstLine(“data.txt”);
ifstream readData(“data.txt”);
弦线;
int numRoom,numPerson=0;
int i=-1;
while(getline(readFirstLine,line))
{
istringstream linestream(线);
如果(i==-1)
{
linestream>>numRoom;

cout执行当前程序的更好方法是使用,如下所示:

#include <iostream>
#include <vector>
#include <fstream>

int main()
{
    std::ifstream dataFile("data.txt");

    // Get the number of "rooms"
    unsigned roomCount;
    if (!(dataFile >> roomCount))
    {
        // TODO: Handle error
    }

    // Create the vector to contain the rooms
    std::vector<std::vector<int>> rooms(roomCount);

    for (unsigned currentRoom = 0; currentRoom < roomCount; ++currentRoom)
    {
        unsigned personCount;
        if (dataFile >> personCount)
        {
            rooms[currentRoom].resize(personCount);
        }
        else
        {
            // TODO: Handle error
        }
    }

    // Don't need the file anymore
    dataFile.close();

    // Print the data
    std::cout << "Number of rooms: " << rooms.size() << '\n';
    for (unsigned currentRoom = 0; currentRoom < rooms.size(); ++currentRoom)
    {
        std::cout << "Room #" << currentRoom + 1 << ": " << rooms[currentRoom].size() << " persons\n";
    }
}
#包括
#包括
#包括
int main()
{
std::ifstream数据文件(“data.txt”);
//获取“房间”的数量
未签名的房间数;
如果(!(数据文件>>roomCount))
{
//TODO:句柄错误
}
//创建包含房间的向量
标准:病媒室(房间数);
对于(无符号currentRoom=0;currentRoom>个人帐户)
{
房间[currentRoom]。调整大小(personCount);
}
其他的
{
//TODO:句柄错误
}
}
//我不再需要这个文件了
dataFile.close();
//打印数据

std::cout除非这是一个使用指针和动态分配的练习,否则不要这样做。改用。除此之外,为什么对第一个输入使用循环?为什么不对另一个输入使用
循环?至于你的问题,你能详细说明一下吗?你从文件中读取的数字读取正确吗?是
新[]
不抛出异常?您尝试过吗?它是否达到了您的预期效果?第一个循环用于从文本文件中提取第一行的值并中断循环第二个循环用于提取第二行的值,直到最后一行为止不抛出任何异常。这是输出:numRoom:4 i:0 numstation:4 i:1 numstation:6 i:2 numstation:5 i:3 numstation:3这个方法比较简单,但我想我的讲师希望我使用指针法lol,刚刚意识到,但不知怎的,房间的大小从4增加到了8,应该只有4。我还在考虑如何使用指针