免責宣告:這些都是假地址,僅供學習之用。
我希望我的串列根據 [i][0] 的第一列進行排序,并且使行的其余部分 ([i][j]) 跟隨新的排序位置列。現在我的代碼似乎只是對第一列進行排序,而不是使整個行跟隨它到第一列給出的新位置。我嘗試了很多方法,但似乎還沒有找到解決方案。
請幫我!
/*
konst.txt includes following:
Stengren Lena Bokstavsgatan 10 27890 Stadk?ping
Osterblad Johan Gr?nskog 12A 10908 Ljush?jda
Broholme Reny Havstundav 8 36799 H?k?nget
Roholm Karol Stugsten 7 45892 R?gskog
Lindagren Erika Hjufjord 139 87834 Skogholma
*/
string adresser[50][6];
string input_file = "D:\\konst.txt";
ifstream input_stream;
int lastpos = 0;
string temp;
input_stream.open(input_file);
for (int i = 0; i < 20; i ) {
for (int j = 0; j < 6; j ) {
input_stream >> adresser[i][j]; //Saves the columns and rows in the 2d array
cout << adresser[i][j] << ' '; //Writes out the whole list
}
cout << endl;
}
for (int i = 0; i < 50; i ) { //Finds the last position for collumns
if (adresser[i][0] == "") {
lastpos = i;
break;
}
}
cout << "\n\n\n\n";
for (int i = lastpos - 1; i > 0; i--) {
for (int j = 0; j < i; j ) {
if (adresser[j][0] > adresser[j 1][0]) { //Sorts the array for ONLY the first collumns.
temp = adresser[j][0];
adresser[j][0] = adresser[j 1][0];
adresser[j][0] = temp;
}
}
}
提醒一下 2d 陣列包含的內容:
/*
Stengren Lena Bokstavsgatan 10 27890 Stadk?ping
Osterblad Johan Gr?nskog 12A 10908 Ljush?jda
Broholme Reny Havstundav 8 36799 H?k?nget
Roholm Karol Stugsten 7 45892 R?gskog
Lindagren Erika Hjufjord 139 87834 Skogholma
*/
代碼的作用:
/*
Broholme Lena Bokstavsgatan 10 27890 Stadk?ping
Lindagren Johan Gr?nskog 12A 10908 Ljush?jda
Osterblad Reny Havstundav 8 36799 H?k?nget
Roholm Karol Stugsten 7 45892 R?gskog
Stengren Erika Hjufjord 139 87834 Skogholma
*/
我想要它做什么:
/*
Broholme Reny Havstundav 8 36799 H?k?nget
Lindagren Erika Hjufjord 139 87834 Skogholma
Osterblad Johan Gr?nskog 12A 10908 Ljush?jda
Roholm Karol Stugsten 7 45892 R?gskog
Stengren Lena Bokstavsgatan 10 27890 Stadk?ping
*/
uj5u.com熱心網友回復:
陣列不是做你想做的事情的好資料型別。從現實生活中提取“單詞”并在您的代碼中使用它們是很好的。您正在做的是對聯系人進行排序,因此首先制作代表聯系人的東西,例如結構。從那里構建您的代碼(一次一個函式,函式也非常適合命名事物),然后它將變得更具可讀性和更容易理解。例如像這樣:
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <regex>
struct contact_info_t
{
std::string first_name;
std::string last_name;
std::string address;
std::string postal_code;
std::string city;
};
// removed the "special" characters for now, seems code has some trouble with that
std::istringstream file_content
{
"Stengren Lena Bokstavsgatan 10 27890 Stadkoping\n"
"Osterblad Johan Gronskog 12A 10908 Ljushojda\n"
"Broholme Reny Havstundav 8 36799 Hokanget\n"
"Roholm Karol Stugsten 7 45892 Ragskog\n"
"Lindagren Erika Hjufjord 139 87834 Skogholma\n"
};
// load uses a regex to split up string, I think that's what causes the issues with special characters.
// but the bottom line is read your file and build a vector of structs from it. Not an array
auto load(std::istream& file)
{
static const std::regex rx{ "(\\w )\\s (\\w )\\s (\\w \\s\\w )\\s (\\w )\\s (.*)" };
std::smatch match;
std::vector<contact_info_t> contacts;
std::string line;
while (std::getline(file,line))
{
if (std::regex_search(line,match,rx))
{
contact_info_t info;
info.last_name = match[1];
info.first_name = match[2];
info.address = match[3];
info.postal_code = match[4];
info.city = match[5];
contacts.push_back(info);
}
}
return contacts;
}
// Since you can sort by multiple predicates (functions that return a bool)
// I made a generic sort function. It sorts pointers to the structs
// so that no data is move where it is not necessary
auto sort(const std::vector<contact_info_t>& contacts, std::function<bool(const contact_info_t* lhs, const contact_info_t* rhs)> predicate)
{
std::vector<const contact_info_t*> sorted_contacts;
for (const auto& contact : contacts) sorted_contacts.push_back(&contact);
std::sort(sorted_contacts.begin(), sorted_contacts.end(), predicate);
return sorted_contacts;
}
// show the sorted contacts, one struct at a time.
// that way it is impossible for columns to get mixed up
void show_sorted_contacts(const std::vector<const contact_info_t*>& sorted_contacts)
{
for (const auto& contact : sorted_contacts)
{
std::cout << contact->last_name << " ";
std::cout << contact->first_name << " ";
std::cout << contact->address << " ";
std::cout << contact->city << " ";
std::cout << contact->postal_code << "\n";
}
}
int main()
{
auto contacts = load(file_content);
auto contacts_sorted_by_last_name = sort(contacts, [](const contact_info_t* lhs, const contact_info_t* rhs)
{
return (lhs->last_name < rhs->last_name);
});
show_sorted_contacts(contacts_sorted_by_last_name);
}
uj5u.com熱心網友回復:
如果你問 10 位 C 開發人員,你可能會得到 11 種不同的答案,這些答案具有不同的復雜性,并且使用了更多或更少的語言特性。這是我的看法,距離 OP 的代碼僅幾步之遙。
如果您想將所有資訊保持在一行中,那么這就是classes 或structs 的用途,并且確實是 C 與 C 不同的很大一部分。您可以滾動自己的型別(在這種情況下是 Record 結構) 將所有姓名/地址資訊組合到一個專案中。無需任何額外代碼即可復制僅具有簡單成員變數的結構。
還有幾件事要做。OP 正在使用coutand ifstream,它們都可以處理strings 和數字。他們需要一點幫助來處理我們的新 Record 型別,這就是兩個流運算子多載為我們所做的。最后,如果我們要排序,我們需要能夠比較兩個記錄:在這種情況下,按姓氏的字母順序排列,因此我們多載了 '>' 運算子。
現在,我們有了一維記錄陣列,而不是二維字串陣列。
#include <fstream>
#include <iostream>
#include <string>
//Create a structure to hold all the items for a name/address
struct Record
{
std::string familyName;
std::string firstName;
std::string streetName;
std::string streetNumber;
long postCode{ 0 };
std::string townName;
};
//Overloads for printing out a record ...
std::ostream& operator<<(std::ostream& os, const Record& r)
{
os << r.familyName << " " << r.firstName << " " << r.streetName << " "
<< r.streetNumber << " " << r.postCode << " " << r.townName;
return os;
}
//... and reading in a record
std::istream& operator>>(std::istream& is, Record& r)
{
is >> r.familyName >> r.firstName >> r.streetName >> r.streetNumber >> r.postCode >> r.townName;
return is;
}
//How to compare two records
bool operator>(const Record& lhs, const Record& rhs)
{
return lhs.familyName > rhs.familyName;
}
int main()
{
using namespace std;
//Create any fixed-length array of Records
Record records[20];
//Load the name/address info
//and keep a count
ifstream ifs;
ifs.open("konst.txt", ifstream::in);
auto nRecords = 0;
cout << "Input:\n";
while(ifs.good() && nRecords<20)
{
Record& r = records[nRecords];
ifs >> r;
cout << r << "\n";
nRecords ;
}
//Perform the bubble sort
for (int i = nRecords - 1; i > 0; i--)
{
for (int j = 0; j < i; j )
{
if( records[j] > records[j 1])
{
//Swap records
Record tmp = records[j];
records[j] = records[j 1];
records[j 1] = tmp;
}
}
}
//Print out result
cout << "\nSorted:\n";
for (int i = 0; i < nRecords; i )
{
cout << records[i] << "\n";
}
ifs.close();
}
這是結果(我的默認語言環境不解碼 ? 或 ?):
Input:
Stengren Lena Bokstavsgatan 10 27890 Stadk├╢ping
Osterblad Johan Gr├╢nskog 12A 10908 Ljush├╢jda
Broholme Reny Havstundav 8 36799 H├╢k├?nget
Roholm Karol Stugsten 7 45892 R├?gskog
Lindagren Erika Hjufjord 139 87834 Skogholma
Sorted:
Broholme Reny Havstundav 8 36799 H├╢k├?nget
Lindagren Erika Hjufjord 139 87834 Skogholma
Osterblad Johan Gr├╢nskog 12A 10908 Ljush├╢jda
Roholm Karol Stugsten 7 45892 R├?gskog
Stengren Lena Bokstavsgatan 10 27890 Stadk├╢ping
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361253.html
下一篇:從C中的陣列中洗掉元素
