排序数组并在输出中获得额外的第一行随机符号

时间:2019-06-17 18:44:17

标签: c++ sorting

我写了一个程序,其中程序按每个人的时间对人进行排序,如果时间相同,则程序按名称按字母顺序进行排序。一切正常,只是我得到的第一行充满了随机符号。我当然可以写if函数并说不显示第一行,但这是一件好事吗?

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
struct people {
  string full_name;
  int h;
  int min;
  int sec;
};
bool comp(const people &p1, const people &p2) {
  return (p1.min < p2.min || p1.min == p2.min && p1.sec < p2.sec ||
          p1.min == p2.min && p1.sec == p2.sec && p1.full_name < p2.full_name);
}

int main() {
  int time;
  string fullnamechange[30];
  people peo[30];

  cin >> time;
  for (int i = 0; i < time; i++) {
    getline(cin, peo[i].full_name); // taking everything into a string
    fullnamechange[i] = peo[i].full_name;
    fullnamechange[i].erase(fullnamechange[i].begin(),
                            fullnamechange[i].end() - 8);
    peo[i].h = atoi(fullnamechange[i].c_str());
    fullnamechange[i].erase(fullnamechange[i].begin(),
                            fullnamechange[i].end() -
                                5); // changing time in string to int
    peo[i].min = atoi(fullnamechange[i].c_str());
    fullnamechange[i].erase(fullnamechange[i].begin(),
                            fullnamechange[i].end() - 2);
    peo[i].sec = atoi(fullnamechange[i].c_str());
  }
  for (int i = 0; i < time; i++) { // erasing time from string
    peo[i].full_name.erase(peo[i].full_name.begin() + 20,
                           peo[i].full_name.end());
  }
  sort(peo, peo + time, comp);
  cout << endl;
  for (int i = 0; i < time; i++) {
    cout << peo[i].full_name << " " << peo[i].min << " " << peo[i].sec << endl;
  }
  return 0;
}

/*
input for example:
6
Petras A. Petraitis    0 20 00
Jurgis Jurgutis        0 12 59
Romas Jonas            0 15 12
Zigmas Nosis           0 23 9
Rimas Senasis          0 15 12

output I get:
 em3╣Mg n Ç         0 0      //random numbers I get
Jurgis Jurgutis      12 59
Rimas Senasis        15 12
Romas Jonas          15 12
Petras A. Petraitis  20 0
Zigmas Nosis         23 9 */

0 个答案:

没有答案