istream& getline (istream& is, string& str);
ここで、str は改行文字の後で終了します。しかし、str コンテンツが 2 ~ 3 行ある状況に対処したい場合、代替手段は何ですか?
2 に答える
1
いくつかのサンプル入力を使用できると思いますが、このコードは行がstd::cin
見つからなくなるまで から行を読み取り、それらすべての行をstd::vector
.
#include <iostream>
#include <vector>
int main() {
std::string line;
std::vector<std::string> lines;
while (std::getline(std::cin, line)) { // iterates until exhaustion
lines.push_back(line);
}
// lines[k] can be used to fetch the k'th line read, starting from index 0
// Simply repeat the lines back, prepended with a "-->"
for (auto line : lines) {
std::cout << "--> " << line << '\n';
}
}
たとえば、私が入力した場合
cat
bat
dog
私のプログラムの出力
--> cat
--> bat
--> dog
于 2013-08-23T05:55:18.343 に答える