[[ゆん]]

[[プログラム班]]
*CからC++へ [#q35d90b2]
個人的なC++のメモ書き。

需要はゼロな気がしますけど。

#contents

**iostream [#s1524b7b]
iostreamはC++の標準的な入出力ライブラリで、C言語のstdio.hにあたるものです。
***利点 [#h5c2ecd2]
-アドレス演算子(&)がいらない。scanfを書くときに&を書き忘れると、プログラムがとんでもない動作をしちゃいます。
-フォーマット文字列を書くことなく変数の型を見て、入出力をしてくれる。scanfやprintf関数を使ったことのある人は経験があると思うのですが、引数に与える文字列と変数が間違っていてコンパイラに怒られることがよくあります(私だけ?)。iostreamを使えばそういった心配がありません。
-演算子をオーバーロードすることによって、自分で定義したクラス型に対する入出力を使用することができる。

***例 [#xde562bc]
とりあえずC版のコードから。
 #include <stdio.h>
 
 int main() {
 	char c;
 	int i;
 	float f;
 
 	printf("c = ");
 	scanf("%c", &c);
 	
 	printf("i = ");
 	scanf("%d", &i);
 
 	printf("f = ");
 	scanf("%f", &f);
 
 	printf("入力された値は\n");
 	printf("c = %c\n", c);
 	printf("i = %d\n", i);
 	printf("f = %f\n", f);
 	printf("です。\n");
 	
 	return 0;
  }
C++版。
 #include <iostream>
 
 using namespace std;
 
 int main() {
 	char c;
 	int i;
 	float f;
 
 	cout << "c = ";
 	cin >> c;
 	
 	cout << "i = ";
 	cin >> i;
 
 	cout << "f = ";
 	cin >> f;
 
 	cout << "入力された値は" << endl;
 	cout << "c = " << c << endl;
 	cout << "i = " << i << endl;
 	cout << "f = " << f << endl;
 	cout << "です。" << endl;
 	
 	return 0;
  }
***解説 [#h4d3ba58]
:#include <iostream>|
標準C++のライブラリをインクルードする時にはC言語のような".h"は必要ありません。
:using namespace std;|
本当はcinやcoutにはstdという名字のようなもの(名前空間)を指定しないといけないのですが、これを書くことによって省略できるようになります。ex.std::cout
:cout << some_value;|
some_valueの値を標準出力へ出力します。printf相当。
:cin >> some_value;|
some_value変数へ標準入力から値を読み取ります。scanf相当。
:cout << endl;|
改行を出力すると共にストリームを空にします。
**多重配列の動的確保 [#d3973260]
大きさが実行時まで未定の二次元以上の配列を利用する方法。

newを用いる方法は解放が面倒です。
***newを用いる [#uffba6f1]
例としてint型の配列をnew演算子を用いて作成します。
widthとheightに二つの次元の配列の大きさが代入されているものとします。
 int main()
 {
 	int width = 5;
 	int height = 10;
 //配列の確保
 	int** array1 = new int*[width];
 	for(int i = 0; i < width; ++i)
 	{
 		array1[i] = new int[height];
 	}
 
 //処理
 	Some();
 
 //解放
 	for(int i = 0; i < width; ++i)
 	{
 		delete[] array1[i];
 	}
 	delete[] array1;
 }
***vectorを用いる [#b6517889]
 int main()
 {
 	int width = 5;
 	int height = 10;
 //配列の確保
 	std::vector< std::vector<int> > array1(width);
 	for(int i = 0; i < width; ++i)
 	{
 		array1[i].resize(height);
 	}
 
 //処理
 	Some();
***boost::multi_arrayを用いる [#q337772d]
 int main()
 {
 	int width = 5;
 	int height = 10;
 //配列の確保
 	boost::multi_array<int, 2> array1(boost::extents[width][height]);
 
 //処理
 	Some();
**配列や構造体のゼロ初期化 [#ff48c9a3]
POD(Plain Old Data)に限ります。
 int array[10] = {};
 struct some_type{int INT; char CHAR; double DOUBLE;} v = {};
memsetするのとどっちがいいのだろうか。
**C++形式のキャスト [#ecf6dc63]
C++ではC形式のキャストは非推奨となり、新たに四種類のキャストが加えられています。
***static_cast<T> [#q5c31d2d]
暗黙の変換、またはその逆変換。基本的にこのキャストを使う。
***dynamic_cast<T> [#sfe8398d]
ベースクラスを指すポインタから派生クラスを指すポインタを安全に得ることができる。ただしベースクラスが仮想関数を持つことが必要。
***reinterpret_cast<T> [#h57eee88]
どんな型へもキャストできる。実装依存。
***const_cast<T> [#b223625d]
constやvolatileを取り除く。
**Technical Report 1 [#u594a2b8]
C++の新しい拡張機能。
-http://ja.wikipedia.org/wiki/Technical_Report_1
スマートポインタやハッシュテーブル、正規表現、擬似乱数などが追加されている。
名前空間はstd::tr1。
なお個人的には[[Boostライブラリ:http://www.kmonos.net/alang/boost/]]を加えて使用することを勧めます。
***例 [#vf95e1ca]
以下shared_ptrの利用例。ポインタがスコープから外れて破棄されるときに、ポインタの中身に対してもdeleteが呼ばれる。
 #include <iostream>
 #include <memory>
 
 class Destructable
 {
 public:
	Destructable(){}
	~Destructable()
	{
		std::cout << "Object is Destructed." << std::endl;
	}
 };
 
 int main()
 {
	std::tr1::shared_ptr<Destructable> smart_ptr(new Destructable());
 
	return 0;
 }
**C++で2進数値を記述 [#y8fa916a]
templateを利用してリテラルとして2進数値を記述する方法。面白い。
-http://d.hatena.ne.jp/h0shu/20071011/p1
**開発環境 [#z5a17622]
***Eclipse - CDT [#jda74ed4]
-http://monoist.atmarkit.co.jp/fembedded/articles/eclipseccplusplus/02/eclipseccplusplus02a.html
**C++/CLI [#l904c3a0]
***Visual Studioの設定 [#g48088d5]
プロジェクトのプロパティから共通言語ランタイムのサポートをするように変更。
参照設定を追加する。
**C++0x [#b2b2d1cd]
VS2010で自動型推論とかが使えるらしい.
-http://ja.wikipedia.org/wiki/C%2B%2B0x

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS