C++文字列


C++のstring型について調べるも…
HP-UXのaCCで使えないので活用できない資料群。

C++ 文字列

文字列

文字列にはC言語の文字列charと、C++で定義されているstringクラスがある。

char型文字列は配列と同じ性質を持つ。文字定数のラベルは文字列のアドレスとなり、関数の引数にはポインタを使用する。

例:

#include <stdio.h>
#include <string.h>

out(char *p){
   printf("%s\n",p);
}

main(){
   char *a="JSA";
   out(a);
}

stringクラスを使用する場合は

#include <string>

を指定する。ラベルはオブジェクトであるからchar型のようにポインタに注意する必要がない。

例1:

/*
cc -lstdc++ b.cpp
*/
#include <iostream>
#include <string>

out(string p){
   cout << p << endl;
}

main(){
   string a="JSA";
   out(a);
}

例2:'+'などの演算子が使用できる。

/*
cc -lstdc++ e.cpp
*/

#include <iostream>
#include <string>
using namespace std;

main(){ 
   string s,s1,s2;

   s="文字列";
   cout << s << endl;
   s1="abc";
   cout << s1 << endl;
   s2=s+s1;
   cout << s2 << endl;
}

初期化

文字列の初期化

string s;          //空文字列
string s="";       //空文字列
string s(5, 'x');  // "xxxxx"
string s="hello";  // hello
string s1=s;       // 代入
string s1(s,1,3);  // 部分代入 s[1]とs[3]
string s1(s+1,3);  // 部分代入 s[1]からs[3]まで

文字列長

文字列の長さを返す。size(), length()は同じ。

例:

string s="日本麻酔学会";
cout << s.size() << endl;  // 12を返す。

文字操作

文字列の要素は配列の様に指定できる。

string s;
char c;
s="abc";
c=s[0];       // 'a'が代入される。
s.at(2)='d';  // 'adc'となる。

結合

+演算子で文字列の結合ができる。

string s1,s2,s3;
s1="abc";

C文字列へ

string型からC言語文字列に変換する。

string s="abcd"
s.c_str();

検索

文字列の検索
日本語(euc)全角文字は2バイトで計算される。

例:

/*
cc -lstdc++ a.cpp
*/

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

main(){

//           0123456
   string s="accdcde";
   cout << s.find("cd") << endl;              // 2が返る
   cout << s.rfind("cd") << endl;             // 4が返る
   cout << s.find_first_of("cd") << endl;     // 1が返る
   cout << s.find_last_of("cd") << endl;      // 5が返る
   cout << s.find_first_not_of("cd") << endl; // 0が返る
   cout << s.find_last_not_of("cd") << endl;  // 6が返る

}

サンプルプログラム

その1


#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;
 
void out(string p){
   const char *p2=p.c_str();
   printf("test='%s'\n",p2);
}

void make(string *a){
   char MSG[512];
   sprintf(MSG,"テストです。\n");
   *a = *a + MSG;
   sprintf(MSG,"次の行%d\n",__LINE__);
   *a = *a + MSG;
}

void main(){
   string a="ABC";
   make(&a);
   out(a);
}

その2

  • //-------------------------------------------
  • // string 型使用法いろいろ (ただのメモ)
  • //-------------------------------------------
  • #include
  • #include
  • #include
  • using namespace std;
  • //テスト用メインプログラム
  • int main(){l;
  • //string型からchar型配列へ
  • cout <;
  • cout << " " << endl;
  • //string型からint型へ
  • cout << "string型からint型へ" << endl;
  • str = "123";
  • int it;
  • it = atoi(str.c_str());
  • cout << it << endl;
  • cout << " " << endl;
  • //数字をstring型に
  • cout << "数字をstring型に" << endl;
  • double dbl2 = 246.8;
  • sprintf(chr,"%f",dbl2);
  • str = string(chr);
  • cout << str << endl;
  • cout << str[2] << endl; //stringの3文字目
  • cout << str.find(".")+1 << endl; //"."は何番目?
  • cout << " " << endl;
  • return 0;
  • }

ナビゲーション

プロフィール

Photo Hondarer  My status

自分に正直に、目指す物を目指すかたちで、全ての人が幸せになれるシステムを削り出す職人でありたい。

Powered by
HndWiki 011122