C++ の文字列について質問があります
ユーザー22文字から読み取り、文字列に保存したい
私は試した:
std::string name;
std::cin.getline(name,23);
エラーが表示されます。
string で cin.getline を使用する解決策は何ですか?
std::getline(std::istream&, std::string&)
代わりにfromを使用し<string>
ます。
22 文字に制限したい場合はstd::string
、C スタイルの API に渡すのと同じように使用できます。
std::string example;
example.resize(22); // Ensure the string has 22 slots
stream.getline(&example[0], 22); // Pass a pointer to the string's first char
example.resize(stream.gcount()); // Shrink the string to the actual read size.