Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想流出 astd::string但我希望能够在没有前两个字符或最后两个字符的情况下做到这一点。
std::string
例如:
string foo{"bu blah blah blee le"}; cout << foo.substr(2) << endl << foo.substr(0, foo.size() - 2) << endl;
有什么iomanip工具吗?还是我应该继续构建临时strings?
iomanip
string
你可以使用cout.write:
cout.write
cout.write(foo.c_str() + 2, foo.size() - 4);
它还返回流,因此您可以执行以下操作:
cout << "First two: "; cout.write(foo.c_str(), 2) << "\nAll but first two: "; cout.write(foo.c_str() + 2, foo.size() - 2) << '\n';