C++ 如何使用户能够输入将放置在多维数组中的值

问题描述

这可能是一个非常像初学者的问题。

但是,我有一个处理图形的算法,而我目前拥有的只是预先输入的值。我想让它使用户能够输入图形的边缘,并且将用值填充多维数组。

这里有一些代码

#include <stdbool.h>
#include <stdio.h>
#include <iostream>
using namespace std;

#define vertex_count 4
int main()
{

   // int vertex_count;

    bool graph[vertex_count][vertex_count] =
    {
        { 0,1,1 },{ 1,0 },};
    int used_colors = 3;
    graphColor(graph,used_colors);

    return 0;
}

我假设我必须要求用户输入有多少个顶点和边,当用户输入边时,我会将它们一一放入数组中。

然而,我遇到了一个问题,当顶点计数没有定义但输入时,函数说它没有声明,等等。

有没有人知道这样做的最佳方法?

先谢谢你!

解决方法

您可以用来收集可变数据量的一种技术是首先询问用户他们需要多少个顶点,然后在 std::cin 循环中for 来收集实际数据。

std::vector<bool> graph;
int vertex_count = 0;
std::cout << "How many vertices?\n";
std::cin >> vertex_count;
std::cout << "Enter the graph:\n";
int answer = 0;
for (int i = 0; i < vertex_count * vertex_count; ++i) {
    std::cin >> answer;
    graph.push_back(answer);
}

我推荐使用 std::vector<bool> 来保存值,因为它是一个可变大小的数组。要像访问二维数组一样访问这个一维数组中的值,请使用表达式 graph[y*vertex_count+x]

运行程序时,可以输入如下数据:

How many vertices?
4
Enter the graph:
1 0 1 0
1 1 0 1
0 0 0 0
1 1 0 0

因为 std::cin 分隔所有空格,而不仅仅是 \n

,

我假设我必须要求用户输入多少 顶点和边有

我认为这是一个坏主意。要求用户计算边和顶点是非常乏味的。假设用户将完美地执行此任务只会在您的程序因计数错误而崩溃时导致沮丧。一旦节点和链接的数量达到数千个,就像他们在现实世界中所做的那样,计数任务就变得几乎不可能了。

最好让输入阅读代码自己计算。

这是我建议的算法(我自己总是使用)

LOOP over input file specifying edges one at a time until file ends
   check if first vertex is already present in graph.  If not then add new vertex.
   repeat for second vertex
   add edge between vertices

您可以在 https://github.com/JamesBremner/PathFinder

查看实现此功能的 C++ 代码

这是一个例子

    std::string line;
    while (std::getline(inf,line))
    {
        std::cout << line << "\n";
        auto token = ParseSpaceDelimited(line);
        if (!token.size())
            continue;
        switch (token[0][0])
        {

        case 'l':
            if (token.size() != 4)
                throw std::runtime_error("cPathFinder::read bad link line");
            addLink(
                findoradd(token[1]),findoradd(token[2]),atof(token[3].c_str()));
            break;

        case 's':
            if (token.size() != 2)
                throw std::runtime_error("cPathFinder::read bad start line");
            myStart = find(token[1]);
            break;

        case 'e':
            if (token.size() != 2)
                throw std::runtime_error("cPathFinder::read bad end line");
            myEnd = find(token[1]);
            break;
        }
    }
}

此代码正在读取的文件格式的文档位于 https://github.com/JamesBremner/PathFinder/wiki/Costs

,

所以就像其他人说的你可能想使用 Vector 一样,虽然我不想承认我是新手,(我还是有点新),所以 IDk 如何使用向量,我认为这就是下一个学期。我仍然可以回答你的问题,因为我们喜欢从字面上学到这一点。

您想创建一个指向数组开头的指针,然后使用 new 创建数组。

    using namespace std;
    int main(){
        //making pointer
        int **Dynamic2D;
        int x,y;
        cout << "Enter x,then y,separated by a space"<<endl;
        cin>>x>>y;
        cout<<endl;

        Dynamic2D = new int* [x];
        for(int i=0;i<x;i++){
            //making pointer to each y
            Dynamic2D[i]= new int [y];
            }
         //filling array
         for (int i=0; i<x;i++){
            for (int j=0; j<y; j++){
                cout <<"Enter position"<< i << j <<": "<<endl;
                cin>>Dynamic2D[i][j];
                }
            }
        //printing array
        for (int i=0; i<x; i++){
            for (int j=0; j<y; j++){
                cout << Dynamic2D[i][j];
                }
           }
     }

所以我 gtg 中可能有拼写错误,但应该是一个简单的动态 2d int 数组。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...