「MFC CString」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==VC++ Cstring== [VC++][VisualStudio 2008]{{category MFC}} *http://fatalita.sakura.ne.jp/3DLib/Ref/_T/ *http://www.ruche-home.net/?%A5%D7%A5%ED%A5%B0%A5%E9%A5%DF%A5%…」) |
|||
| 1行目: | 1行目: | ||
==VC++ Cstring== | ==VC++ Cstring== | ||
| − | [VC++][VisualStudio 2008]{{category MFC}} | + | [[VC++][VisualStudio 2008]]{{category MFC}} |
*http://fatalita.sakura.ne.jp/3DLib/Ref/_T/ | *http://fatalita.sakura.ne.jp/3DLib/Ref/_T/ | ||
| 27行目: | 27行目: | ||
CString message = s1 + _T("big ") + s2; | CString message = s1 + _T("big ") + s2; | ||
// Message contains "This is a big test". | // Message contains "This is a big test". | ||
| − | ====CString - | + | ====CString -> int 変換==== |
int i = _ttoi(str); | int i = _ttoi(str); | ||
| − | ====int - | + | ====int -> CString 変換==== |
CString str; | CString str; | ||
int i = 999; | int i = 999; | ||
| 44行目: | 44行目: | ||
CString copiedString = fromLiteral; | CString copiedString = fromLiteral; | ||
if (fromLiteral == copiedString ) { | if (fromLiteral == copiedString ) { | ||
| − | pDC- | + | pDC->TextOutW(10, 70, _T("Equal.")); |
} else { | } else { | ||
| − | pDC- | + | pDC->TextOutW(10, 70, _T("Not Equal.")); |
} | } | ||
*CString オブジェクトが同じ文字列を表しているため、"等価" と見なされる | *CString オブジェクトが同じ文字列を表しているため、"等価" と見なされる | ||
| 56行目: | 56行目: | ||
*GetAtの場合、[]を利用することもできる | *GetAtの場合、[]を利用することもできる | ||
// 文字へのアクセス1 | // 文字へのアクセス1 | ||
| − | for( int i=0; i | + | for( int i=0; i<fromLiteral.GetLength(); i++) { |
| − | pDC- | + | pDC->TextOutW(10 + (i * 20), 100, CString(fromLiteral.GetAt(i))); |
} | } | ||
// 文字へのアクセス2 | // 文字へのアクセス2 | ||
| − | for( int i=0; i | + | for( int i=0; i<fromLiteral.GetLength(); i++) { |
| − | pDC- | + | pDC->TextOutW(10 + (i * 20), 130, CString(fromLiteral[i])); |
} | } | ||
// 文字へのアクセス3 | // 文字へのアクセス3 | ||
fromLiteral.SetAt(1, _T('2')); | fromLiteral.SetAt(1, _T('2')); | ||
// fromLiteral[3] = _T('4'); // SetAtの代わりには使えない | // fromLiteral[3] = _T('4'); // SetAtの代わりには使えない | ||
| − | pDC- | + | pDC->TextOutW(10, 160, fromLiteral); |
====書式付==== | ====書式付==== | ||
| 76行目: | 76行目: | ||
out.Format(profile, name, age); | out.Format(profile, name, age); | ||
| − | pDC- | + | pDC->TextOutW(10, 190, out); |
[[File:0786_mfc03_02.jpg]] | [[File:0786_mfc03_02.jpg]] | ||
2020年2月15日 (土) 08:04時点における版
目次
VC++ Cstring
[[VC++][VisualStudio 2008]]テンプレート:Category MFC
- http://fatalita.sakura.ne.jp/3DLib/Ref/_T/
- http://www.ruche-home.net/?%A5%D7%A5%ED%A5%B0%A5%E9%A5%DF%A5%F3%A5%B0%2F%BE%AE%A5%CD%A5%BF%BD%B8%2FUnicode%C2%D0%B1%FE%A5%B3%A1%BC%A5%C7%A5%A3%A5%F3%A5%B0
階層
特徴
- 動的にメモリが割り当てられるため、サイズを気にしなくてよい
- 便利な演算子とメンバー関数
使用法
リテラルから生成
CString fromLiteral = _T("String from Literal.");
代入
- オブジェクトの内容はコピーされる
- 同一の実体を指すことはない
CString fromLiteral = _T("String from Literal.");
CString copiedString = fromLiteral;
結合
CString s1 = _T("This ");
s1 += _T("is a ");
CString s2 = _T("test");
CString message = s1 + _T("big ") + s2;
// Message contains "This is a big test".
CString -> int 変換
int i = _ttoi(str);
int -> CString 変換
CString str; int i = 999; _itot(i, str.GetBuffer(0), 10);
- str.GetBuffer(0) : 文字列バッファの最小サイズ
- 10 : 基数
評価
- サイズが変化する動的なオブジェクトだが、基本的な組み込み型や単純なクラスと同じように評価される
- CString の各オブジェクトは一意の値を持つ
- 文字列へのポインタではなく実際の文字列と見なす必要がある
CString fromLiteral = _T("String from Literal.");
CString copiedString = fromLiteral;
if (fromLiteral == copiedString ) {
pDC->TextOutW(10, 70, _T("Equal."));
} else {
pDC->TextOutW(10, 70, _T("Not Equal."));
}
- CString オブジェクトが同じ文字列を表しているため、"等価" と見なされる
- CString クラスは等価演算子 (==) をオーバーロードして、オブジェクトの値 (内容) に基づいて 2 つの CString を比較する
文字へのアクセス
- GetAt、SetAtを利用してアクセスできる
- GetAtの場合、[]を利用することもできる
// 文字へのアクセス1
for( int i=0; i<fromLiteral.GetLength(); i++) {
pDC->TextOutW(10 + (i * 20), 100, CString(fromLiteral.GetAt(i)));
}
// 文字へのアクセス2
for( int i=0; i<fromLiteral.GetLength(); i++) {
pDC->TextOutW(10 + (i * 20), 130, CString(fromLiteral[i]));
}
// 文字へのアクセス3
fromLiteral.SetAt(1, _T('2'));
// fromLiteral[3] = _T('4'); // SetAtの代わりには使えない
pDC->TextOutW(10, 160, fromLiteral);
書式付
CString name = _T("Yagi");
int age = 36;
CString profile = _T("name:%s, age:%d");
CString out = _T("");
out.Format(profile, name, age);
pDC->TextOutW(10, 190, out);
© 2006 矢木浩人

