迭代时如何保留字符串?
这是我使用的 C++ 方法。
我注意到您只有 3 种字段类型:string、null 和 int。
以下方法使用这些字段类型(在方法“void init()”中),按照每行显示字段的顺序,有时使用 string::find() (而不是 getline() )来定位字段结尾.
这 3 种方法中的每一种都使用擦除字符串中的字符。我知道擦除速度很慢,但为了方便起见,我做出了这个选择。 (擦除更容易测试,只需在每次提取后添加一个 cout )。可以通过适当处理(在需要时)搜索开始索引来删除/替换擦除。
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>
class CSV_t
{
typedef std::vector<int> IntVec_t;
// private nested class -- holds contents of 1 csv record
class CSVRec_t
{
public:
std::string primary;
std::string secondary;
std::string nullary;
std::string thirdary;
IntVec_t i5;
std::string show()
{
std::stringstream ss;
ss << std::setw(25) << primary
<< " " << std::setw(10) << secondary
<< " " << std::setw(12)<< thirdary << " ";
for (size_t i=0;
i<i5.size(); ++i) ss << std::setw(5) << i5[i];
ss << std::endl;
return (ss.str());
}
}; // class CSVRec_t
typedef std::vector<CSVRec_t> CSVRecVec_t;
CSVRecVec_t csvRecVec; // holds all csv record
public:
CSV_t() { };
void init(std::istream& ss)
{
do // read all rows of file
{
CSVRec_t csvRec;
std::string s;
(void)std::getline(ss, s);
if(0 == s.size()) break;
assert(s.size()); extractQuotedField(s, csvRec.primary); // 1st quoted substring
assert(s.size()); extractQuotedField(s, csvRec.secondary); // 2nd quoted substring
assert(s.size()); confirmEmptyField(s, csvRec.nullary); // null field
assert(s.size()); extractQuotedField(s, csvRec.thirdary); // 3rd quoted substring
assert(s.size()); extract5ints(s, csvRec.i5); // handle 5 int fields
csvRecVec.push_back(csvRec); // capture
if(ss.eof()) break;
}while(1);
}
void show()
{
std::cout << std::endl;
for (size_t i = 0; i < csvRecVec.size(); ++i)
std::cout << std::setw(5) << i+1 << " " << csvRecVec[i].show();
std::cout << std::endl;
}
private:
void extractQuotedField(std::string& s, std::string& s2)
{
size_t indx1 = s.find('"', 0);
assert(indx1 != std::string::npos);
size_t indx2 = s.find('"', indx1+1);
assert(indx2 != std::string::npos);
size_t rng1 = indx2 - indx1 + 1;
s2 = s.substr(indx1, rng1);
s.erase(indx1, rng1+1);
}
void confirmEmptyField(std::string& s, std::string nullary)
{
size_t indx1 = s.find('"');
nullary = s.substr(0, indx1);
// tbd - confirm only spaces and comma's in this substr()
s.erase(0, indx1);
}
void extract5ints(std::string& s, IntVec_t& i5)
{
std::stringstream ss(s);
int t = 0;
for (int i=0; i<5; ++i)
{
ss >> t;
ss.ignore(1); // skip ','
assert(!ss.bad()); // confirm ok
i5.push_back(t);
}
s.erase(0, std::string::npos);
}
}; // class CSV_t
int t288(void) // test 288
{
std::stringstream ss;
ss << "\"Primary, Secondary, Third\", \"Primary\", , \"Secondary\", 18, 4, 0, 0, 0\n"
<< "\"Pramiry, Secandory, Thrid\", \"Pramiry\", , \"Secandory\", 19, 5, 1, 1, 1\n"
<< "\"Pri-mary, Sec-ondary, Trd\", \"Pri-mary\", , \"Sec-ondary\", 20, 6, 2, 3, 4\n"
<< std::endl;
CSV_t csv;
csv.init(ss);
csv.show(); // results
return (0);
}