Upcoming SlideShare
×

# Stroustrup11章雑感

859 views

Published on

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
859
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
2
0
Likes
1
Embeds 0
No embeds

No notes for slide
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• ### Stroustrup11章雑感

1. 1. 20代で知っておくべきマニピュレータのこと 第11章雑感 by sato@ipl
2. 2. マニピュレータの分別<iostream>で定義されているマニピュレータ<iomanip>で定義されているマニピュレータ
3. 3. <iostream>boolalpha, noboolalphashowbase, noshowbaseshowpoint, noshowpointskipws, noskipwsunitbuf, nounitbufuppercase, nouppercasedec, hex, octﬁxed, scientiﬁcinternal, left, rightwsendl, ends, ﬂush
4. 4. <iomanip>setiosﬂags, resetiosﬂagssetbasesetﬁllsetprecisionsetw
5. 5. <iostream>
6. 6. boolalpha, noboolalphabool型と”true”,”false”を変換します。<istream> <ostream>#include <iostream>using namespace std;int main() {! bool b;! b = true;! cout << boolalpha << b << endl;! cout << noboolalpha << b << endl;! return 0;}true1
7. 7. showpoint, noshowpoint必要がない場合でも小数点を表示します。<ostream>#include <iostream>using namespace std;int main() {! double a, b, pi;! a = 30.0;! b = 10000.0;! pi = 3.1416;! cout.precision(5);! cout << showpoint << a << t << b << t << pi << endl;! cout << noshowpoint << a << t << b << t << pi << endl;! return 0;}30.000! 10000.! 3.141630! 10000! 3.1416
8. 8. showpos, noshowpos正の数値の前に+符号を表示します。<ostream>#include <iostream>using namespace std;int main() {! signed int p, z, n;! p = 1;! z = 0;! n = -1;! cout << showpos << p << t << z << t << n << endl;! cout << noshowpos << p << t << z << t << n << endl;! return 0;}+1! 0! -11! 0! -1
9. 9. skipws, noskipwsストリームからの読み込み時に、ホワイトスペースを読み飛ばします。<istream>#include <iostream>#include <sstream>using namespace std;int main(){! char a, b, c;! istringstream iss ("! 123");! iss >> skipws >> a >> b >> c;! cout << a << b << c << endl;! iss.seekg(0);! iss >> noskipws >> a >> b >> c;! cout << a << b << c << endl;! return 0;}123! 12
10. 10. uppercase, nouppercase数値の文字表記の際に大文字で表示します。<ostream>#include <iostream>#include <string>using namespace std;int main(){! cout << showbase << hex;! cout << uppercase << 77 << endl;! cout << nouppercase << 77 << endl;! return 0;}0X4D0x4d
11. 11. unitbuf, nounitbuf出力毎にバッファをフラッシュします。<ostream>#include <fstream>using namespace std;int main() {! ofstream outﬁle("test.txt");! outﬁle << unitbuf << "Test " << "ﬁle" << endl;! outﬁle.close();! return 0;}Test ﬁle
12. 12. internal, left, right数値出力を符号と数値で分けるか、左寄せ、右寄せに指定します。<ostream>#include <iostream>using namespace std;int main() {! int n;! n = -77;! cout.width(6);! cout << internal << n << endl;! cout.width(6);! cout << left << n << endl;! cout.width(6);! cout << right << n << endl;! return 0;}- 77-77 -77
13. 13. wsホワイトスペースを抽出します。<istream>#include <iostream>#include <sstream>using namespace std;int main() {! char a[10], b[10];! istringstream iss ("one n t two");! iss >> noskipws;! iss >> a >> ws >> b;! cout << a << "," << b << endl;! return 0;}one,two
14. 14. endl, ends改行、ヌル文字(‘0’)を追加します。<ostream>#include <iostream>using namespace std;int main() {! int a = 100;! double b = 3.14;! cout << a;! cout << endl;! cout << b << ends << a*b << endl;! return 0;}1003.14314
15. 15. <iomanip>
16. 16. setiosﬂags, resetiosﬂags複数のフラグを設定、解除します。<istream> <ostream>#include <iostream>#include <iomanip>using namespace std;int main() {! cout << hex << setiosﬂags(ios_base::showbase | ios_base::uppercase);! cout << 100 << endl;! cout << resetiosﬂags(ios_base::showbase | ios_base::uppercase);! cout << 100 << endl;! return 0;}0X6464
17. 17. setbase基数を指定します。 <ostream>#include <iostream>#include <iomanip>using namespace std;int main() {! cout << setbase(16) << 100 << endl;! return 0;}64
18. 18. setﬁll文字列に対して指定した文字を埋めます。<ostream>#include <iostream>#include <iomanip>using namespace std;int main() {! cout << setﬁll(x) << setw(10);! cout << 77 << endl;! return 0;}xxxxxxxx77
19. 19. ところで<iostream>と<iomanip>のマニピュレータの違いは？
20. 20. ところで<iostream>と<iomanip>のマニピュレータの違いは？ A. 引数なしか引数付きか
21. 21. 以上
22. 22. 参考文献manipulators - C++ ReferenceC++入出力フラグ