program

『プログラミング作法』まとめ16

頻繁に使われる値をキャッシュしよう キャッシュされた値は計算し直す必要がない。 キャッシングは、局所性(プログラムや人間が最近アクセスされた項目や手近にある項目を再利用したがる傾向のこと)を利用している。ハードウェアにもキャッシュがふんだん…

UTF-8のBOMなしでファイル出力

BOOL UTFout(void) { FILE *fp; long position; if (_tfopen_s(&fp, _T("file.txt"), _T("wt, ccs=UTF-8"))) { error(_T("リンクページの作成に失敗しました。")); return FALSE; } // ファイル開始位置を最初にもって行きBOMを消去 // position = ftell(fp)…

WritePrivateProfileSection()

プライベートなiniファイルを書き込む際に、"Key=値"の文字列の終端には"\0\0"である必要があるそうです。 TCHAR crntPath[1024]; // カレントパス+iniファイルのパス名 BOOL SaveIniFile(void) { TCHAR strBuf[NUM_MAXBUF]; int length; // 『Key=値の末端…

ResEditでの読み込みエラー

Resource.hファイル内にをインクルードする必要があります。

プログラムに参考になります。

http://docs.codegear.com/docs/radstudio/radstudio2007/RS2007_helpupdates/HUpdate4/JA/html/devwin32/contents.html

Google C++スタイルガイド 日本語版

日本語に訳された奇特なお方がおられる。 textdrop.net - このウェブサイトは販売用です! - リソースおよび情報

『プログラミング作法』 第4章

/* csv.c: csvライブラリ本体 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "csv.h" enum { NOMEM = -2 }; /* メモリ不足のシグナル */ /* 独立csvフィールド構造体 */ typedef struct csv_field { int csv_num; /* csv独立ナンバー */ char *line; /* 入力文</string.h></stdlib.h></stdio.h>…

『プログラミング作法』 デバッグ

デバッガ ・我々の個人的な手法で言えば、我々がデバッガを使うのは、せいぜいスタックとレースを実行したり変数の値を1~2個表示させたりするときぐらいにすぎない。 ・プログラムをステップ実行するよりも、もっと真剣に考えたり、重要な部分に出力文や自動…

検索文字列置換

/*----------------------------------------------------------- 心霊研究所引用テキスト変換プログラム -----------------------------------------------------------*/ #include <stdio.h> #include <string.h> #include <stddef.h> #define FOUND 0 #define GYOUMAX 500 #define TIKA</stddef.h></string.h></stdio.h>…

分割コンパイルの仕方

すべてのクラスの宣言部分を「クラス名.h」のファイルに入れる すべてのクラスの定義部分を「クラス名.cpp」のファイルに入れる クラスの定義部分の先頭に「#include "クラス名.h"」を入れる クラスを使うソース・ファイルの先頭に「#include "クラス名.h"」…

クラス宣言でインライン関数を使ったプログラム

#include <iostream> class ITest{ private: int a; public: int get() {return a;} void set(int aa) {a = aa;} }; int main() { ITest t; t.set(15); std::cout << t.get(); return 0; }inline 関数名(引数) {内容} クラスのメンバー関数をインライン関数として定義</iostream>…

クラスのメンバー関数にconstを指定したプログラム

#include <iostream> class CTest{ private: int a; public: int get() const; void set(int aa); }; int CTest::get() const { return a; } void CTest::set(int aa) { a = aa; } int main() { CTest c; c.set(7); std::cout << c.get(); return 0; }constのついたメ</iostream>…

関数の仮引数にconstを使ったプログラム

#include <iostream> void mul2(int * const a) { *a *= 2; } int main() { int x = 3; int *px = &x; mul2(px); std::cout << x; return 0; }わざわざconstを使うことで、この関数内でポインタの値が変更されない(できない)ことを、関数のユーザーやコンパイラに伝え</iostream>…

STLのvectorクラスを利用して、リスト構造のデータを処理する

#include <iostream> #include <vector> using namespace std; int main() { int d; vector<int> v; //メインループ while(1){ cout << "数値を入力してください(0で終了): "; cin >> d; if(d == 0) break; //リストにデータを追加 v.push_back(d); //リストにのデータを表示 cout <</int></vector></iostream>…

クラスにもテンプレートをつかう

template<class T> class Point { public: Point(); Point(T sx, T sy); T x, y; }; template<class T> Point<T>::Point() { x = 0; y = 0; } template<class T> Point<T>::Point(T sx, T sy) { x = sx; y = sy; }</t></class></t></class></class>

テンプレートを使う

template<class T> T minimum(T a, T b) { return (a < b) ? a : b; }</class>

(条件) ? A : B について

int minimum(int a, int b) { return (a < b> ? a : b; } ? より前の条件 (a

例外の使い方

#include <iostream> using namespace std; const int ERROR = 1; double divide(double a, double b) { if(b == 0) throw ERROR; return a / b; } int main() { double a, b, c; cout << "空白で区切って数を2つ入力してください: "; cin >> a >> b; try{ c = divide(</iostream>…

Pointクラスで+演算(二項)を使えるようにしたプログラム

#include <iostream> class Point2{ public: Point2(int ax = 0, int ay = 0); Point2 operator+(Point2 &a); int x, y; }; Point2::Point2(int ax, int ay) { x = ax; y = ay; } Point2 Point2::operator+(Point2 &a) { return Point2(x + a.x, y + a.y); } std::ostr</iostream>…

演算子をクラスで再定義する

#include <iostream> class IncChar { public: IncChar(char ac = 'a'); IncChar &operator++(); char c; }; IncChar::IncChar(char ac) { c = ac; } IncChar &IncChar::operator++() { c++; if(c > 'z') c = 'a'; return *this; } int main() { IncChar incchar('t');</iostream>…

リファレンス(参照)の動作を確認するプログラム

#include <iostream> int c = 0; void inc(int &x) { x++; } int &ref_c() { return c; } int main() { int a = 3; int &ra = a; std::cout << "a = " << a << "\n"; ra = 15; std::cout << "a = " << a << "\n"; int b = 4; std::cout << "b = " << b << "\n"; inc(b)</iostream>…

多様性(ポリモーフィズム)実現する仮想関数

#include <iostream> #include "point.cpp" #include "canvas.cpp" #include "shape.cpp" #include "rect.cpp" #include "triangle.cpp" #include "square.cpp" int main() { Canvas c; Shape *shape[3]; Rect r(5, 5, 20, 10); shape[0] = &r; Triangle t(1, 18, 16, </iostream>…

派生派クラス型ポインタを基底クラス型ポインタに変換するプログラム

#include <iostream> class Base{ public: void showName(); }; void Base::showName() { std::cout << "I am Base.\n"; } class Derived : public Base{ public: void showName(); }; void Derived::showName() { std::cout << "I am Derived.\n"; } int main() { Bas</iostream>…

同名のメンバー関数がある時は、派生クラスのメンバー関数が優先されることを示すプログラム

#include <iostream> class Base{ public: void showName(); }; void Base::showName() { std::cout << "I am Base.\n"; } class Derived : public Base{ public: void showName(); }; void Derived::showName() { std::cout << "I am Derived.\n"; } int main() { Der</iostream>…

マインスイーパーゲーム

#include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; //-----------地雷クラス------------------- //地雷を表現するクラス class Mine { int x, y; static bool map[5][5]; public: enum MSTAT{MISS, NEAR, HIT}; Mine(); MSTAT check(int cx, i</string></ctime></cstdlib></iostream>…

enum/列挙型の例

#include <stdio.h> enum TYUMON {OMUSUBI, ODEN, SABANONITUKE, YASAIITAME}; int main() { enum TYUMON tyu1 = ODEN; printf("%d\n", tyu1); return 0; }実行結果 1</stdio.h>

クラスの例

#include <iostream> using namespace std; class Color { private: int r, g, b; public: void setRGB(int ar, int ag, int ab); void getRGB(int* ar, int* ag, int* ab); }; void Color::setRGB(int ar, int ag, int ab) { r = ar; g = ag; b = ab; } void Color::g</iostream>…

名前空間の指定

a#include <iostream> int main() { std::cout << "Hello, Hello, This is the first program" "in C++.\n"; return 0; }#include <iostream> using namespace std; int main() { cout << "Hello, Hello, This is the first program" "in C++.\n"; return 0; }</iostream></iostream>

Cの構造体とC++のクラスの違い

#include <stdio.h> /* i時刻を格納する構造体 */ typedef struct { int hour; int minute; int second; } Time; /*時刻をセットする関数*/ void Time_set(Time *t, int h, int m, int s) { t->hour = h; t->minute = m; t->second = s; } /*時刻を進める関数*/ void </stdio.h>…

関数の多重定義

#include <iostream.h> void printtype(int num) { cout << num << "はintです。\n"; } void printtype(double num) { cout << num << "はdoubleです。\n"; } int main() { int a = 3; double b = 5.5; printtype(a); printtype(b); return 0; }実行結果 3はintです。 5.</iostream.h>…