0

Can someone explain/confirm me meaning of below lines?

  1. bool instring{false}; - this mean that false is the initial value of this variable, yes?
  2. for (const char* p = mystart; *p; p++) - here pointer *p at second parameter of for means that this loop exist to the moment that this pointer exist, yes?
  3. string(mystart,p-mystart) - I can't find this string usage in c++ reference, I know result of it is difference between this parameters, but don't understand how this happen.

This lines are from code below(original code from another SO question):

string line; 
while (std::getline(cin, line)) {        // read full line
    const char *mystart=line.c_str();    // prepare to parse the line - start is position of begin of field
    bool instring{false};                
    for (const char* p=mystart; *p; p++) {  // iterate through the string
        if (*p=='"')                        // toggle flag if we're btw double quote
            instring = !instring;     
        else if (*p==',' && !instring) {    // if comma OUTSIDE double quote
            csvColumn.push_back(string(mystart,p-mystart));  // keep the field
            mystart=p+1;                    // and start parsing next one
        }
    }
csvColumn.push_back(string(mystart));   // last field delimited by end of line instead of comma
}
BlueMark
  • 962
  • 4
  • 23
  • 41

4 Answers4

1
  1. Correct. This is a relatively new unified initializer syntax
  2. The loop exits the moment that pointer p points to a zero value
  3. The string is using the two-iterator constructor, which can take two pointers as well. The new string contains everything starting at the first pointer, inclusive, up to the second (computed) pointer, excusive.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1
  1. bool instring{false}; This is something known as list initialization, which is available since C++11. You can find more about it over here: http://en.cppreference.com/w/cpp/language/list_initialization

  2. for (const char* p = mystart; *p; p++) This will loop for as long as there are characters left in your string.

  3. string(mystart,p-mystart) This is an overloaded constructor of the string(). In your case, it copies the first p-mystart characters. You can find more about this over here (number 5 on the list): http://www.cplusplus.com/reference/string/string/string/

1
  1. bool instring{false} means what you think, using C++11 initializer lists.
  2. c_str() returns a pointer to a c-style string, that is, null terminated. *p will dereference to '\0' at the end of the string, which is 0, which evaluates to false in the loop
  3. string (const char* s, size_t n); is the constructor being used, passing the start and the size (p-mstart). http://www.cplusplus.com/reference/string/string/string/
Ramon
  • 1,169
  • 11
  • 25
1

For item 1, maybe you mean bool instring(false); (paren instead of curly brace). Yes, that means initialize instring to the value 'false';

For item 2, *p is 0 means that you have reached the end of the null terminated string. So the loop will stop when you reach the end of the string.

For item 3, the first arg to string is a string, the second is an integral (numeric) value representing the length of the string or portion of the string.

Bill Dhimitri
  • 204
  • 2
  • 6