gpt4 book ai didi

c++ - 获取二维数组中的所有数字对 C++

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:33 24 4
gpt4 key购买 nike

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

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;

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

最佳答案

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

关于c++ - 获取二维数组中的所有数字对 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52828098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com