Getline 直接到数组 C++,在一行中

getline directly to array c++, in one line

本文关键字:一行 数组 C++ Getline      更新时间:2023-10-16

有没有办法直接将getline存储到c char array中。

目前,我正在做这样的事情...

istreams.getline(name, sizeof(name));
strcpy(names[i], name);

有没有办法只在一行中做到这一点,例如istreams.getline().

替换这两行

istreams.getline(name, sizeof(name));
strcpy(names[i], name);

对于这个

istreams.getline( names[i], sizeof(names[i]));

我想names是一个二维字符数组。否则,如果names[i]是一个指针,那么你必须使用其他值而不是表达式sizeof(names[i])

您也可以使用 std::vector<std::string> 作为字符串的容器。

例如

while ( std::getline( istreams, name ) ) v.push_back( name );