★string 関数★


stringはC言語の変数とは違いますので、色々な事が出来ます。

ここではメンバ関数について説明します。


文字数の取得


文字数の取得はC言語では「strlen」という関数を使っていました。

stringでは、↓のようになります。

<sample program cpp007-01>

#include <iostream>
#include <string>

int main()
{
    std::string str;

    std::cin >> str;

    int length = str.length();

    std::cout << "length = " << length << std::endl;

    return 0;
}

<実行結果>

abc
length = 3
続行するには何かキーを押してください・・・

str.length()

stringの実体である「str」の後ろに「.演算子」があり、length関数が呼び出されています。

「.演算子」を使うという事は、構造体と同じような仕組みです。

C言語の構造体と違う事は、メンバ変数の他にメンバ関数というものがあるという事です。

※メンバ関数については、「クラス」のところで詳しく説明します。

stringにはlength関数というメンバ関数が用意されており、それを呼び出す事で文字数が取得出来ます。


文字検索


文字列に含まれる文字や文字列を検索する関数もあります。

<sample program cpp007-02>

#include <iostream>
#include <string>

int main()
{
    std::string str = "Hello World";

    std::string::size_type result;

    result = str.find("ld");

    std::cout << "result = " << result << std::endl;

    result = str.find("ab");

    std::cout << "result = " << result << std::endl;

    if (result == std::string::npos) {
        std::cout << "Not Found" << std::endl;
    }

    return 0;
}

<実行結果>

result = 9
result = 4294967295
Not Found
続行するには何かキーを押してください・・・

str.find("ld")

find関数を使う事で検索処理が出来ます。

最初の実行結果を見ると、

  result = 9

となっています。

先頭の文字の位置を0とすれば、9番目の箇所に「"ld"」があります。

戻り値は、

std::string::size_type

という型で受け取っています。

実際には「符号無し」の変数みたいです。

文字列が発見できなかった時の実行結果は、

  result = 4294967295

となっていますが、この値は、

std::string::npos

という名前で定数化されていますから、if文などで比較することで判定出来ます。


文字列の置換


replace関数を使うと文字列の置換が可能です。

<sample program cpp007-03>

#include <iostream>
#include <string>

int main()
{
    std::string str = "Hello World";

    str.replace(6, 5, "Japan");

    std::cout << "str = " << str << std::endl;

    return 0;
}

<実行結果>

str = Hello Japan
続行するには何かキーを押してください・・・

str.replace(6, 5, "Japan");

これは、6文字目から5文字分を「"Japan"」という文字列に置換する、という意味です。

find関数と組み合わせると、↓のような事も出来ます。

<sample program cpp007-04>

#include <iostream>
#include <string>

int main()
{
    std::string sourceString = "Hello World";

    std::string targetString = "World";

    std::string replaceString = "United States";

    std::string::size_type pos = sourceString.find(targetString);

    if (pos != std::string::npos) {
        sourceString.replace(pos, targetString.length(), replaceString);
    }

    std::cout << "sourceString = " << sourceString << std::endl;

    return 0;
}

<実行結果>

sourceString = Hello United States
続行するには何かキーを押してください・・・

find関数で「"World"」の位置を取得し、その位置から5文字分("World"の文字数)を「"United States"」に置換しました。


C言語の文字列との互換


stringで大事な関数の1つがこれです。

基本的にstringはC言語の文字列を受け取る関数に渡せません。

例えば、printf関数は「char*」を受け取るため、stringは受け取れないのです。

C++なのでprintfを使わなくても良いのですが、WindowsでのプログラムなどC言語の文字列を受け取る関数を使う事は多々あります。

では、サンプルを書きましょう。

<sample program cpp007-05>

#include <cstdio>
#include <string>

int main()
{
    std::string str = "Hello";

    printf("%s\n", str.c_str());

    return 0;
}

<実行結果>

Hello
続行するには何かキーを押してください・・・

str.c_str()

この関数を使う事で、C言語の文字列に変換することが出来ます。


この関数を使うにあたって1つ注意点があります。

printf関数などで表示するには問題無いのですが、scanf関数で入力することは出来ません。

<sample program cpp007-06>

#include <cstdio>
#include <string>

int main()
{
    std::string str;

    scanf("%s", str.c_str());

    printf("%s\n", str.c_str());

    return 0;
}

<コンパイル結果>

warning C4477: 'scanf' : 書式文字列 '%s' には、型 'char *' の引数が必要ですが、可変個引数 1 は型 'const char *' です

コンパイルすると警告が表示されました。

引数が「const char*」になっている、と書いてあります。

「const」が付いていますから書き換え出来ません。

警告ですから一応は実行出来ますので実行してみてください。

短い文だと上手く表示されたりする事もありますが、プログラムが強制的に停止する事もあります。

c_str関数は、読み取り専用として使ってください。


余裕があれば、C言語の文字列で作ったプログラムをstringに書き換えてみてください。


次へ

戻る

目次へ