文字列のコンテナ:
文字列の基本的な概念:
エッセンス:
C ++形式の文字列は、文字列で、文字列は、本質的にクラスであります
*文字列と違い文字:
char *ポインタ
文字列クラス、パッケージ内のクラスの文字で、文字列の管理、char型で型容器。
特長:
内部文字列クラスは、多くのメンバーメソッドをカプセル化
たとえば:検索、コピーのコピーを見つけ、削除しますが、置き換える置き換え、挿入、挿入
CHAR *文字列は内部クラスを担当した、割り当てられたメモリを管理するなど、国境を越えたクロスボーダーの値をコピーすることについては心配しないでください
文字列コンストラクタ:
コンストラクタのプロトタイプ:スリー
図1に示すように、文字列(); //は、空の文字列、例えば作成し;列(CONST文字列str * S); // 初期化の列
2、列(CONST文字列&STR ); //はAを使用して文字列オブジェクトを初期化します別の文字列オブジェクト
3、列(INT N、チャー C); //は、 n個の文字cを初期化します
サンプルコード:
#include<iostream>
#include<string>
using namespace std;
/*
构造函数原型:
string(); //创建一个空的字符串 例如: string str; string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化
*/
int main() {
string name1; // 第一种:创建空字符串,调用无构造函数
cout << name1 << endl;
string name2("这是name2"); //第二种方式,初始化另一个string对象
string name3(name2);
cout << name3 << endl;
string name4(3, '6'); //第三种方式:使用n个字符c初始化
cout << name4 << endl;
system("pause");
return 0;
}
文字列の割り当て:
説明:文字列に文字列を代入
関数プロトタイプの割り当て:
図1に示すように、文字列&演算子=(CONST CHAR * S); //チャー現在の文字列に割り当てられたタイプ列
2、列&演算子=(定数ストリング&S); // 文字列sの現在の文字列割り当て
3を、文字列&演算子=(チャーC); //現在の文字列に割り当てられた
。4、割り当て・ストリング(CONSTチャー S); //文字列の現在の文字列割り当てる
5、文字列&割り当て(CONSTのchar * S、N INT ); //文字列sの最初のn文字が現在の文字列に割り当てられている
(CONST文字列6、列&アサイン &S); // 文字列の現在の文字列割り当てる
7、ストリング・アサイン(整数nは 、文字cを); // C割り当てられた現在の文字列のN文字を含みます
サンプルコード:
#include<iostream>
#include<string>
using namespace std;
/*
string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
string& operator=(const string &s); //把字符串s赋给当前的字符串
string& operator=(char c); //字符赋值给当前的字符串
string& assign(const char *s); //把字符串s赋给当前的字符串
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s); //把字符串s赋给当前字符串
string& assign(int n, char c); //用n个字符c赋给当前字符串*/
int main() {
string a = "a"; //第一种
cout << a << endl;
string b = "b"; //第二种
a = b;
cout << a << endl;
b = 'a'; //第三种
cout << b << endl;
a.assign("asd"); //第四种
cout << a << endl;
a.assign("dnf",1); //第五种
cout << a << endl;
a.assign(b); //第六种
cout << a << endl;
a.assign(5, 'a'); //第七种
cout << a << endl;
system("pause");
return 0;
}
文字列CONCATENATE文字列の末尾に実装されました:
まあスプライスカスタムの場所アペンドオーバーロードされた関数への能力は、個人的な感情が便利です退屈します
サンプルコード:
#include<iostream>
#include<string>
using namespace std;
/*
string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
string& operator+=(const string& str); //重载+=操作符
string& append(const char *s); //把字符串s连接到当前字符串结尾
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=(const string& str)
string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾*/
//字符串拼接
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love ");
str3.append("game abcde", 4); //取前四个进行拼接
//str3.append(str2);
str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
cout << "str3 = " << str3 << endl;
}
int main() {
test01();
system("pause");
return 0;
}