C++
2008年1月30日
Suns & Moon Laboratory
はじめに
C++の勉強中です
テンプレートを使った関数
template <class T> double GET_PIXELS(IkImage &Image,const int sx,const int sy,const int w,const int h){
double result = 0;
int line_bytes = Image.get_line_bytes(Image.Width(),Image.BitsPerPixel());
for(int y=sy;y<(sy+h);y++){
T *ptr;
ptr = (T *)(Image.PixelData + (sx * sizeof(T)) + (line_bytes * y));
for(int xc=0;xc<w;xc++){
result += *ptr;
ptr++;
}
}
return result / (w * h);
}
void IkImageManiGetPixels::Execute(IkImage &Image,const int sx,const int sy,const int w,const int h)
{
if(Image.BitsPerPixel() == 8){
Color = GET_PIXELS<BYTE>(Image,sx,sy,w,h);
}else if(Image.BitsPerPixel() == 16){
Color = GET_PIXELS<WORD>(Image,sx,sy,w,h);
}else{
throw "Error! not supported pixel format";
}
}
無名の名前空間
参考:msdn→Deep C++ 無名の名前空間
文字列と戻り値
参考:どう書く?C++
例外
#include <iostream>
using namespace std;
void func1(void)
{
cout << "func1 start" << endl;
try{
throw "throw func1\n";
}
catch(...){
cout << "func1 catch" << endl;
throw;//ここで例外を再生成すれば、mainでも例外を受け取れる。
}
cout << "func1 end" << endl;//例外再生成すると、ここにはこない
}
void main(int argc,char *argv[])
{
try{
cout << "main start" << endl;
func1();
cout << "main called func1" << endl;//例外によってここには来ない
}
catch(...){
cout << "main catch" << endl;
}
cout << "main end" << endl;//catchした後に、ここは実行される
}
string
文字列の追加はappend()か+=を使う。
string::erase
src = "0123456789";
src.erase(0,3);
cout << "erase(0,3) " << src << endl;//No.0から3個削除 -> 3456789
src = "0123456789";
src.erase(1,3);
cout << "erase(1,3) " << src << endl;//No.1から3個削除 -> 0456789
src = "0123456789";
src.erase(2,5);
cout << "erase(2,5) " << src << endl;//No.2から5個削除 -> 01789
src = "0123456789";
src.erase(3);
cout << "erase(3) " << src << endl;//No.3以降を削除 -> 012
vector
バッファとしての使用
結論を先に言うと、メモリ上の配置が連続する事は、仕様で規定されている...そうです。
ネットでそういう裏付けをさがしていたのですが、見つからなくて困っていました。
その疑問の答えが下記リンクにあります。(教えてくれてありがとう>某氏)
参考:テンポラリ・バッファとしての std::vector の利用
基本
insertよりもpush_backを使う。insertはメモリーの再確保を行うので遅い
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector<int> arr;
// 要素追加
for(int i = 0; i < 10; ++i )
arr.push_back( i );
cout << "配列の添え字でアクセス" << endl;
for(unsigned int i = 0; i < arr.size(); ++i )
cout << arr[i] << endl;
cout << "イテレータでアクセス" << endl;
for(vector<int>::iterator it = arr.begin();it != arr.end();it++)
cout << *it << endl;
return 0;
}
イテレータで移動
vector<double> v;
v.resize(10);
vector<double>::iterator it = v.begin();
advance(v,5);
コンテナの初期化
fill(),fill_n()を使う。
vector<double> v;
v.resize(100);
fill(v.begin(),v.end(),0);//すべて0にする
map
#include <map>
#include <string>
using namespace std;
map<string,string> data;
data.insert(map<string,string>::value_type("square","enix"));
data.insert(map<string,string>::value_type("bandai","namco"));
data.insert(map<string,string>::value_type("sega","summy"));
//要素数
cout << "要素数 = " << (unsigned int)data.size() << endl;
//全要素出力
map<string,string>::const_iterator it = data.begin();
while(it != data.end()){
cout << "key=" << it->first << " val=" << it->second << endl;
it++;
}
//keyで検索
{
map<string,string>::const_iterator it = data.find("square");
if (it != data.end()){//成功失敗はendと比較
cout << it->second.c_str() << endl;
}
}
//全て削除
data.clear();
//空?
if(data.empty()){
cout << "空です。" << endl;
}
出力はこう
要素数 = 3
key=bandai val=namco
key=sega val=summy
key=square val=enix
enix
空です。
ifstream
#include <iostream>
#include <fstream>
int main(int argc,char *argv[])
{
using namespace std;
ifstream ifs(argv[1],ios::in | ios::binary);
while(!ifs.eof()){
char buf[256];
ifs.getline(buf,sizeof(buf),'\n');
cout << "> " << buf << endl;
}
return 0;
}
seekg
seekg(移動量,std::ios::beg);
参考
●C++編(標準ライブラリ) トップページ
書籍:C++ Coding Standards
2008-05-13 13:50:50 32400