├── .gitignore ├── any ├── Makefile └── main.cpp ├── rand ├── Makefile └── main.cpp ├── string_split ├── Makefile └── main.cpp ├── tuple ├── Makefile └── main.cpp ├── tuple_tie ├── Makefile └── main.cpp ├── scoped_ptr ├── Makefile └── main.cpp ├── regex ├── Makefile └── main.cpp ├── regex_replace ├── Makefile └── main.cpp ├── regex_split ├── Makefile └── main.cpp ├── filelist ├── Makefile └── main.cpp ├── program_options ├── Makefile └── main.cpp ├── thread ├── Makefile └── main.cpp ├── regex_match ├── Makefile └── main.cpp ├── create_directory ├── Makefile └── main.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .DS_Store 3 | *.bin 4 | -------------------------------------------------------------------------------- /any/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O main.cpp -o main.bin 3 | -------------------------------------------------------------------------------- /rand/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O main.cpp -o main.bin 3 | -------------------------------------------------------------------------------- /string_split/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O main.cpp -o main.bin 3 | 4 | -------------------------------------------------------------------------------- /tuple/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | LIBPATH=$(prefix)/lib 3 | 4 | all: 5 | g++ -O main.cpp -o main.bin 6 | -------------------------------------------------------------------------------- /tuple_tie/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | LIBPATH=$(prefix)/lib 3 | 4 | all: 5 | g++ -O main.cpp -o main.bin 6 | -------------------------------------------------------------------------------- /scoped_ptr/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | all: 6 | g++ -O main.cpp -o main.bin 7 | -------------------------------------------------------------------------------- /regex/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | LIBS+=$(LIBPATH)/libboost_regex-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /regex_replace/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | LIBS+=$(LIBPATH)/libboost_regex-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /regex_split/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | LIBS+=$(LIBPATH)/libboost_regex-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /filelist/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | LIBPATH=$(prefix)/lib 3 | 4 | LIBS+=$(LIBPATH)/libboost_filesystem-mt.a 5 | LIBS+=$(LIBPATH)/libboost_system-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /program_options/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | LIBS+=$(LIBPATH)/libboost_program_options-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /thread/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | 6 | LIBS+=$(LIBPATH)/libboost_thread-mt.a 7 | 8 | all: 9 | g++ -O main.cpp -o main.bin $(LIBS) 10 | -------------------------------------------------------------------------------- /regex_match/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | INCPATH=$(prefix)/include 3 | LIBPATH=$(prefix)/lib 4 | 5 | LIBS+=$(LIBPATH)/libboost_regex-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | 10 | -------------------------------------------------------------------------------- /create_directory/Makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | LIBPATH=$(prefix)/lib 3 | 4 | LIBS+=$(LIBPATH)/libboost_filesystem-mt.a 5 | LIBS+=$(LIBPATH)/libboost_system-mt.a 6 | 7 | all: 8 | g++ -O main.cpp -o main.bin $(LIBS) 9 | -------------------------------------------------------------------------------- /regex_match/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace boost; 6 | 7 | int main(int argc, char* argv[]){ 8 | string str = "asdftesthogehoge"; 9 | 10 | if(regex_match(str, regex(".*t.st.*"))){ 11 | cout << "match" << endl; 12 | } 13 | else{ 14 | cout << "not match" << endl; 15 | } 16 | return 0; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /regex_replace/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace boost; 7 | 8 | int main(int argc, char* argv[]){ 9 | string str = "asdf\"jjjjj\"hog'ehoge"; 10 | cout << str << endl; 11 | 12 | string result = regex_replace(str, regex("[s\"\']"), "\\\\$0"); // $0でmatchした対象が取れる 13 | cout << result << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /scoped_ptr/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace boost; 6 | 7 | int main(int argc, char* argv[]) 8 | { 9 | scoped_ptr p(new string("scoped ptrを使う")); 10 | 11 | if(p) cout << *p << endl; 12 | 13 | size_t i = p->size(); 14 | cout << "size:" << i << endl; 15 | 16 | *p = "ポインタのように値を書き換え"; 17 | cout << *p << endl; 18 | return 0; 19 | } // pとstd::stringは自動破棄される 20 | -------------------------------------------------------------------------------- /rand/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace boost; 7 | 8 | int main(void){ 9 | mt19937 gen(static_cast(time(0))); 10 | uniform_smallint<> dst(1, 100); // 1~100の乱数 uniform_realでdouble型になる 11 | variate_generator > rand( gen, dst ); 12 | for (int i = 0; i < 10; i++) { 13 | std::cout << rand() << std::endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /string_split/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost::algorithm; 8 | 9 | int main() 10 | { 11 | string s = "a,b.c,as/df"; 12 | vector results; 13 | split(results, s, boost::is_any_of(",./")); 14 | 15 | cout << "要素数: " << results.size() << endl; 16 | for (int i = 0; i < results.size(); i++){ 17 | cout << results[i] << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | c++ boostlib study 2 | ================== 3 | 4 | 5 | export LIBRARY_PATH=/usr/local/lib:/opt/local/lib:$LIBRARY_PATH 6 | export LD_LIBRARY_PATH=/usr/local/lib:/opt/local/lib:$LD_LIBRARY_PATH 7 | export C_INCLUDE_PATH=/usr/local/include:/opt/local/include:$C_INCLUDE_PATH 8 | export CPLUS_INCLUDE_PATH=/usr/local/include:/opt/local/include:$CPLUS_INCLUDE_PATH 9 | export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib 10 | export BOOST_ROOT=/usr/local/include/boost 11 | 12 | -------------------------------------------------------------------------------- /tuple_tie/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace boost; 7 | 8 | // 多値を返す 9 | tuple test_func(){ 10 | return make_tuple(15, 3.14, "hello work"); 11 | } 12 | 13 | int main(int argc, char* argv[]){ 14 | int a; 15 | double b; 16 | string message; 17 | tie(a,b,message) = test_func(); 18 | 19 | cout << a << endl; 20 | cout << b << endl; 21 | cout << message << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /filelist/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost; 8 | namespace fs = boost::filesystem; 9 | 10 | int main(void) 11 | { 12 | string pt = "/Users/sho/"; 13 | fs::path path = complete(fs::path(pt, fs::native)); 14 | fs::directory_iterator end; 15 | for (fs::directory_iterator i(path); i!=end; i++){ 16 | string name = pt + i->leaf(); 17 | cout << name << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /create_directory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost; 8 | namespace fs = boost::filesystem; 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | if(argc < 2){ 13 | cerr << "require : directory path to create" << endl; 14 | return 1; 15 | } 16 | string pt = argv[1]; 17 | fs::path path = complete(fs::path(pt, fs::native)); 18 | int res = fs::create_directory(path); 19 | cout << res << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /tuple/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost; 8 | 9 | // 多値を返す 10 | tuple test_func(){ 11 | return make_tuple(15, 3.14, "hello work"); 12 | } 13 | 14 | int main(int argc, char* argv[]){ 15 | tuple result_t = test_func(); 16 | cout << result_t.get<0>() << endl; // 個別の値を取り出す 17 | cout << result_t.get<1>() << endl; 18 | cout << result_t.get<2>() << endl; 19 | 20 | cout << result_t << endl; // tuple_ioのincludeが必要 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | using namespace boost; 5 | 6 | void a(){ 7 | while(true){ 8 | sleep(1); 9 | cout << "a" << endl; 10 | } 11 | } 12 | 13 | void b(int sec){ 14 | while(true){ 15 | sleep(sec); 16 | cout << "b (" << sec << " sec)" << endl; 17 | } 18 | } 19 | 20 | int main(int argc, char* argv[]){ 21 | if(argc < 2){ 22 | cout << "main.bin 5" << endl; 23 | return 1; 24 | } 25 | int sec = atoi(argv[1]); 26 | thread th_a(a); 27 | thread th_b(b, sec); 28 | 29 | th_a.join(); 30 | th_b.join(); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /regex/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(int argc, int *argv[]){ 8 | boost::regex reg_exp; 9 | string str_reg; 10 | string str; 11 | string replace; 12 | string result; 13 | 14 | cout << "input Regular Expression ->"; 15 | getline(cin, str_reg); 16 | reg_exp = str_reg; 17 | cout << "input Replace word ->"; 18 | getline(cin, replace); 19 | 20 | while(true){ 21 | cout << "input Result word ->"; 22 | getline(cin, str); 23 | result = boost::regex_replace(str, reg_exp, replace, boost::format_no_copy); 24 | cout << result << endl; 25 | } 26 | 27 | return 0; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /regex_split/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace boost; 7 | 8 | int main(int argc, char* argv[]){ 9 | cout << "--input source string" << endl; 10 | string str; 11 | getline(cin, str); 12 | 13 | while(true){ 14 | string regex_str; 15 | cout << "--input regex" << endl; 16 | getline(cin, regex_str); 17 | regex delimiter(regex_str); 18 | 19 | vector results; 20 | string s = str; // regex_splitするとソース文字列が破壊されるので 21 | regex_split(back_inserter(results), s, delimiter); 22 | cout << str << endl; 23 | 24 | for(int i = 0; i < results.size(); i++){ 25 | cout << '"' << results[i] << '"' << endl; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /any/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost; 8 | 9 | int main(int argc, char* argv[]){ 10 | vector v; 11 | v.push_back(15); 12 | v.push_back(string("test")); 13 | v.push_back((char*)"hello"); 14 | v.push_back(3.1415926535897932); 15 | 16 | for(int i=0; i < v.size(); i++){ 17 | any a = v[i]; 18 | if(a.type() == typeid(int)) cout << "int: " << any_cast(a) << endl; 19 | else if(a.type() == typeid(double)) cout << "double: " << any_cast(a) << endl; 20 | else if(a.type() == typeid(string)) cout << "string: " << any_cast(a) << endl; 21 | else if(a.type() == typeid(char*)) cout << "char*: " << any_cast(a) << endl; 22 | cout << a.type().name() << endl; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /program_options/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace boost; 8 | 9 | int main(int argc, char* argv[]){ 10 | program_options::options_description opts("options"); 11 | opts.add_options() 12 | ("help,h", "ヘルプを表示") 13 | ("server,s", program_options::value(), "サーバーのアドレス") 14 | ("port,p", program_options::value(), "サーバーのport番号") 15 | ("fork", program_options::value(), "preforkする数"); 16 | program_options::variables_map argmap; 17 | program_options::store(parse_command_line(argc, argv, opts), argmap); 18 | program_options::notify(argmap); 19 | 20 | if(argmap.count("help") or !argmap.count("server") or !argmap.count("port")){ 21 | cout << opts << endl; 22 | cout << "serverとportが必要です" << endl; 23 | return 1; 24 | } 25 | 26 | cout << "server : " << argmap["server"].as() << endl; 27 | cout << "port : " << argmap["port"].as() << endl; 28 | if(argmap.count("fork")) cout << "fork : " << argmap["fork"].as() << endl; 29 | 30 | return 0; 31 | } 32 | --------------------------------------------------------------------------------