2014-12-06 45 views
0

我想流出std::string但我希望能夠在沒有前兩個字符或最後兩個字符的情況下執行此操作。流出部分字符串

例如:

string foo{"bu blah blah blee le"}; 

cout << foo.substr(2) << endl << foo.substr(0, foo.size() - 2) << endl; 

是否有任何iomanip工具是什麼?還是應該繼續構建臨時的string

回答

1

你可以使用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'; 
+1

這不是流化這樣的。 – interjay 2014-12-06 15:44:03

+0

@interjay固定。 – Barry 2014-12-06 16:59:41