Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Can anybody help me to understand below code ?
Forum Updated to NodeBB v4.3 + New Features

Can anybody help me to understand below code ?

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 661 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    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 ?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Q Qt embedded developer

      @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 ?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #5

      @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 after pos. substr(beg, pos - beg) takes the substring starting at beg and ending at pos - 1. So, just as you would expect, it extracts the next "token" between those delimiter characters. And then because you have it in a while loop it moves on to get the next one, restarting at pos, where it got to last time.

      1 Reply Last reply
      2
      • Q Qt embedded developer

        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 ?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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!

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Q 1 Reply Last reply
        2
        • Q Qt embedded developer

          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 ?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #3

          @Qt-embedded-developer
          A glance tells you that it splits the string on delims= ", $", so I would guess on any of comma, space or dollar. Then all you have to do is look at the std::string documentation for find_first_not_of/find_first_of/substr() methods to see what they do (pretty obvious). What is the mystery?

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @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!

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #4

            @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 ?

            JonBJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @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 ?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #5

              @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 after pos. substr(beg, pos - beg) takes the substring starting at beg and ending at pos - 1. So, just as you would expect, it extracts the next "token" between those delimiter characters. And then because you have it in a while loop it moves on to get the next one, restarting at pos, where it got to last time.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved