åèï¼ http://d.hatena.ne.jp/faith_and_brave/20100201/1264997004
ãC++0xã«ãªãã¨ãC++03 ã§ãã¡ããã¡ãããé¨åãã ãã¶ãã£ããæ¸ããããã«ãªãã
ãããã§ãããC++0xãå¾
ããªãã¦ãBoostã使ãã°ã ãã¶ãã£ããæ¸ããã®ã§ã
Boostã§ã®C++å
¥éã¯ãããªæãã ãã¼ãã¨ããæ°æã¡ã§ä»¥ä¸ç¥ã
ãã®è¨äºã¯Cè¨èªãããç¨åº¦ç解ãã¦ãããã¨ãåæã§ãã
1. Hello World
C++/Boostã§ã®åºåã¯C++æ¨æºã® IOStream ã©ã¤ãã©ãªã¨ Boost.Format ãçµã¿åããã¦è¡ãã¾ãã
ä¾ã¨ãã¦ãCè¨èªã®printfãç¨ãã Hello World ããC++/Boostã使ã£ã¦æ¸ãç´ãã¦ã¿ã¾ãã
#include <stdio.h> int main() { printf( "%s\n", "Hello, World" ); return 0; }
以ä¸ã®ã³ã¼ããC++/Boostã§æ¸ãã¨ã以ä¸ã®ããã«ãªãã¾ãã
#include <iostream> // std::cout ã使ãã®ã«å¿ è¦ #include <boost/format.hpp> // boost::format ã使ãã®ã«å¿ è¦ int main() { std::cout << boost::format("%s\n") % "Hello, World"; return 0; }
std::cout ã¨ããã®ã¯æ¨æºåºåã§ããCã§è¨ã stdout ã§ãã
ãæ¨æºåºåï¼console outï¼ã« "Hello, World" ãæ´å½¢ï¼formatï¼ãã¦åºåãããã¨èªã¿ã¾ãã
boost::format ã«æ¸¡ãæååã¯ãCè¨èªã® printf ã¨åãã¹ã¿ã¤ã«ã§æ¸ããã¨ãåºæ¥ã¾ãã
è¤æ°ã®å¼æ°ã渡ãã«ã¯ã
std::cout << boost::format("%s, %s\n") % "Hello" % "World";
ã®ããã« % æ¼ç®åãã¤ãªãã¦æ¸ãã¾ãã
ã¾ãããã¨ã¯å¥ã®ã¹ã¿ã¤ã«ã¨ãã¦ã
std::cout << boost::format("%1%, %2%\n") % "Hello" % "World";
ãã®ããã«ãä½çªç®ã®å¼æ°ã表示ãããããæè¨ãã¦æ¸ããã¨ãåºæ¥ã¾ãã
ãã®å ´åã
std::cout << boost::format("1, 2, %1%, 4, %2%, %1%, 7, 8, %1%, %2%\n") % "Fizz" % "Buzz";
ãã®ããã«å¼æ°ãä½åã使ãåãããã§ãã¾ãã
使ãåããªãå ´åã§ãããã®æ¸ãæ¹ãªã ãã¡ãã¡åãæå®ããå¿
è¦ããªãã®ã§ãªã¹ã¹ã¡ã§ãã
2. å¼
Boost.Format ã«æ¸¡ãå¤ã¯ãå¥ã«æååã§ããå¿ è¦ã¯ããã¾ããã
#include <iostream> #include <boost/format.hpp> int main() { std::cout << boost::format("%1% = %2%\n") % "1 + 1" % ( 1 + 1 ); return 0; }
ãã®ããã«æ¸ããã¨ã§ãå¼ã®è¨ç®çµæã表示ããããã¨ãåºæ¥ã¾ãã
()ã§ãããã§ããã®ã¯ % æ¼ç®åã®åªå
é ä½ã®é¢ä¿ã§ãã Boost.Format ã«æ¸¡ãå¼æ°ãè¤éãªå ´åã¯()ã§ãããã§æ¸¡ããæ¹ãæ©ã¾ãªãã§æ¸ãã¨æãã¾ãã
Cè¨èªã¨åæ§ãæ´æ°å士ã®å²ãç®ãè¡ãå ´åã«ã¯æ³¨æãå¿
è¦ã§ãã
#include <iostream> #include <boost/format.hpp> int main() { std::cout << boost::format("%1%\n") % ( 3 / 2 ); // 端æ°ãåæ¨ã¦ããã std::cout << boost::format("%1%\n") % ( 3.0 / 2.0 ); // ãã¡ãã¨ç«¯æ°ãèæ ®ããã return 0; }
è¤éãªè¨ç®ãè¡ããããã¨ãåºæ¥ã¾ãã
#include <iostream> #include <boost/format.hpp> #include <cmath> // std::sin ã使ãã®ã«å¿ è¦ int main() { std::cout << boost::format("%1%\n") % ( 1 * 2 + 2 * 3 ); // * 㯠+ ããåªå ããã std::cout << boost::format("%1%\n") % ( 10 * ( 10 - 1 ) / 2 ); // () ãç¨ãã¦åªå é ä½ãå¤ãããã std::cout << boost::format("%1%\n") % std::sin(1.0); // sinãæ±ãããåä½ã¯ã©ã¸ã¢ã³ return 0; }
3. å¤æ°
Cè¨èªã¨åæ§ã§ãããC++/Boostã§ã¯åºæ¬çã« const
ãä»ãã¦å®£è¨ãããã¨ãæã¾ããã§ãã
#include <string> // std::string ã使ãã®ã«å¿ è¦ #include <iostream> #include <boost/format.hpp> int main() { const int i = 23; // 3 ã¨ããæ´æ°(int)ã« a ã¨ããååãå²ãå½ã¦ã const std::string s = "abc"; // ãã¡ãã¯æåå std::cout << boost::format("i ã®å¤ã¯'%1%', s ã®å¤ã¯'%2%'.\n") % i % s; return 0; }
ãã®ããã« "const
å å¤æ°å =
å¤;
" ã®ããã«æ¸ãã¨ã§ããã®å¤ã«å¯¾ãã¦ååãã¤ãããã¨ãå¯è½ã«ãªãã¾ãã
以éã¯ã23
ã¨ãã£ãå
·ä½çãªå¤ã®ä»£ããã«ãi
ã¨ãã£ã宣è¨ããå¤æ°ã®ååãæ¸ããã¨ã§ãå
·ä½çãªå¤ãæ¸ããã®ã¨åãããã«æ¯èããããã¨ãåºæ¥ã¾ãã
"const
å å¤æ°ï¼ =
å¤ï¼,
å¤æ°ï¼ =
å¤ï¼,
å¤æ°ï¼ =
å¤ï¼;
" ã®ããã«ãè¤æ°ã®å¤æ°ãå®ç¾©ãããã¨ãã§ãã¾ãã
#include <iostream> #include <boost/format.hpp> int main() { const int mikan = 30, apple = 100; // ã¿ããã¨ãããã®å¤æ®µ std::cout << boost::format("ã¿ããï¼åã¨ãããï¼åã§ãåè¨ %1% å\n") % ( mikan * 7 + ringo * 3 ); return 0; }
4. é¢æ°
é¢æ°ã使ããã¨ã§å¦çã«ååãä»ãããã¨ãåºæ¥ã¾ãã
"æ»ãå¤ã®å é¢æ°å (
ä»®å¼æ°ãªã¹ã )
{
å¦ç }
" ã®ããã«æ¸ãã¾ãã
// å¼æ°ãåããªãä¾ int the_Answer_to_life_the_universe_and_everything() { // "return å¤;" ã§å¤ãè¿ã return 42; } // å¼æ°ã²ã¨ã¤ãåãä¾ int square( int x ) { return x * x; } // è¤æ°ã®å¼æ°ãåãä¾ double ave2( double x, double y ) { return ( x + y ) / 2; }
é¢æ°ãå¼ã³åºãã«ã¯ã "é¢æ°å (
å¼æ°ãªã¹ã )
" ã®ããã«æ¸ãã¾ãã
// ãã£ãã®é¢æ°å®ç¾©ãããã«ããã¨ãã #include <iostream> #include <boost/format.hpp> int main() { // é¢æ°ã®æ»ãå¤ã«ååãã¤ãã const int i = the_Answer_to_life_the_universe_and_everything(); const int s = square(i); std::cout << boost::format("%1% ã®äºä¹ã¯ %2% ã§ãã\n") % i % s; // ç´æ¥ä½¿ããã¨ãåºæ¥ã std::cout << boost::format("%1% 㨠%2% ã®å¹³å㯠%3% ã§ãã") % i % s % ave2( i, s ); return 0; }
Cè¨èªã¨åæ§ãä½ãå¤ãè¿ããªãé¢æ°ãå®ç¾©ãããã¨ãåºæ¥ã¾ãããã®å ´å㯠void
ã¨ããç¹å¥ãªåã使ãã¾ãã
#include <iostream> #include <boost/format.hpp> void disp(const std::string& s) { std::cout << boost::format("%1%\n") % s; } int main() { disp("Hello World"); return 0; }
ã¾ãBoostã«ã¯é¢æ°ãã©ãããã Boost.Function ã¨ãããã®ãåå¨ããããã使ããã¨ã§ãé¢æ°ãå¤æ°ã¨ãã¦æ±ããã¨ãåºæ¥ã¾ãã
#include <iostream> #include <boost/format.hpp> #include <cmath> // for std::sqrt #include <boost/function.hpp> int the_Answer_to_life_the_universe_and_everything() { return 42; } void disp(const std::string& s) { std::cout << boost::format("%1%\n") % s; } double norm2( double x, double y ) { return std::sqrt( x * x + y * y ); } int main() { // the_Answer_to_life_the_universe_and_everything ã«å¥åãä»ãã const boost::function<int()> f1 = the_Answer_to_life_the_universe_and_everything; std::cout << boost::format("%1%\n") % f1(); // f1() 㯠the_Anï¼ä¸ç¥ï¼ing() ã¨åã // disp ã«å¥åãä»ãã const boost::function<void(const std::string&)> f2 = disp; f2("Hello World"); // disp("Hello, World"); ã¨åã // norm2 ã«å¥åãä»ãã const boost::function<double (double, double)> f3 = norm2; std::cout << boost::format("%1%\n") % f3( 3, 4 ); // norm2( 3, 4 ) ã¨åã return 0; }
ååã¯"[]boost::function<[]
æ»ãå¤ã®å (
å¼æ°åãªã¹ã )[]>[]
"ã¨ãªãã¾ãã
5. å
C++/Boost ã«ã¯ä»¥ä¸ã®ãããªåãç¨æããã¦ãã¾ã(ä¸é¨ã§ã)ã
åºæ¬ãã¼ã¿å int : æ´æ°å(0, 1, 2, 3, -1, etc...) char : æåå('a', 'b', 'c', '1', etc...) double : æµ®åå°æ°ç¹æ°å(0.1, 3.14, etc...) bool : è«çå(true, false) è¤åãã¼ã¿å boost::array<T, N> : é å(<boost/array.hpp>ãã¤ã³ã¯ã«ã¼ã) std::vector<T> : ãªã¹ã(<vector>ãã¤ã³ã¯ã«ã¼ã) std::string : æåå(<string>ãã¤ã³ã¯ã«ã¼ã) std::map<Key, Value> : è¾æ¸(<map>ãã¤ã³ã¯ã«ã¼ã) std::set<T> : éå(<set>ãã¤ã³ã¯ã«ã¼ã) boost::function<Signature> : é¢æ°(<boost/function.hpp>ãã¤ã³ã¯ã«ã¼ã) boost::tuple<T1,T2, ...> : è¤æ°ã®å¤ã®è©°ãåãã(<boost/tuple/tuple.hpp>ãã¤ã³ã¯ã«ã¼ã) boost::shared_ptr<T> : ãã¤ã³ã¿(<boost/shared_ptr.hpp>ãã¤ã³ã¯ã«ã¼ã) boost::optional<T> : Tåãç¡å¹å¤(<boost/optional.hpp>ãã¤ã³ã¯ã«ã¼ã) boost::variant<T1,T2, ...> : å¯å¤å(Cã§ããunion)(<boost/variant.hpp>ãã¤ã³ã¯ã«ã¼ã) boost::any : ä»»æå(<boost/any.hpp>ãã¤ã³ã¯ã«ã¼ã)
6. åºæ¬æ¼ç®
C++/Boost ã®æ¼ç®åã¯æ®ã©Cè¨èªã¨åæ§ã§ãã
ãã ã >> æ¼ç®åããã³ << æ¼ç®åã¯ãå¾æ¥ã®æå³ï¼ãããã·ããï¼ã«å ãã¦ãã¹ããªã¼ã å
¥åºåãã¨ããæå³ãæã¡ã¾ãã
ã¾ã % æ¼ç®åããå¾æ¥ã®æå³ï¼å°ä½æ¼ç®ï¼ã«å ãã¦ããã©ã¼ãããåºåãã¨ããæå³ãæã¡ã¾ãã
ãããã¯ãæ¼ç®åå¤éå®ç¾©ãã¨ããä»çµã¿ã«ãã£ã¦å®ç¾ããã¦ãã¾ãããã©ã¤ãã©ãªè£½ä½è
以å¤ã¯ãã®ä»çµãå¦ç¿ããå¿
è¦ã¯ãªãã§ãããã
7. åç §
åç §ã¯å¤æ°ã«å¥åãä¸ãã¾ãã
// conståç § const int a = 42; const int& b = a; // b 㯠a ãåç §ãã std::cout << boost::format("%1%\n") % b; // boost::format("%1%\n") % a ã¨åã // å¯å¤ãªåç § int x = 1; int& y = x; // y 㯠x ãåç §ãã y = 23; // x = 23; ã¨åããåç §å ã® x ãæ¸ãæãã
åç
§ã¯é¢æ°ã®å¼æ°ã¨ãã¦ä½¿ããã¨ãåºæ¥ã¾ãã
ç¹ã«constãªåç
§ã§æ¸¡ãããå¤æ°ã¯ãç¡é§ãªã³ãã¼åä½ãèµ·ãããªãã®ã§ãã³ã¹ããæ°ã«ããå ´åã«ãã°ãã°ç¨ãããã¾ãã
ã¾ããå¯å¤ãªåç
§ããè¤æ°ã®å¤ãè¿ãããã«ä½¿ããã¨ãåºæ¥ã¾ãã
// http://d.hatena.ne.jp/faith_and_brave/20100201/1264997004 ã®ä¾ void get_ip(int& aa, int& bb, int& cc, int& dd) { aa = 127; bb = 0; cc = 0; dd = 1; } int a = 0; int b = 0; int c = 0; int d = 0; get_ip(a, b, c, d);
ãããããã®å ´åã¯constã§ã¯ãªããªã£ã¦ãã¾ãçºã Boost.Tuple ã使ã£ãæ¹ãè¯ãã§ãï¼
#include <boost/tuple/tuple.hpp> boost::tuple<int, int, int, int> get_ip() { return boost::make_tuple( 127, 0, 0, 1 ); } #include <iostream> #include <boost/format.hpp> int main() { const boost::tuple<int, int, int, int> t = get_ip(); const int& a = boost::get<0>(t); const int& b = boost::get<1>(t); const int& c = boost::get<2>(t); const int& d = boost::get<3>(t); std::cout << boost::format("%1%.%2%.%3%.%4%\n") % a % b % c % d; }
8. å¶å¾¡æ§æ
C++/Boost ã§ã¯ãCè¨èªã® if/switch/while/for ã«å ããç¯å² for æ§æãæ¬ä¼¼çã«ãµãã¼ããã¦ãã¾ãã
#include <boost/foreach.hpp> // ãã¯ãBOOST_FOREACHã使ããããã«ãã #include <iostream> #include <boost/format.hpp> int main() { const int a[] = {1, 2, 3}; BOOST_FOREACH( const int x, a ) { std::cout << boost::format("%1%\n") % x; } return 0; }
ã¾ãã if æã®æ¡ä»¶é¨ã for æã®åæåé¨çã§å¤æ°ã宣è¨ãããã¨ãåºæ¥ã¾ãã
#include <cmath> #include <boost/optional.hpp> #include <iostream> #include <boost/format.hpp> // boost::optional ã使ã£ãä¾ // 詳ãã説æããã¨è±ç·ããã®ã§ããããªæ¸ãæ¹ããããç¨åº¦ã«æã£ã¦ä¸ãã boost::optional<double> my_sqrt( double x ) { if( x < 0 ) { return boost::none; } else { return std::sqrt(x); } } int main() { // ifæä¸ã§å¤æ°ã宣è¨ã if( const boost::optional<double> x_ = my_sqrt(-1) ) { const double& x = x_.get(); // ããã使ã std::cout << boost::format("%1%\n") % x; } else { std::cout << boost::format("%1%\n") % "è² ã®å¤ã§ãï¼"; } return 0; }
9. ã¯ã©ã¹
Cã§è¨ãæ§é ä½ã§ãã C++/Boost ã§ã¯ãã¯ã©ã¹ã使ããã¨ã§æ°ããåãå®ç¾©ãããã¨ãåºæ¥ã¾ãã
ä»ã¾ã§åºã¦ãã std::string ã boost::function ãªã©ã¯å
¨ã¦ã¯ã©ã¹ã§ãããããã¯åºæ¬åã¨æ®ã©åãããã«æ±ããã¨ãåºæ¥ãä»ã "å¤æ°å.
ã¡ã³ãé¢æ°å" ã¨æ¸ããã¨ã«ããããã®ã¯ã©ã¹å°ç¨ã«ç¨æãããé¢æ°ã使ããã¨ãåºæ¥ã¾ãã
const std::string s = "abc"; std::cout << boost::format("%1%\n") % s.length(); // s ã®é·ããæ±ãã
ã¦ã¼ã¶å´ã§å®ç¾©ãããã¨ãåºæ¥ã¾ãããã©ã¤ãã©ãªå´ã§å®ç¾©ãããã¯ã©ã¹ã使ãã ãã§ããåäºåã«ããã°ã©ãã³ã°ã¯å¯è½ã§ãã
èªåã§ã¯ã©ã¹ãä½ããã¨ã¯è½ã¨ãç©´ãå¤ãã®ã§ãæ
£ããªããã¡ã¯ã©ã¤ãã©ãªå®ç¾©ã®ã¯ã©ã¹ã使ãã¾ãããã
10. ãã³ãã¬ã¼ã
boost::function<int()> çã§æ¢ã«è§¦ãããã¦ããç©ã§ãã
ä»»æã®åã«å¯¾ãã¦åããã¨ãããããå ´åã«ç¨ãã¾ãã
ä¾ã¨ãã¦ç°¡åãªãã³ãã¬ã¼ãé¢æ°ãä½ã£ã¦ã¿ãã¨ã
template<typename T> void disp( T x ) { std::cout << boost::format("%1%\n") % x; }
ãã®ããã«æ¸ããã¨ã§ã disp
é¢æ°ã¯ãããããåã®å¤æ° x ãåãå
¥ãããã¨ãåºæ¥ãããã«ãªãã¾ãã
ä¾ãã°ã
disp<int>(23);
ã®ããã«å¼ã³åºãã¨ã disp
ã® T
ã int
ã§ç½®ãæãã
void disp( int x ) { std::cout << boost::format("%1%\n") % x; }
ã¨ããé¢æ°ãã³ã³ãã¤ã©ã«ãã£ã¦èªåçæãããå¼ã³åºããã¾ãã
åæ§ã« []disp<std::string>("abc");[]
ã®ããã«å¼ã³åºãã¨ã
void disp( std::string x ) { std::cout << boost::format("%1%\n") % x; }
ã¨ããé¢æ°ãå¼ã³åºããã¾ãã
ã¾ãããããã®è§æ¬å¼§ <> ã¯çç¥å¯è½ã§ãã
const double d = 3.14; disp(d);
ãã®ããã«æ¸ãã°ã d
ã®åã§ãã double
çã® disp
ãå¼ã°ãã¾ãã
ã¾ããåé
ã®ã¯ã©ã¹ãããã³ãã¬ã¼ãã«ãããã¨ãåºæ¥ã¾ãã []std::vector<int> v;[]
ã®ããã«ä½¿ãã¾ãã
ãã®å ´åãè§æ¬å¼§ã¯çç¥ä¸è½ã§ãã
11. åºå®é·é å
Cè¨èªã®é
åã¯C++/Boostã§ã使ãã¾ãããC++/Boostã«ã¯ã¯æ§ã
ãªç¹ã§ä¾¿å©ãª boost::array<T, N> ã¨ããã¯ã©ã¹ãç¨æããã¦ãã¾ãã
ãã®ã¯ã©ã¹ã¯é常ã®é
åã¨åæ§ã«æ±ããä»ãã¡ã³ããã¨ã®ã³ãã¼ãªã©ãç°¡åã«è¡ãã¾ãã
#include <boost/array.hpp> #include <boost/foreach.hpp> #include <iostream> #include <boost/format.hpp> int main() { const boost::array<int, 3> a = {{ 1, 2, 3 }}; // è¦ç´ æ°åå¾ const int size = a.size(); // 3 // è¦ç´ ã¢ã¯ã»ã¹ const int front = a.front(); // 1 const int back = a.back(); // 3 const int second = a[1]; // 2 // å ¨é¨è¡¨ç¤º BOOST_FOREACH( const int x, a ) { std::cout << boost::format("%1%\n") % x; } // ã³ãã¼ boost::array<int, 3> b = a; // ç ´å£çåä½ // è¦ç´ æ¸ãæã for( std::size_t i = 0; i < b.size(); ++i ) { b[i] = i; } // å ¨è¦ç´ ã 0 ã§åãã b.assign( 0 ); return 0; }
12. ãªã¹ã
Cè¨èªã§ã¯åãæ±ãã®é£ããã£ãå¯å¤é·é åã¯ãC++/Boost ã§ã¯ std::vector<T> ã¨ããã¯ã©ã¹ã«ã¾ã¨ãããã¦ãã¾ãã
#include <vector> #include <boost/assign.hpp> #include <boost/foreach.hpp> #include <iostream> #include <boost/format.hpp> int main() { // æ§ç¯ã¯å°ãé¢åãC++ã®æ¬¡ä¸ä»£è¦æ ¼ã§æ¹åããã const std::vector<int> a = boost::assign::list_of(1)(2)(3)(4); // boost::array ã¨åãããã«æ±ãã const int size = a.size(); // 4 const int front = a.front(); // 1 const int back = a.back(); // 4 const int second = a[1]; // 2 // 以ä¸ãç ´å£çåä½ std::vector<int> b; using boost::assign::operator+=; // += ã«ããè¦ç´ 追å ãè¡ãã®ã«å¿ è¦ b += 2, 3, 5, 7, 11, 13; // é£ç¶çè¦ç´ 追å b.push_back( 17 ); // å¾ãã«è¦ç´ ã追å ãã b.pop_back(); // æå¾ã®è¦ç´ ãåé¤ãã // è¦ç´ æ¸ãæã // BOOST_FOREACH ã使ã£ã¦ã¿ã BOOST_FOREACH( int& x, b ) // int& ã¨ãããã¨ã§ãæ¸ãæããå¯è½ã«ãªã { x *= 2; } // å ¨é¨è¡¨ç¤º BOOST_FOREACH( const int x, b ) { std::cout << boost::format("%1%\n") % x; } return 0; }
13. é£æ³é å
std::map<Key, Value>, boost::unordered_map<Key, Value> ã¨ããäºç¨®é¡ã®é£æ³é åãç¨æããã¦ãã¾ããåè ã¯äºåæ¨ãå¾è ã¯ããã·ã¥ãã¼ãã«ã«ãã£ã¦å®è£ ããã¦ãã¾ãã
#include <boost/unordered_map.hpp> // æ®æ®µã¯é«éãªãã£ã¡ã使ã // #include <map> // BOOST_FOREACH ã§åå¾ããæã«ã½ã¼ãããã¦ããå¿ è¦ãããå ´åç¨ #include <boost/assign.hpp> #include <boost/foreach.hpp> #include <iostream> #include <boost/format.hpp> int main() { // å°ãé¢åãªã®ã§ map ã®åã typedef ãã typedef boost::unordered_map<std::string, int> map_type; // typedef std::map<std::string, int> map_type; // ãã¯ãæ§ç¯ã¯å°ãé¢åãC++ã®æ¬¡ä¸ä»£è¦æ ¼ã§æ¹åããã const map_type m = boost::assign::map_list_of("mikan", 30)("ringo", 100)("meron", 2000); // m.count(è¦ç´ ) ã§ããã®è¦ç´ ãåå¨ãã¦ããããã§ãã¯ã§ãã if( m.count("ringo") ) { // ãããã®å¤æ®µã¯ï¼ const int ringo = m.at("ringo"); std::cout << boost::format("%1%\n") % ringo; } // è¦ç´ ã®æ¸ãæãã¨ã map_type m2 = m; // è¦ç´ ã¢ã¯ã»ã¹ï¼ãªããã°ä½ãï¼ m2["iyokan"] = 100; // å ¨é¨è¡¨ç¤ºããï¼å°ãé¢åï¼ BOOST_FOREACH( const map_type::value_type x, m2 ) { // x.first ããã¼ã x.second ãå¤ std::cout << boost::format("%1%: %2%\n") % x.first % x.second; } return 0; }
14. åå空é
ä»ã¾ã§ std::cout ã boost::format ã¨ä½¿ã£ã¦ããä¸ã®ã std ã¨ã boost ã¨ãã§ãã
ãããã¯åå空éã¨å¼ã°ããåãååã®é¢æ°ãã¯ã©ã¹ã®è¡çªãé¿ããããã«ä½¿ããã¾ãã
namespace A { int f() { return 23; } } namespace B { int f() { return 42; } } #include <iostream> #include <boost/format> int main() { std::cout << boost::format("%1%\n") % A::f(); // 23 std::cout << boost::format("%1%\n") % B::f(); // 42 }
ãã¡ãã¡ä½åã std:: ã boost:: ãæ¸ãã®ãé¢åãªå ´åã¯ã using 宣è¨ã¨ãããã®ãè¡ãã¾ãã
#include <iostream> #include <boost/format> int main() { using std::cout; // 以éã¯åã« cout ã§ä½¿ããããã«ãªã using boost::format; // åæ§ã« format åç¬ã§ä½¿ããããã«ãªã cout << format("%1%\n") % "Hello, World"; return 0; }
ããã« using namespace std;
ã¨æ¸ããã¨ã§ã std åå空éã«ããå
¨ã¦ã®ååã std:: ã¨æ¸ããã«ä½¿ããã¨ãåºæ¥ã¾ãããæå³ããªãåä½ã«ãªãå¯è½æ§ãããããããªã¹ã¹ã¡ã¯ãã¾ããã
15. ãã®ä»ã®ç´°ãããã¨
ãã®é ã¯éæ追å ããããããã¾ããã
- main é¢æ°ã«éããæå¾ã®
return 0;
ã¨ããè¨è¿°ã¯çç¥ã§ãã¾ãããã®å ´åã«ã¯æå¾ã«return 0;
ãå¼ã°ããäºã«ãªãã¾ãã - å®ã¯åç´ãªåºåãªã boost::format ã使ããªãã¦ã
std::cout << "Hello, World\n";
ã®ããã«æ¸ãã¾ããè¤éãªåºåã§ã[]std::cout << "i ã®å¤ã¯" << i << std::endl;[]
çã« << æ¼ç®åãé£ç¶ããããã¨ã§è¨è¿°ã§ãã¾ãããã ãèªã¿ã«ãããªãã®ã§åºæ¬ã¯ Boost.Format ã使ãã¹ãã§ãããªã std::endl ã¯ãæ¹è¡ãã¦åºåãããã¨ããæå³ã§ãã - å®ã¯ boost::format ã®åæå®æå %s ã¯åéä¾åã§ãã
const int i = 42; std::cout << boost::format("%s\n") % i;
ã¨ããã³ã¼ããã¨ã©ã¼ã«ãªãã表示ããã¾ãã