All_Header
#pragma once
#define _USE_MATH_DEFINES
#include <complex>
#include <exception>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <limits>
#include <locale>
#include <memory>
#include <new>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#ifdef _MSC_VER
#include <windows.h>
#endif
#ifdef _MSC_VER
#define SLEEP(x) Sleep(1000*x)
#else
#define SLEEP(x) sleep(x)
#endif
#ifdef _MSC_VER
#define DISP_CLEAR system("cls")
#else
#define DISP_CLEAR system("clear")
#endif
using namespace std;
Split
//標準入力から読み込んだ文字を任意の文字で分割する
vector<string> split(char split_char){
string str;
vector<string> res;
ifstream ifs(stdin);
size_t current = 0, found;
getline(ifs, str);
while((found = str.find_first_of(split_char, current)) != string::npos)
{
res.push_back(string(str, current, found - current));
current = found + 1;
}
res.push_back(string(str, current, str.size() - current));
return res;
}
int main(void){
ios::sync_with_stdio(false);
/* start */
vector<string> arr;
cout << "空白区切りで入力:";
arr = split(' ');
for(int i=0;i<arr.size();i++)cout << arr[i] << endl;
cout << "カンマ区切りで入力:";
arr = split(',');
for(int i=0;i<arr.size();i++)cout << arr[i] << endl;
/* end */
return 0;
}
2線分の交点
//p1(a,b) p2(c,d)
//p3(e,f) p4(g,h)
bool _inline crossSegment(double a, double b, double c, double d, double e, double f, double g, double h){
bool foo=false;
double A,B,C;
double x,y;
fprintf(stderr, "( %.2f , %.2f ) ", a, b);
fprintf(stderr, "( %.2f , %.2f ) ", c, d);
fprintf(stderr, "( %.2f , %.2f ) ", e, f);
fprintf(stderr, "( %.2f , %.2f ) => ", g, h);
A = f*g-e*h;
B = b*c-a*d;
C = (d-b)*(g-e)-(c-a)*(h-f);
if(C == 0){
fprintf(stderr, "N/A \n");
return false;
}
x = ( A*(c-a) - B*(g-e) ) / C;//交点のx座標
y = ( A*(d-b) - B*(h-f) ) / C;//交点のy座標
fprintf(stderr, "( %.2f , %.2f )", x, y);
/* ((a<x<c)||(c<x<a)) && ((e<x<g)||(g<x<e)) */
if( ((a <= x && x <= c) || (c <= x && x <= a)) && ((e <= x && x <= g) || (g <= x && x <= e)) ){
foo = true;
}else{
foo = false;
}
foo ? cerr << "cross" << endl : cerr << "not cross" << endl;
cerr << endl;
return foo;
}
実行時間の計測(秒単位)
class MyTime{
struct tm *t;
time_t start, end, tmp;
public:
MyTime();
void MeasurementStart();//計測の開始
void MeasurementEnd();//計測の終了
void PrintRuntime();//標準エラー出力で時間を表示
int getDifftime();//経過秒数をint型で返す
void PrintLocalTime();//現在時刻を表示する
void PrintStartTime();//計測の開始時刻を表示する
void SleepSec(int sec);//sec秒だけ処理を停止
};
MyTime::MyTime(){
start = time(NULL);
end = time(NULL);
tmp = time(NULL);
t = NULL;
};
void MyTime::MeasurementStart(){
time( &start );
};
void MyTime::MeasurementEnd(){
time( &end );
};
void MyTime::PrintRuntime(){
int tmp = (int)difftime(end, start);
int hour, min, sec;
hour = tmp / 3600;
tmp -= hour * 3600;
min = tmp / 60;
sec = tmp % 60;
fprintf(stderr, "%5dH %2dM %2dS", hour, min, sec );
};
int MyTime::getDifftime(){
return (int)difftime(end, start);
};
void MyTime::PrintLocalTime(){
time( &tmp );
t = localtime( &tmp );
fprintf(stderr, "%s", asctime(t) );
}
void MyTime::PrintStartTime(){
t = localtime( &start );
fprintf(stderr, "%s", asctime(t) );
}
void MyTime::SleepSec(int sec){
#ifdef _MSC_VER
Sleep((int)sec * 1000);
#else
sleep((int)sec);
#endif
}
実行時間の計測
#include <cstdio>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
void print_run_time(clock_t start, clock_t end){
cout << "[";
cout << (end - start)/1000/60/60 << "h ";
cout << setw(2) << ((end - start)/1000/60)%60 << "m ";
cout << setw(2) << ((end - start)/1000)%60 << "s] ";
cout << "[" << (double)(end - start)/1000. << "s]" << endl;
}
int main(){
srand((unsigned int)time(NULL));
clock_t c_start,c_end;
c_start = clock();
/* 計測したい処理 */
c_end = clock();
print_run_time(c_start, c_end);
return 0;
}
コンソール表示色の変更
サンプル1
template <class X> void printColor(X out, int flag) {
HANDLE hStdout;
WORD white, red, blue, green, yellow, pink, aqua, tmp;
CONSOLE_SCREEN_BUFFER_INFO csbi;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbi);
white = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
red = FOREGROUND_INTENSITY | FOREGROUND_RED;
green = FOREGROUND_INTENSITY | FOREGROUND_GREEN;
blue = FOREGROUND_INTENSITY | FOREGROUND_BLUE;
yellow = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN;
pink = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE;
aqua = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE;
switch(flag){
case 1:
tmp = white;
break;
case 2:
tmp = red;
break;
case 3:
tmp = blue;
break;
case 4:
tmp = green;
break;
case 5:
tmp = yellow;
break;
default:
tmp = csbi.wAttributes;
}
SetConsoleTextAttribute(hStdout, tmp);
cout << out << '\n';
SetConsoleTextAttribute(hStdout, csbi.wAttributes);
}
サンプル関数(100までの整数をゲージで表示)
#include <windows.h>
#include <stdio.h>
#include <conio.h>
void gauge(int x){
HANDLE hStdout;
WORD white, red, blue, green, yellow, pink, aqua;
CONSOLE_SCREEN_BUFFER_INFO csbi;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbi);
//各色の定義
white = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
red = FOREGROUND_INTENSITY | FOREGROUND_RED;
green = FOREGROUND_INTENSITY | FOREGROUND_GREEN;
blue = FOREGROUND_INTENSITY | FOREGROUND_BLUE;
yellow = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN;
pink = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE;
aqua = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE;
if(x < 0 || 100 < x){
return;
}else{
SetConsoleTextAttribute(hStdout, white);
cout << "|";
SetConsoleTextAttribute(hStdout, blue);
for(int i=0;i<100;i+=2){
if(i > 20)SetConsoleTextAttribute(hStdout, aqua);
if(i > 40)SetConsoleTextAttribute(hStdout, green);
if(i > 60)SetConsoleTextAttribute(hStdout, yellow);
if(i > 80)SetConsoleTextAttribute(hStdout, red);
if(i <= x)cout << ">";
else cout << " ";
}
SetConsoleTextAttribute(hStdout, white);
cout << "|" << endl;
}
//元のテキスト状態に戻す
SetConsoleTextAttribute(hStdout, csbi.wAttributes);
}
cin/coutの高速化
c++で標準入力・出力にはcin/coutを使用することが多いが,この関数は実行速度が速くない.
そのため,main関数の初めにあるフレーズを挿入することで高速化が可能である.
int main(){
ios::sync_with_stdio(false);//main関数の最初に記述
//処理
return 0;
}
最大公約数(GCD)
int gcd(int x, int y)
{
int tmp;
// ユーグリッド互除法
while((tmp = x % y) != 0){
x = y;
y = tmp;
}
return y;
}
int main()
{
int x, y;
int num=0;
while(!num){
cin >> x >> y;
num = gcd(x, y);
}
cout << num << endl;
return 0;
}
素数
MAXまでの素数をあらかじめ,計算しておく関数.(改善の余地あり)
#define MAX 1000000
int main(void){
/* start */
int num=2;
bool check[MAX+2];
vector<int> arr;
while(num < MAX){
if(!check[num]){
int tmp=num;
arr.push_back(num);
while(tmp<MAX){
check[tmp]=true;
tmp+=num;
}
}
num++;
}
//for(i=0;i<arr.size();i++)cout << arr[i] << endl;//素数の表示
cout << arr.size() << endl;//素数の数の表示
/* end */
return 0;
}
階乗・コンビネーション
int kaijou(int n){
if(n == 1)return 1;
else return n*kaijou(n-1);
}
int combination(int n, int m){//nCm
int tmp1,tmp2,tmp3;
tmp1 = kaijou(n);
tmp2 = kaijou(n-m);
tmp3 = kaijou(m);
return tmp1 / tmp2 / tmp3;
}
2次元配列のnewによる動的生成の例
多次元配列をnewによって動的に生成する為に思いつくのは(例1)のような記述の仕方であるが,
そのように記述してもエラーが出てしまうため(例2)のように記述する必要がある.
例1
double **arr;
arr = new double[size_x][size_y];
例2
double **arr = new double*[size_x];
for (int i = 0; i < size_x; i++) {
arr[i] = new double[size_y];
}
//配列の使用
for (int i = 0; i < size_x; i++) {
delete[] arr[i];
}
delete[] arr;
ファイルの検索および検索結果のstringへの格納
#include <vector>
#include <string>
#include <io.h>
#include <iostream>
using namespace std;
void find_file();
vector<string> arr;
int main(){
find_file();
for(int i=0;i<arr.size();i++){
cout << arr[i] << endl;
}
return 0;
}
void find_file()
{
long handle;
struct _finddata_t finddata;
handle = _findfirst("*.txt", &finddata);
if(handle != -1){
arr.push_back(finddata.name);
while(_findnext(handle, &finddata) != -1){
arr.push_back(finddata.name);
}
_findclose(handle);
}
}
日付の取得・表示
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <sstream>
#include <string>
#include <windows.h>
using namespace std;
//int型をstring型に変換
string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}
//日付をstring型で返す
string DateTimeString(void)
{
string strtmp = "";
time_t timer;
struct tm *date;
char ch[256];
/* 時間を取得 */
timer = time(NULL);
/* 構造体 date に変換 */
date = localtime(&timer);
strtmp += IntToString(date->tm_year + 1900) + "_";
if(date->tm_mon + 1 < 10)strtmp += "0" + IntToString(date->tm_mon + 1);
else strtmp += IntToString(date->tm_mon + 1);
if(date->tm_mday < 10)strtmp += "0" + IntToString(date->tm_mday) + "_";
else strtmp += IntToString(date->tm_mday) + "_";
if(date->tm_hour < 10)strtmp += "0" + IntToString(date->tm_hour);
else strtmp += IntToString(date->tm_hour);
if(date->tm_min < 10)strtmp += "0" + IntToString(date->tm_min) + "_";
else strtmp += IntToString(date->tm_min) + "_";
if(date->tm_sec < 10)strtmp += "0" + IntToString(date->tm_sec);
else strtmp += IntToString(date->tm_sec);
return strtmp;
}
int main(){
while(1){
system("cls");
cout << DateTimeString() << endl;
Sleep(1000);
}
return 0;
}
最終更新:2012年12月27日 11:49