Некорректная сортировка
Не сортируются числовые значения, остаются на своих местах. Когда сортирую фамилии по алфавиту, то айдишник и остальное остается на своих прежних местах при выводе на экран, есть мысли почему они не перемещаются с фамилией? SWAP тоже не работает...
void SortingBySourname()
{
ifstream reading("Data.txt");
int n = 0, temp = 0;
int arr_ID[100]{};
int arr_KV[100]{};
int arr_Floors[100]{};
string arr_Surname[100];
bool flag = true;
reading >> n;
if (reading)
{
// чтение данных из файла и сохранение в массивы
for (int i = 0; i < n; i++)
{
reading >> arr_ID[i];
reading >> arr_KV[i];
reading >> arr_Floors[i];
reading >> arr_Surname[i];
}
do
{
flag = true;
sort(arr_Surname, arr_Surname + n);
for (int i = 0; i < n - 1; i++)
{
if (arr_Surname[i].compare(arr_Surname[i + 1]) > 0)
{
swap(arr_Surname[i], arr_Surname[i + 1]);
flag = false;
if(flag)
{
for (int i = 0; i < n - 1; i++)
{
temp = arr_ID[i];
arr_ID[i] = arr_ID[i + 1];
arr_ID[i + 1] = temp;
temp = arr_KV[i];
arr_KV[i] = arr_KV[i + 1];
arr_KV[i + 1] = temp;
temp = arr_Floors[i];
arr_Floors[i] = arr_Floors[i + 1];
arr_Floors[i + 1] = temp;
}
}
// swap(arr_ID[i], arr_ID[i + 1]);
// swap(arr_KV[i], arr_KV[i + 1]);
// swap(arr_Floors[i], arr_Floors[i + 1]);
}
}
} while (!flag);
// вывод данных
for (int i = 0; i < n; i++) {
cout << "ID: " << arr_ID[i] << endl;
cout << "кв.м: " << arr_KV[i] << endl;
cout << "Этажность: " << arr_Floors[i] << endl;
cout << "Фамилия: " << arr_Surname[i] << endl;
cout << "____________________________________________\n" << endl;
}
}
}
Ответы (1 шт):
Автор решения: NunOfIt
→ Ссылка
#include <fstream>
#include <iostream>
#include <algorithm>
struct House {
int id;
size_t area;
size_t floors;
std::string owner;
friend std::ostream& operator<<(std::ostream&, const House&);
};
std::ostream& operator<<(std::ostream& out, const House& other) {
out << "House(";
out << "id: " << other.id << "; ";
out << "area: " << other.area << "; ";
out << "floors: " << other.floors << "; ";
out << "owner: " << other.owner << ')';
return out;
}
char print(House* arr, size_t len) {
std::cout << '[';
if(len) {
std::cout << arr[0];
for(size_t i = 1; i < len; ++i)
std::cout << ", " << arr[i];
}
std::cout << ']';
return '\0';
}
class Comparator {
public:
bool operator()(const House& h1, const House& h2) {
return h1.owner.compare(h2.owner) < 0;
}
};
House* scan(const std::string& data_path, size_t& n) {
House* arr = nullptr;
std::ifstream ifs(data_path);
if(!ifs.is_open()) {
throw std::runtime_error("Can not open the file: " + data_path);
}
ifs >> n;
arr = new House[n];
for(size_t i = 0; i < n; ++i) {
ifs >> arr[i].id;
ifs >> arr[i].area;
ifs >> arr[i].floors;
ifs >> arr[i].owner;
}
ifs.close();
return arr;
}
int main() {
size_t n;
House* arr;
arr = scan("Data.txt", n);
std::cout << "Initial Array: " << print(arr, n) << '\n';
std::sort(arr, arr + n, Comparator());
std::cout << "Sorted Array: " << print(arr, n) << '\n';
delete[] arr;
return 0;
}
Data.txt:
4
-3 5 10 Ivanov
0 10 5 Sidorov
231 6 6 Petrov
8 10 10 Ivanov
Вывод:
Initial Array: [House(id: -3; area: 5; floors: 10; owner: Ivanov), House(id: 0; area: 10; floors: 5; owner: Sidorov), House(id: 231; area: 6; floors: 6; owner: Petrov), House(id: 8; area: 10; floors: 10; owner: Ivanov)]
Sorted Array: [House(id: -3; area: 5; floors: 10; owner: Ivanov), House(id: 8; area: 10; floors: 10; owner: Ivanov), House(id: 231; area: 6; floors: 6; owner: Petrov), House(id: 0; area: 10; floors: 5; owner: Sidorov)]