[英]C++ Alphabetically Sorting Strings in Vectors of Vectors
nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
kb93,Kyle,Borris,Aetna,2
我試圖像上面那樣獲取一個CSV輸入文件,對其進行存儲,按保險分類(第4列),然后根據保險將其寫入diff文件,但按姓氏的字母順序排列。
因此,在此程序中,我有一個uniqueInsurances向量,而后者又有一個注冊者向量。 我想以此登記人的名字按姓氏(第3行)按字母順序排序,因此,如果uniqueInsurances [0] .name是Aetna,則uniqueInsurances [0] .enrollees []的Kyle Borris將列在Nate Matthews之前。 現在,我將其存儲為與Kyle Borris之前列出的Nate Matthews相反。
我認為這是由於向量的矢量以及此問題所需的嵌套嵌套循環使我感到困惑,所以我想知道是否有人可以幫助指導我針對每個uniqueInsurance的最佳參與者矢量排序方式?
struct enrollee
{
string userid = "";
string fname = "";
string lname = "";
string insurance = "";
string version = "";
};
struct uniqueInsurance
{
string name = "";
int numEnrollees = 0;
vector <enrollee> enrollVector;
};
如果您的任務只是寫不同名稱的文件,那么您就不需要第二種結構。 只需讓一個std::vector<enrollee>
根據保險和名稱對其進行排序,然后對其進行迭代。 保險名稱更改后,請相應地重新打開文件:
std::vector<enrollee> enrollees;
// read them from csv file
std::sort( enrollees.begin(), enrollees.end(), []( const enrollee &e1, const enrollee &e2 ) {
return std::tie( e1.insurance, e1.fname, e1.lname ) < std::tie( e2.insurance, e2.fname, e2.lname );
} );
std::string insurance;
std::ofstream out;
for( const auto &e : enrollees ) {
if( insurance != e.insurance ) {
insurance = e.insurance;
out.open( insurance + ".csv" );
}
out << e.fname << ',' << e.lname << std::endl;
}
這按名字排序,然后按姓氏排序,如果您首先需要姓氏,只需在std::tie()
交換順序即可
這是昨天您問題的跟進:
我修改了代碼,並添加了一行用於排序。
請參見:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <regex>
struct Enrollee
{
// Data
std::string userid{};
std::string fname{};
std::string lname{};
std::string insurance{};
std::string version{};
// Overload Extractor Operator to read data from somewhere
friend std::istream& operator >> (std::istream &is, Enrollee& e) {
std::vector<std::string> wordsInLine{}; // Here we will store all words that we read in onle line;
std::string wholeLine; // Temporary storage for the complete line that we will get by getline
std::regex separator("[ \\;\\,]"); ; // Separator for a CSV file
std::getline(is, wholeLine); // Read one complete line and split it into parts
std::copy(std::sregex_token_iterator(wholeLine.begin(), wholeLine.end(), separator, -1), std::sregex_token_iterator(), std::back_inserter(wordsInLine));
// If we have read all expted strings, then store them in our struct
if (wordsInLine.size() == 5) {
e.userid = wordsInLine[0];
e.fname = wordsInLine[1];
e.lname = wordsInLine[2];
e.insurance = wordsInLine[3];
e.version = wordsInLine[4];
}
return is;
}
// Overload Inserter operator. Insert data into output stream
friend std::ostream& operator << (std::ostream& os, const Enrollee& e) {
return os << "userid is: " << e.userid << "\nfname is: " << e.fname << "\nlname is: " << e.lname << "\ninsurance is: " << e.insurance << "\nversion is: " << e.version << '\n';
}
};
int main()
{
// Her we will store all Enrollee data in a dynamic growing vector
std::vector<Enrollee> enrollmentData{};
// Define inputFileStream and open the csv
std::ifstream inputFileStream("r:\\input.csv");
// If we could open the file
if (inputFileStream)
{
// Then read all csv data
std::copy(std::istream_iterator<Enrollee>(inputFileStream), std::istream_iterator<Enrollee>(), std::back_inserter(enrollmentData));
// Sort the data
std:sort(enrollmentData.begin(),enrollmentData.end(),[](const Enrollee& left, const Enrollee& right){return left.lname < right.lname;});
// For Debug Purposes: Print all data to cout
std::copy(enrollmentData.begin(), enrollmentData.end(), std::ostream_iterator<Enrollee>(std::cout, "\n"));
}
else {
std::cerr << "Could not open file 'input.csv'\n";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.