【问题标题】:Getting all pairs of numbers in a 2D array c++获取二维数组c ++中的所有数字对
【发布时间】:2018-10-16 04:48:48
【问题描述】:

我需要打印从文本文档中读取的每一行中的所有数字对。一个示例文本文档是:

6 8
1 3 5
2 3 4
3 6 5
7 6 8
4 6
7 5

其中第一行是超图的网络数 (6) 和单元数 (8)。其余的行是网络中的单元格。所以网络 1 由单元格 1、3 和 5 组成,网络 2 由单元格 2、3 和 4 组成,依此类推。为了把这个网表变成一个实际的图表,我需要遍历每一行,基本上取每一行数字的所有组合。因此,在阅读了第一个网络之后,我希望能够用 (1,3)、(1,5) 和 (3,5) 制作一个图表,然后沿着网表向下并添加到图表中。到目前为止,我能够从文本文件中读取所有内容并打印出我放入二维数组的各个单元格。这是我的代码:

    int main() {

ifstream infileHGR; // set stream for hypergraph text file
string inputFileName = "structP.hgr"; // input hypergraph filename here
infileHGR.open(inputFileName, ios::in);

clock_t start = clock(); // start clock

string line;
string data[2]; // initialize data array to take in # of nets and # of cells
int nets = 0;
int cells = 0;

// Reads in the first line of the text file to get # for nets and cells
    getline(infileHGR, line);
    stringstream ssin(line);
    int i = 0;

    while (ssin.good() && i < 2) { // error checking to make sure first line is correct format
    ssin >> data[i];
    i++;
    }
    nets = atoi(data[0].c_str()); // set first number to number of nets 
    cells = atoi(data[1].c_str()); // set second number to number of cells

    freopen("output.txt", "w", stdout); // writes outptut to text file

// TESTING PURPOSES
cout << "Number of nets = " << nets << endl;
cout << "Number of cells = " << cells << endl;

// while loop to go through rest of the hgr file to make hypergraph (starts at line 2)
string str;
int count = 1; // counter for nets

while (infileHGR.good()) {
    getline(infileHGR, str);
    stringstream in(str);
    int i = 0;
    // have the line in str

    int n = 1; // start at 1, spaces + 1 = number of nodes per net
    for (int i = 0; i < str.length(); ++i) {
        if (str.at(i) == ' ') {
            n++; // n is number of cells in the net
        }
    }
    // testing
    //cout << "str = " << str << endl;
    //cout << "n = " << n << endl;

    int number;

    vector<vector<int> > netList;
    vector<int> temp;
    while (in >> number){
        temp.push_back(number);
    }
    netList.push_back(temp);
    //printNetList(temp); // test to see if info is being put into the vectors

    // loop through the 2d vector
    for (const auto& inner : netList) {

        cout << "net " << count << " = "; //TESTING PURPOSES
        for (const auto& item : inner) {
            cout << item << " ";
        }
        count = count + 1;
    }
    cout << endl;
} 
clock_t stop = clock(); // end clock
infileHGR.close();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f", elapsed);
system("pause"); //for original testing
return 0;

}

我使用向量是因为每个输入文件都有不同的大小,有些包含很多网络,有些网络中有多达 20 个单元格。我需要帮助从网表中获取所有对(坐标)并将它们打印出来以显示所有它们。我经常使用 for 循环,但似乎无法获得有效的东西。任何帮助将不胜感激,请问我是否需要包括其他任何内容。谢谢!

【问题讨论】:

  • 我使用了向量,因为每个输入文件的大小都不同很好。 while (infileHGR.good()) 不幸的是,在阅读之前测试阅读是否成功。 getline(infileHGR, str); 可能会失败,并且程序无论如何都会尝试解析它。 while (getline(infileHGR, str)) 快速清理,阅读然后测试。
  • 关于ssin &gt;&gt; data[i];。在这种情况下,建议直接阅读nets 和cells。容易得多。 if (ssin &gt;&gt; nets &gt;&gt; cells) { use 'em } else { report file read error }

标签: c++ parsing vector graph hypergraph


【解决方案1】:

如果您希望从数组中打印出所有可能的索引。你正在寻找正确的方向。您可以使用 for 循环来完成,实际上我在分配时遇到了这个问题。看看这个,它应该返回所有可能的索引:

int b, c = 0;
int userIndex_A;
int userIndex_B;

cin >> userIndex_A;
cin >> userIndex_B;

 //This is so you can have variable array lengths

int Arr[userIndex_A][userIndex_B];

for(int a = 0; c < 10; a++){
        //The reason I put c in the for loop, to tell it when to stop, is because when c is equal to 10, that's the last index being paired
            cout << "Array Index: "<< b << ", "<< c << endl;
        if(b == (userIndex_A - 1)){
        //userIndex-A is the array length, but 10 doesn't exist, so subtract 1
            b = 0;
            c++;
        //if b is equal to max index value entered, set it to zero and add one to c.  You only add one to c when you want to start with the next set of index.
        }
        b++;    
        //after each loop, increment the variables
    }

这也适用于 3D 和 4D 数组,只需添加更多变量以递增每个循环并设置循环以在变量达到各自的最大数组索引长度时重置变量。

【讨论】:

  • int Arr[userIndex_A][userIndex_B]; 符合标准的 C++ 中的无效代码。提问者使用std::vector 的另一个重要原因。
  • int b, c = 0; 没有初始化b,当b 未初始化使用时,以后调试会很有趣。
  • 现在我的程序确实打印出我拥有的二维向量的所有索引。我更需要帮助来从这些索引中获取 2 个整数的每个组合。网络 1、网络 2 等中所有数字的组合。
  • 可变数组长度只是为了让用户可以了解数字的来源。他可以使用向量,但主要答案在于 for 循环和变量增量
【解决方案2】:

看看你的例子,

for each line 
    for i = 1 to N-1th element
        for j = i+1 to Nth element
            print (i,j)

【讨论】:

  • 同一行中永远不会有重复的数字。至于顺序,(1,5)与(5,1)相同,但只应打印其中一个。如果该行有 4 个数字 (2 5 7 9),我只是打印出你认为我认为会得到 (2, 5)、(5, 7) 和 (7, 9) 的元素,但我会错过 (2, 7)、(2, 9) 和 (5, 9)。但也许我读错了你的建议?
【解决方案3】:

我会在这里发布我的答案,因为我能够通过一些建议来解决这个问题。还要感谢您对其余代码的所有反馈,自从原始帖子以来,我肯定使它变得更好。我的 for 循环虽然用于循环遍历 2D 向量并打印出所有对是这样的(您可以忽略输出的权重变量):

    for (vector<vector<int>> ::iterator i = netList.begin(); i != netList.end(); ++i) {
        for (vector<int> ::iterator j = temp.begin(); j != temp.end() - 1; ++j) {
            for (auto k = temp.begin() + 1; k != temp.end(); ++k) {
                if (*j != *k) {
                    cout << *j << " " << *k << " " << weight << endl;
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多