在二维数组中获取所有数字对C ++

时间:2018-10-16 04:48:48

标签: c++ parsing vector graph hypergraph

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

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)制作图形,然后在网表中查找并添加到图形中。到目前为止,我已经能够从文本文件中读取所有内容,并打印出放入2D数组中的单个单元格。这是我的代码:

    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循环弄得一团糟,但似乎无法得到有用的东西。任何帮助将不胜感激,只是问我是否需要包括其他任何东西。谢谢!

3 个答案:

答案 0 :(得分: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数组,只需添加更多变量以增加每个循环,并设置循环以在变量达到各自的最大数组索引长度时重置该变量。

答案 1 :(得分:0)

看你的例子,

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

答案 2 :(得分:0)

我将在此处发布答案,因为我可以通过一些建议来弄清楚答案。也要感谢您对其余代码的所有反馈,自从原始帖子以来,我肯定使它变得更好。我的for循环虽然是用于遍历2D向量并打印出所有对的(但您可以忽略输出的weight变量):

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