#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/thread.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
class Someone
{
public :
Someone()
{
cout << "コンストラクタ" << endl;
}
~Someone()
{
cout << "デストラクタ" << endl;
}
void Update()
{
cout << "アップデート" << endl;
}
};
static void func1()
{
string message = "This is a pen";
vector<string> v;
boost::algorithm::split(v, message, boost::algorithm::is_space());
cout << boost::algorithm::join(v, "-") << endl;
}
static void func2()
{
string str1 = "this is a pen";
typedef boost::tokenizer<> tokenizer1;
tokenizer1 tok1(str1);
for (tokenizer1::iterator it = tok1.begin(); it != tok1.end(); ++it)
cout << "TOKEN: " << *it << endl;
string str2 = "20020903";
const int offsets[] = { 4, 2, 2 };
boost::offset_separator ofs(offsets, offsets + 3);
typedef boost::tokenizer<boost::offset_separator> tokenizer2;
tokenizer2 tok2(str2, ofs);
for (tokenizer2::iterator it = tok2.begin(); it != tok2.end(); ++it)
cout << "TOKEN: " << *it << endl;
}
static void file_controle()
{
namespace fs = boost::filesystem;
fs::path dir("my_dir");
fs::create_directory(dir);
fs::ofstream f(dir / "test.txt");
f << "Hello!" << endl;
f.close();
fs::directory_iterator end;
for (fs::directory_iterator it(fs::current_path()); it != end; ++it)
{
if (fs::is_directory(*it))
cout << "D ";
else
cout << "F ";
cout << it->path() << endl;
}
}
class urger
{
typedef boost::mutex::scoped_lock lock;
volatile bool end_flag;
boost::mutex ef_guard;
boost::condition_variable_any exitRequest;
public:
urger() : end_flag(false) {}
void u_main()
{
for (;;)
{
lock lk(ef_guard);
if (end_flag) break;
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC_);
xt.sec += 5;
if (exitRequest.timed_wait(lk, xt)) break;
else cout << "\n何か書くのじゃー!: " << flush;
}
}
void exit()
{
lock lk(ef_guard);
end_flag = true;
exitRequest.notify_one();
}
};
static void thread_test()
{
urger u;
boost::thread thr(&urger::u_main, &u);
cout << "何か書いてね: ";
string str;
getline(cin, str);
u.exit();
thr.join();
}
static void xml_read()
{
shared_ptr<Someone> ptr(new Someone);
shared_ptr<Someone> ptr2 = make_shared<Someone>();
const string xml_file_name = "./conf.xml";
const boost::filesystem::path xml_path = xml_file_name;
boost::system::error_code error;
const bool result = boost::filesystem::exists(xml_path, error);
if (!result || error) {
std::cout << xml_file_name << " ファイルがない" << std::endl;
return;
}
boost::property_tree::ptree xml_root;
read_xml(xml_file_name, xml_root);
boost::optional<std::string> str = xml_root.get_optional<std::string>("conf.gmm_history");
int param = boost::lexical_cast<int>(str.get());
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, xml_root.get_child("conf.values")) {
const int value = boost::lexical_cast<int>(child.second.data());
std::cout << value << std::endl;
}
if (boost::optional<int> id = xml_root.get_optional<int>("conf.data.<xmlattr>.id")) {
std::cout << id.get() << std::endl;
}
if (boost::optional<string> name = xml_root.get_optional<string>("conf.data.<xmlattr>.name")) {
std::cout << name.get() << std::endl;
}
}
static void func_random()
{
mt19937 rand;
for (int i = 0; i < 10; i++)
{
cout << rand() << '\n';
}
uniform_int_distribution<int> dist(1, 6);
for (int i = 0; i < 10; i++)
{
cout << dist(rand) << '\n';
}
uniform_real_distribution<double> dist_real(-100.0, 100.0);
for (int i = 0; i < 10; i++)
{
cout << dist_real(rand) << '\n';
}
}
static void func_auto()
{
auto x = 5;
auto y = 3.5f;
auto z = sin(3.14);
}
static void func_loop_range()
{
int scores[5] = { 10, 20, 30, 40, 50 };
for (const auto score : scores)
{
cout << score << '\n';
}
vector<int> v = { 1, 2, 3 };
list<double> li = { 5.0, 6.0, 7.0, };
string str = "testtest";
for (auto n:v)
{
cout << n << endl;
}
for (auto x : li)
{
cout << x << endl;
}
for (auto ch : str)
{
cout << ch << endl;
}
Someone obj1, obj2;
vector<Someone> some_vec;
some_vec.push_back(obj1);
some_vec.push_back(obj2);
for (auto& o : some_vec)
o.Update();
}
static void func_ramda()
{
vector<int> v = { 0, 1, 2, 3, 4, 5 };
size_t x = count_if(v.begin(),
v.end(),
[](int n){ return n % 2 == 0; }
);
auto Square = [](int n){return n*n; };
cout << Square(9) << endl;
}
static void func_class_enum()
{
enum class Team {RED,WHITE};
}
static void func_array()
{
array<int, 100> ar;
for (int i = 0; i < ar.size(); i++)
{
ar[i] = i;
}
array<int, 5> a = { 1, 2, 3, 4, 5 };
array<int, 5> b;
b.fill(100);
a = b;
a[0] = 0;
for (auto n : a)
{
cout << n << endl;
}
}
static void func_some_algo()
{
const int a = 5, b = 10,c = 2;
const int small = min({ a, b, c });
const int large = max({ a, b, c });
array<int, 100> arr;
for (unsigned int i = 0; i < arr.size(); i++)
{
arr.at(i) = i;
}
const bool chk = all_of(arr.begin(), arr.end(), [](int n){return n <= 500; });
const auto start = chrono::system_clock::now();
for (int i = 0; i < 100000;i++)
{
int a = 0;
a++;
}
const auto end = chrono::system_clock::now();
const chrono::duration<double> elapsed = end - start;
cout << "経過時間 :" << elapsed.count() << "秒" << endl;
const chrono::minutes min(5);
const chrono::seconds sec(15);
const auto result = min + sec;
}
void main()
{
xml_read();
func_random();
func_loop_range();
func_ramda();
func_array();
func_some_algo();
namespace fs = boost::filesystem;
fs::directory_iterator end;
boost::filesystem::path fs_path = fs::current_path();
fs_path += "\\img";
vector<boost::filesystem::path> file_name_vector;
for (fs::directory_iterator it(fs_path); it != end; ++it)
{
if (!fs::is_directory(*it))
{
cout << it->path() << endl;
file_name_vector.push_back(it->path());
}
}
}