I have a statement like this, and I want to parse by space, colon and comma, mostly for the select part, and the orderby part: Can someone tell me an easy way to do this? SELECT LNAME:1,FNAME:1,SALARY:1 FROM EMPLOYEE WHERE SALARY >= 30000 ORDERBY SALARY:1;
I am not sure what to try for this, and whatever I did is not working. I tried separating the string one by one by the delimiter, but that did not work.
while((pos = col_list.find(":")) != string::npos){
开发者_StackOverflow社区 token = col_list.substr(0, pos);
if(col_list[pos + 1] == '1'){
cols.push_back(token);
}
col_list.erase(0, pos + 1);
//check last column
if(col_list[0] == '1'){
cols.push_back(col_list);
}
That is what I tried, but it is not working as I wanted, and it is not parsing properly, and I realized I need multiple delimiters.
There is no built-in std::string
method that searches for multiple delimiters.
It's up to you to implement the logic for this, correctly. This is not an uncommon task; there a few different approaches that are commonly used for something like this. Which one is used is mostly up to personal preferences of whoever is writing the code. Such as:
It's common to implement something withstd::find_if
:
auto b=col_list.begin(), e=col_list.end(),
p=std::find_if(b, e, [](char c) { return c == ' ' || c == ':' c == ',' });
// [b, p) is the sequence up to the space, colon, or comma, that can be
// used to construct a discrete std::string. p ends up being either the
// ending iterator, or the iterator to the space, colon or comma.
This is a basic building block for parsing out individual tokens that have multiple delimiters.
Alternatively, one can just iterate character by character, collecting each character into a buffer, and then processing the collected buffer when a delimiter is seen:
std::string token;
for (auto c:col_list)
{
if (c == ' ' || c == ',' || c == ':')
{
// process the collected token
token.clear();
}
else
token.push_back(c);
}
// process the last token
Different, logic-based variations, can also be used. In all case one will need to decide what's the right thing to do with consecutive delimiters, and what to do in that case, you'll have to figure out what your logic must do in that case.
The bottom line is that some logic and algorithm always needs to be employed to do any kind of a non-trivial task in C++. You described a non-trivial task in C++.
精彩评论