Can anybody help me to understand below code ?
-
Today i have read one code which extract the latest db version from string "1.5,10,1.3$1.5,11,1.4".
for that i had implemented below code :#include <iostream> #include <vector> #define DBGF_TRACE printf using namespace std; int main() { cout<<"Hello World"; string strOTAZip; string strSettingkeyvalue= "1.5,10,1.3$1.5,11,1.4"; if(strSettingkeyvalue !="" && strSettingkeyvalue !="0" && strSettingkeyvalue.size()>4) { // 1. concatenate the zip version with OTA link std::vector<string> vecGetSplittedZipVersion; std::string delims= ", $" ; size_t beg, pos = 0; while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip"; } std::cout <<strOTAZip; return 0; }
I am getting expected ouput as "/Config11.zip".
But my mind can not understand the part of code mentioned below :
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip";
So please help me to understand above part of code. how it iterate and find the my latest db version from string ?
-
@jsulm I have formatted the code. the unclear part is
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg));
i want to understand what exactly this 3 statement is doing in my example i want to understand.
i have seen find_first_not_of() and find_first_of() first time so what exactly this function is doing in my example code ?
@Qt-embedded-developer said in Can anybody help me to understand below code ?:
i want to understand
Then you need to read the docs and analyze the code, I don't know what "explanation" you are expecting!
beg
is set to the first non-", $"
character.pos
is then set to the next", $"
character, starting from just afterpos
.substr(beg, pos - beg)
takes the substring starting atbeg
and ending atpos - 1
. So, just as you would expect, it extracts the next "token" between those delimiter characters. And then because you have it in awhile
loop it moves on to get the next one, restarting atpos
, where it got to last time. -
Today i have read one code which extract the latest db version from string "1.5,10,1.3$1.5,11,1.4".
for that i had implemented below code :#include <iostream> #include <vector> #define DBGF_TRACE printf using namespace std; int main() { cout<<"Hello World"; string strOTAZip; string strSettingkeyvalue= "1.5,10,1.3$1.5,11,1.4"; if(strSettingkeyvalue !="" && strSettingkeyvalue !="0" && strSettingkeyvalue.size()>4) { // 1. concatenate the zip version with OTA link std::vector<string> vecGetSplittedZipVersion; std::string delims= ", $" ; size_t beg, pos = 0; while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip"; } std::cout <<strOTAZip; return 0; }
I am getting expected ouput as "/Config11.zip".
But my mind can not understand the part of code mentioned below :
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip";
So please help me to understand above part of code. how it iterate and find the my latest db version from string ?
@Qt-embedded-developer said in Can anybody help me to understand below code ?:
But my mind can not understand the part of code mentioned below
What exactly is not clear (which exact part)?
Also please format the code properly! -
Today i have read one code which extract the latest db version from string "1.5,10,1.3$1.5,11,1.4".
for that i had implemented below code :#include <iostream> #include <vector> #define DBGF_TRACE printf using namespace std; int main() { cout<<"Hello World"; string strOTAZip; string strSettingkeyvalue= "1.5,10,1.3$1.5,11,1.4"; if(strSettingkeyvalue !="" && strSettingkeyvalue !="0" && strSettingkeyvalue.size()>4) { // 1. concatenate the zip version with OTA link std::vector<string> vecGetSplittedZipVersion; std::string delims= ", $" ; size_t beg, pos = 0; while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip"; } std::cout <<strOTAZip; return 0; }
I am getting expected ouput as "/Config11.zip".
But my mind can not understand the part of code mentioned below :
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg)); std::cout << strSettingkeyvalue.substr(beg, pos - beg) << std::endl; } string ZipVerName = vecGetSplittedZipVersion[vecGetSplittedZipVersion.size()-2]; strOTAZip ="/Config"+ZipVerName+".zip";
So please help me to understand above part of code. how it iterate and find the my latest db version from string ?
@Qt-embedded-developer
A glance tells you that it splits the string ondelims= ", $"
, so I would guess on any of comma, space or dollar. Then all you have to do is look at thestd::string
documentation forfind_first_not_of
/find_first_of
/substr()
methods to see what they do (pretty obvious). What is the mystery? -
@Qt-embedded-developer said in Can anybody help me to understand below code ?:
But my mind can not understand the part of code mentioned below
What exactly is not clear (which exact part)?
Also please format the code properly!@jsulm I have formatted the code. the unclear part is
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg));
i want to understand what exactly this 3 statement is doing in my example i want to understand.
i have seen find_first_not_of() and find_first_of() first time so what exactly this function is doing in my example code ?
-
@jsulm I have formatted the code. the unclear part is
while ((beg = strSettingkeyvalue.find_first_not_of(delims, pos)) != std::string::npos) { pos = strSettingkeyvalue.find_first_of(delims, beg + 1); vecGetSplittedZipVersion.push_back(strSettingkeyvalue.substr(beg, pos - beg));
i want to understand what exactly this 3 statement is doing in my example i want to understand.
i have seen find_first_not_of() and find_first_of() first time so what exactly this function is doing in my example code ?
@Qt-embedded-developer said in Can anybody help me to understand below code ?:
i want to understand
Then you need to read the docs and analyze the code, I don't know what "explanation" you are expecting!
beg
is set to the first non-", $"
character.pos
is then set to the next", $"
character, starting from just afterpos
.substr(beg, pos - beg)
takes the substring starting atbeg
and ending atpos - 1
. So, just as you would expect, it extracts the next "token" between those delimiter characters. And then because you have it in awhile
loop it moves on to get the next one, restarting atpos
, where it got to last time.