1

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

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 循环,但似乎无法获得有效的东西。任何帮助将不胜感激,请问我是否需要包括其他任何内容。谢谢!

4

3 回答 3

0

看你的例子,

for each line 
    for i = 1 to N-1th element
        for j = i+1 to Nth element
            print (i,j)
于 2018-10-16T06:23:31.360 回答
0

如果您要打印出数组中所有可能的索引。你正在寻找正确的方向。您可以使用 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 数组,只需添加更多变量以递增每个循环并设置循环以在变量达到各自的最大数组索引长度时重置变量。

于 2018-10-16T05:27:17.783 回答
0

我会在这里发布我的答案,因为由于一些建议,我能够弄清楚。还要感谢您对其余代码的所有反馈,自从原始帖子以来,我肯定使它变得更好。我的 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-10-16T18:51:25.417 回答