排序多维数组C ++奇怪的行为

时间:2022-06-15 21:37:15

I'm doing some programming practice right now by trying to sort a 2D array on it's first "column".

我正在做一些编程练习,试图在它的第一个“列”上对2D数组进行排序。

I am reading input from a file:

我正在读取文件中的输入:

100 5
8 80
5 20
9 40
3 10
6 30

This is my code:

这是我的代码:

#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

bool helper(vector<long> k, vector<long> l)
{
   return (k[0] < l[0]);
}

int main()
{
   ifstream fi("milk.in");
   ofstream fo("milk.out");
   long price = 0, n, m, i, p, a;
   vector< vector<long> > farmers;
   vector<long> farmer(2,0);
   fi >> n >> m;

   for (i=0; i<n; ++i)
   {
      fi >> p >> a;
      farmer[0] = p;
      farmer[1] = a;
      farmers.push_back(farmer);
   }


   sort(farmers.begin(),farmers.end(),helper);


   for (i=0; i<m; ++i)
   {
      cout << farmers[i][0] << " " << farmers[i][1] << endl;
   }

   return 0;
}

As you can see, I try to sort the input by it's first column (I don't care about the first line at the moment).

正如您所看到的,我尝试按照它的第一列对输入进行排序(我现在不关心第一行)。

However, this is the result:

但是,这是结果:

3 10
5 20
6 30
6 30
6 30

This is the expected result:

这是预期的结果:

3 10
5 20
6 30
8 80
9 40

I can't figure it out.

我无法弄明白。

2 个解决方案

#1


3  

Your first line of your milk.in:

你的第一行milk.in:

100 5

You're going to end up looping through trying to read in 100 inputs from this file because n = 100.

你最终会尝试从这个文件中读入100个输入,因为n = 100。

fi >> n >> m;
for (i=0; i<n; ++i)

If you change milk.in to:

如果你将milk.in改为:

5 5
8 80
5 20
9 40
3 10
6 30

That seems to work.

这似乎有效。

Perhaps a better idea is to just check if you're done reading input from the filestream:

也许更好的想法是检查你是否已经完成了从文件流中读取输入:

for(i = 0; i < n; ++i)
{
    if(!(fi >> farmer[0] >> farmer[1])) break;

    farmers.push_back(farmer);
}

#2


1  

You need this loop to load the data from the file in: And remember to close the file.

您需要此循环来加载文件中的数据:并记住关闭该文件。

   while(!fi.eof()) 
   {
     fi >> n >> m;
    farmer[0] = n;
    farmer[1] = m;
    farmers.push_back(farmer);
   }

   fi.close( );

Also, I made some minor changes to your code:

另外,我对您的代码进行了一些小的更改:

#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

bool helper(vector<long> k, vector<long> l)
{
   return (k[0] < l[0]);
}

int main()
{


   long price = 0, n, m, i, p=0, a=0,numlines=0;
   vector< vector<long> > farmers;
   vector<long> farmer(2,0);

   cout<<"\nLoading data from file: milk.in\n\n";

   ifstream fi("milk.in");
   while(!fi.eof()) 
   {
     fi >> n >> m;
     numlines++;
     cout<<n<<" "<<m<<"\n";

    farmer[0] = n;
    farmer[1] = m;
    farmers.push_back(farmer);
   }

   fi.close( );


   cout<<"--- "<<numlines<<" lines loaded\n";
   cout<<"\n---------------------\n";

   cout<<"\nSorted data:\n\n";

   sort(farmers.begin(),farmers.end(),helper);

   ofstream fo("milk.out");
   for (i=0; i<numlines; ++i)
   {

      cout << farmers[i][0] << " " << farmers[i][1] << endl;
      fo<< farmers[i][0] << " " << farmers[i][1] << endl;

   }
   fo.close();

   cout<<"\n---------------------\n";

   return 0;
}

Output:

Sorted data:

3 10
5 20
6 30
8 80
9 40
100 5

#1


3  

Your first line of your milk.in:

你的第一行milk.in:

100 5

You're going to end up looping through trying to read in 100 inputs from this file because n = 100.

你最终会尝试从这个文件中读入100个输入,因为n = 100。

fi >> n >> m;
for (i=0; i<n; ++i)

If you change milk.in to:

如果你将milk.in改为:

5 5
8 80
5 20
9 40
3 10
6 30

That seems to work.

这似乎有效。

Perhaps a better idea is to just check if you're done reading input from the filestream:

也许更好的想法是检查你是否已经完成了从文件流中读取输入:

for(i = 0; i < n; ++i)
{
    if(!(fi >> farmer[0] >> farmer[1])) break;

    farmers.push_back(farmer);
}

#2


1  

You need this loop to load the data from the file in: And remember to close the file.

您需要此循环来加载文件中的数据:并记住关闭该文件。

   while(!fi.eof()) 
   {
     fi >> n >> m;
    farmer[0] = n;
    farmer[1] = m;
    farmers.push_back(farmer);
   }

   fi.close( );

Also, I made some minor changes to your code:

另外,我对您的代码进行了一些小的更改:

#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

bool helper(vector<long> k, vector<long> l)
{
   return (k[0] < l[0]);
}

int main()
{


   long price = 0, n, m, i, p=0, a=0,numlines=0;
   vector< vector<long> > farmers;
   vector<long> farmer(2,0);

   cout<<"\nLoading data from file: milk.in\n\n";

   ifstream fi("milk.in");
   while(!fi.eof()) 
   {
     fi >> n >> m;
     numlines++;
     cout<<n<<" "<<m<<"\n";

    farmer[0] = n;
    farmer[1] = m;
    farmers.push_back(farmer);
   }

   fi.close( );


   cout<<"--- "<<numlines<<" lines loaded\n";
   cout<<"\n---------------------\n";

   cout<<"\nSorted data:\n\n";

   sort(farmers.begin(),farmers.end(),helper);

   ofstream fo("milk.out");
   for (i=0; i<numlines; ++i)
   {

      cout << farmers[i][0] << " " << farmers[i][1] << endl;
      fo<< farmers[i][0] << " " << farmers[i][1] << endl;

   }
   fo.close();

   cout<<"\n---------------------\n";

   return 0;
}

Output:

Sorted data:

3 10
5 20
6 30
8 80
9 40
100 5