string_view
https://yangacer.blogspot.com/2016/07/c17-stringview.html
#include
using namespace std;
struct tokenizer {
tokenizer(string const &s) : m_s(s), m_beg(0), m_end(0) {}
string operator()() {
for(;m_end < m_s.size();++m_end) {
if (m_s[m_end] == ',') {
auto res = m_s.substr(m_beg, m_end - m_beg); // heap access
m_beg = ++m_end;
return res; // heap access
}
}
if (m_end <= m_s.size())
return m_s.substr(m_beg, m_end); // heap access
}
bool more() const { return m_end < m_s.size(); }
private:
string const &m_s;
size_t m_beg, m_end;
};
void gendata(string &data, size_t count) {
for(size_t i=0; i < count; ++i) {
data.push_back('a'+ i%26);
if (i+1 != count)
data.push_back(',');
}
}
int main() {
string data;
gendata(data, 1<<23);
tokenizer tokr(data);
while(tokr.more()) {
cout << tokr() << endl;
}
return 0;
}
沒有留言:
張貼留言