bar_1

contents_map

2008年4月14日月曜日

g++: GNU C++ ライブラリ パートV チャプター 13. 文字列クラスString Classes

チャプター 13. 文字列クラスString Classes

Table of Contents

シンプルな変換Simple Transformations
ケース・センシティビティCase Sensivitity
任意の文字型Arbitrary Character Types
トークン化するTokenizing
縮めてフィットShrink to Fit
CString (MFC)

シンプルな変換Simple Transformations

Here are Standard, simple, and portable ways to perform common transformations on a string instance, such as "convert to all upper case." The word transformations is especially apt, because the standard template function transform<> is used.
ここに string のインスタンスにおける、標準的でシンプルで、かつポータブルな共通の変換を実施するための方法を、示します。たとえば、「すべて大文字に変換する」といったものです。 単語の変換は、とくに適してます。なぜなら、標準テンプレート関数 transform<> が、使用されるからです。

This code will go through some iterations. Here's a simiple version:
これは、イテレーションをいくつか使用したコードです。シンプルなバージョンです:

#include <string>
#include <algorithm>
#include <cctype> // 古い <ctype.h>

struct ToLower
{
char operator() (char c) const { return std::tolower(c); }
};

struct ToUpper
{
char operator() (char c) const { return std::toupper(c); }
};

int main()
{
std::string s ("Some Kind Of Initial Input Goes Here");

// すべてを大文字に変える
std::transform (s.begin(), s.end(), s.begin(), ToUpper());

// すべてを小文字に変える
std::transform (s.begin(), s.end(), s.begin(), ToLower());

// すべてを大文字に変えるが、変換結果を
// 別の文字列変数に格納
std::string capital_s;
capital_s.resize(s.size());
std::transform (s.begin(), s.end(), capital_s.begin(), ToUpper());
}


Note that these calls all involve the global C locale through the use of the C functions toupper/tolower. This is absolutely guaranteed to work -- but only if the string contains only characters from the basic source character set, and there are only 96 of those. Which means that not even all English text can be represented (certain British spellings, proper names, and so forth). So, if all your input forevermore consists of only those 96 characters (hahahahahaha), then you're done.
気をつけること は、これらの呼び出しはすべてグローバル Cロケールを含むことだ。これは、C関数の toupper/tolower の使用していることによる。 これは絶対的に動くとみなされるが、-- 文字列を構成する文字がベーシック・ソース・文字セット のみ であり、かつそれらの内の96種類の文字 のみ からなる場合 だけ、である。つまり、すべての英語テキストが表示できるわけではない (正式なブリティシュ・スペルや固有名詞など)。 だからもし入力文字列が、かならず96種類の文字だけからなるのであれば、変換される。

Note that the ToUpper and ToLower function objects are needed because toupper and tolower are overloaded names (declared in <cctype> and <locale>) so the template-arguments for transform<> cannot be deduced, as explained in this message. At minimum, you can write short wrappers like
また他に 気をつけることは、 ToUpper と ToLower の関数オブジェクトが必要だということ。なぜなら、 toupper と tolower は、オーバー・ロードされた名前だ( <cctype>と<locale> に宣言)。だから このメッセージ で説明されているように、 transform<> へのテンプレート引数は省略できない。 最小限では、下記のような短いラッパーが書ける

char toLower (char c)
{
return std::tolower(c);
}

The correct method is to use a facet for a particular locale and call its conversion functions. These are discussed more in Chapter 22; the specific part is Correct Transformations, which shows the final version of this code. (Thanks to James Kanze for assistance and suggestions on all of this.)
正しいメソッドは、特定のロケールのファセットを使い、そのロケールで決められた変換関数を使うことだ。この問題は、チャプター22でより詳細に議論される; パートの 正しい変換 に、このコードの最終版を示す。(これに助言と提案をしてくれたJames Kanzeに感謝。)

Another common operation is trimming off excess whitespace. Much like transformations, this task is trivial with the use of string's find family. These examples are broken into multiple statements for readability:
そのほかの一般にある操作は、不必要なホワイト・スペースのトリミングだ。変換のとき以上に、このタスクはトリビアである。stringの find ファミリを使用する。以下の例では、可読性を考慮して複数のステートメントに分割している。

std::string str (" \t blah blah blah \n ");

// 先頭のホワイト・スペースをトリム
string::size_type notwhite = str.find_first_not_of(" \t\n");
str.erase(0,notwhite);

// 末尾のホワイト・スペースをトリム
notwhite = str.find_last_not_of(" \t\n");
str.erase(notwhite+1);

Obviously, the calls to find could be inserted directly into the calls to erase, in case your compiler does not optimize named temporaries out of existence.
明らかに、コンパイラが存在しない名前つき一時変数を最適化しなければ、 find の呼び出しは、 eraseの呼び出しへと、直接的に挿入できる。


Prev Up Next
パート V. 文字列Strings Home ケース・センシティビティ


0 件のコメント:

コメントを投稿

何かありましたら、どうぞ: