所以我有一個“db.csv”資料檔案。代碼從以逗號分隔的檔案中讀取每個值 (value1, ...)。洗掉行中不必要的間距,然后根據需要列印完成的行(值之間有一個空格)。問題是我必須通過名為 repl.it 的站點上的測驗。我每次測驗都失敗了,因為輸出末尾有一個額外的空行。有什么建議?我知道,這可能是因為 cout 中的 endl,但我還能怎么做?
最小的可重現代碼:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
int main() {
fstream file("db.csv", ios::in);
string value1, value2, value3, value4, value5, line;
cout << "result:" << endl;
while (getline(file, line)) {
if (line.length() > 1) {
istringstream str1(line);
while (getline(str1, value1, ','),
getline(str1, value2, ','),
getline(str1, value3, ','),
getline(str1, value4, ','),
getline(str1, value5, '\n')) {
value1.erase(remove(value1.begin(), value1.end(), ' '), value1.end());
value2.erase(remove(value2.begin(), value2.end(), ' '), value2.end());
value3.erase(remove(value3.begin(), value3.end(), ' '), value3.end());
value4.erase(remove(value4.begin(), value4.end(), ' '), value4.end());
value5.erase(remove(value5.begin(), value5.end(), ' '), value5.end());
cout << value1 << " " << value2 << " "
<< value3 << " " << value4 << " "
<< value5
<< endl;
}
}
}
}
db.csv 檔案資料:
Riga,Kraslava,Pr,15:00,11.00
Riga ,Kraslava,Pr ,18:00,11.00
Kraslava,Riga,Pr,08:00,11.00
Kraslava,Daugavpils,Ot ,10:00, 3.00
Ventsplis,8.00,Liepaja,Sv,20:00
Dagda,Sv
Rezekne,Riga,Tr,13:00,10.50
Dagda,Kraslava, Ce,18:00, 2.50
Dagda,Kraslava,Ce,18:00,2.50,Sv
Riga,Ventspils, Pt,09:00 , 6.70
Liepaja,Ventspils,Pt,17:00,5.50
輸出:
Riga Kraslava Pr 15:00 11.00
Riga Kraslava Pr 18:00 11.00
Kraslava Riga Pr 08:00 11.00
Kraslava Daugavpils Ot 10:00 3.00
Ventsplis 8.00 Liepaja Sv 20:00
Rezekne Riga Tr 13:00 10.50
Dagda Kraslava Ce 18:00 2.50
Dagda Kraslava Ce 18:00 2.50,Sv
Riga Ventspils Pt 09:00 6.70
Liepaja Ventspils Pt 17:00 5.50
*blank line*
期望的輸出:
Riga Kraslava Pr 15:00 11.00
Riga Kraslava Pr 18:00 11.00
Kraslava Riga Pr 08:00 11.00
Kraslava Daugavpils Ot 10:00 3.00
Ventsplis 8.00 Liepaja Sv 20:00
Rezekne Riga Tr 13:00 10.50
Dagda Kraslava Ce 18:00 2.50
Dagda Kraslava Ce 18:00 2.50,Sv
Riga Ventspils Pt 09:00 6.70
Liepaja Ventspils Pt 17:00 5.50
uj5u.com熱心網友回復:
而不是檢測的最后一次迭代,并避免把一個換行符,如果你在回圈的開始添加換行,并避免在輸出開始外來換行,保持布爾變數可能更容易is_first最初設定true,并在你的回圈體的開始有
if (!is_first)
cout << endl;
is_first = false;
不漂亮,但編碼很快。
uj5u.com熱心網友回復:
我認為這種方法更簡潔:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
std::string filedata = R"(
Riga,Kraslava,Pr,15:00,11.00
Riga ,Kraslava,Pr ,18:00,11.00
Kraslava,Riga,Pr,08:00,11.00
Kraslava,Daugavpils,Ot ,10:00, 3.00
Ventsplis,8.00,Liepaja,Sv,20:00
Dagda,Sv
Rezekne,Riga,Tr,13:00,10.50
Dagda,Kraslava, Ce,18:00, 2.50
Dagda,Kraslava,Ce,18:00,2.50,Sv
Riga,Ventspils, Pt,09:00 , 6.70
Liepaja,Ventspils,Pt,17:00,5.50
)";
std::string trim( const std::string& str ) {
std::size_t pos = str.find_first_not_of( " \t" );
if ( pos == std::string::npos ) return {};
std::size_t last = str.find_last_not_of( " \t" );
if ( last == std::string::npos ) return {};
return str.substr( pos, last - pos 1 );
}
std::vector<std::string> split( const std::string& line ) {
std::size_t pos = 0;
std::size_t next = 0;
std::vector<std::string> fields;
while ( next != std::string::npos ) {
next = line.find_first_of( ',', pos );
std::string field = next == std::string::npos ? line.substr(pos) : line.substr(pos,next-pos);
fields.push_back( trim( field ) );
pos = next 1;
}
return fields;
}
int main()
{
istringstream file(filedata);
cout << "result:" << endl;
std::string line;
while (getline(file, line)) {
if (!line.empty()) {
auto fields = split( line );
for ( std::string field : fields ) {
cout << field << ",";
}
cout << endl;
}
}
}
結果是
result:
Riga,Kraslava,Pr,15:00,11.00,
Riga,Kraslava,Pr,18:00,11.00,
Kraslava,Riga,Pr,08:00,11.00,
Kraslava,Daugavpils,Ot,10:00,3.00,
Ventsplis,8.00,Liepaja,Sv,20:00,
Dagda,Sv,
Rezekne,Riga,Tr,13:00,10.50,
Dagda,Kraslava,Ce,18:00,2.50,
Dagda,Kraslava,Ce,18:00,2.50,Sv,
Riga,Ventspils,Pt,09:00,6.70,
Liepaja,Ventspils,Pt,17:00,5.50,
編譯器瀏覽器:https : //godbolt.org/z/enheGdjda
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373612.html
標籤:C
上一篇:可以“使用命名空間標準;”和“std::cout”一起使用?
下一篇:Mysqljson二進制編碼
