Nie wiem jak zrobić skopiowanie tablicy struktur, żeby potem użyć jej do sortowania danych. Chodzi mi o to, żeby po posortowaniu danych mieć jeszcze oryginalną tablicę struktur. Mój kod (książka telefoniczna).
#include<fstream>
#include <conio.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <Windows.h>
#include <algorithm>
using namespace std;
struct ksiazka
{
int lp;
string imie;
string nazwisko;
string numer;
};
void wczytaj_z_pliku(vector<ksiazka>& tab)
{
string linia;
fstream file;
file.open("Książka_Telefoniczna.txt", ios::in);
if (file.good() == true)
{
while (!file.eof())
{
getline(file, linia);
cout << linia << endl;
}
file.close();
}
}
void wyswietlanie_struktry(vector<ksiazka>& tab)
{
char o;
for (int i = 0; i < tab.size(); i++)
{
cout << tab[i].lp << " ";
cout << tab[i].imie << " ";
cout << tab[i].nazwisko << " ";
cout << tab[i].numer << "\n";
}
cout << "\n";
cout << "Wróć do menu - Dowolny klawisz" << "\n";
o = _getch();
system("cls");
}
//=================================================================================================
ksiazka str_struct(string txt) // Z pliku txt na strukturę
{
ksiazka element;
string tmp;
int end = 0, front = 0;
while (txt[front] != ' ')
front++;
tmp = txt.substr(end, front - end);
element.lp = atoi(tmp.c_str());
front++;
end = front;
while (txt[front] != ' ')
front++;
element.imie = txt.substr(end, front - end);
front++;
end = front;
while (txt[front] != ' ')
front++;
element.nazwisko = txt.substr(end, front - end);
front++;
end = front;
while (txt[front] != ' ')
front++;
element.numer = txt.substr(end, front - end);
front++;
end = front;
return element;
}
void wczytanie(vector <ksiazka>&tab) // Wpisanie wczytanej struktury funckją ksiazka str_struct(string s)
{
string linia;
fstream plik;
plik.open("Książka_Telefoniczna.txt", ios::in);
if (plik.good() == 0) cout << "Nie mozna otworzyc pliku!" << "\n";
else
{
while (getline(plik, linia))
{
tab.push_back(str_struct(linia));
}
plik.close();
}
system("cls");
}
//=================================================================================================
void sortuj(vector <ksiazka>tab)
{
int o;
cout << "Jak posortować?" << "\n";
cout << "1. Po nazwisku" << "\n";
cout << "2. Po imieniu" << "\n";
cout << "3. Numery telefonu rosnąco" << "\n";
cout << "4. Powrót do menu" << "\n";
o = _getch();
switch (o)
{
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
}
}
int main()
{
int lp = 0;
string imie, nazwisko, telefon;
vector <ksiazka>tab;
vector <ksiazka>tab2;
setlocale(LC_ALL, "pl_PL");
int i = 0;
for (;;)
{
int O;
cout << "Książka telefoniczna" << "\n";
cout << "Menu główne:" << "\n";
cout << "1. Dodaj nowego abonenta" << "\n";
cout << "2. Wczytaj numery z pliku" << "\n";
cout << "3. Wyświetl abonentów" << "\n";
cout << "4. Ustaw limit abonentów (domyślnie 50)" << "\n";
cout << "5. Sortuj" << "\n";
cout << "6. Wyjdź z programu" << "\n\n";
O = _getch();
switch (O)
{
case '1':
system("cls");
for (int n = 0; n < 1; n++)
{
i = tab.size();
lp++;
cout << "Imię: \t";
cin >> imie;
cout << "Nazwisko: \t";
cin >> nazwisko;
cout << "Numer: \t";
cin >> telefon;
ksiazka pozycja = { lp, imie, nazwisko, telefon };
tab.push_back(pozycja);
fstream file;
file.open("Książka_Telefoniczna.txt", ios::out | ios::app);
file << tab[i].lp << " ";
file << tab[i].imie << " ";
file << tab[i].nazwisko << " ";
file << tab[i].numer << " " << endl;
file.close();
i++;
cout << "Zapisano pomyślnie!" << "\n\n\n";
Sleep(1000);
}; break;
case '2':
wczytanie(tab);
lp = tab.size();
cout << "Wczytano!" << "\n";
Sleep(1000);
break;
case '3':
wyswietlanie_struktry(tab);
break;
case '4':
break;
case'5':
sortuj(tab);
break;
case '6':
exit(0);
default:
cout << "Niepoprawna opcja" << "\n";
Sleep(1000);
system("cls");
}
}
system("PAUSE");
}