【发布时间】:2016-07-01 08:02:00
【问题描述】:
有人可以解释/确认下面几行的含义吗?
-
bool instring{false};- 这意味着 false 是这个变量的初始值,是吗? -
for (const char* p = mystart; *p; p++)- 这里的指针*p在for的第二个参数处意味着这个循环存在到这个指针存在的那一刻,是吗? -
string(mystart,p-mystart)- 我在 c++ reference 中找不到这个字符串用法,我知道它的结果是这个参数之间的差异,但不明白这是怎么发生的。
这几行来自下面的代码(来自另一个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
}
【问题讨论】:
标签: c++ arrays string pointers