C++_string模板库知识回顾

//无参构造,创建一个空字符串。

string str = string();

//通过一个字符串,构造另外一个字符串

string str2 = string("Hello World!");

//通过一个字符数组,构建一个字符串。

const char* array = "hello world!";
string str3 = string(str2);
string str3 = array; // 等价于string str3 = string(array);
cout << "str3=" << str3 << endl;

//通过指定数量的指定字符,构造一个字符串,在这里结果是"AAAAA"

string str4 = string(5, 'A');
cout << "str4=" << str4 << endl;

/*string的赋值操作
    string& operator = (const char* s);
    string& operator = (const string& s);
    string& operator = (const char c);
    string& assign(const char* s);
    string& assign(const char* s, int n);
    string& assign(const string& s);
    string& assign(const string& s, int start, int n);

    //string存取字符操作
    char& operator[](int n) // 通过[]方式1取字符
    char& at(int n) // 通过at方法获取字符
*/

 //通过等号进行赋值,等号已经被string进行运算符重载。
 //通过字符串进行赋值

str = "hello world!";
cout << "str=" << str << endl; // str=hello world!

//通过字符数组进行赋值

 const char* arr = "abc";
 str = arr;
 cout << "str=" << str << endl; // str=abc

 //通过字符进行赋值

     str = 'a';
	 cout << "str=" << str << endl;

	//assign
	 str.assign("Hello World!");
	 cout << "str=" << str << endl; // str=Hello World!
	   
	 str.assign(arr);
	 cout << "str=" << str << endl; // str=abc
	   
	 str.assign(8, 'a');
	 cout << "str=" << str << endl; // str=aaaaaaaa

	 //str.assign(6, "a"); // error
	 str.assign(arr, 1); // 把字符串arr的前一个单词付给字符串。
	 cout << "str=" << str << endl; // str=a
	 str.assign("laowang", 4);
	 cout << "str=" << str << endl; // str=laow
	   
	 str.assign("wangxiaobo", 4, 10); //把字符串从str[4]到str[10]的字符串赋值给str
	 cout << "str=" << str << endl; // str=xiaobo

// 通过“下标”,从一个字符串获取到指定位的字符,或者是可以修改指定下标位的字符。

    string str = "Hello World!";
	cout << "str[4]=" << str[4] << endl; // str[4]=o
	cout << "str.at[4]=" << str.at(4) << endl; // str.at[4]=o

// 使用字符引用返回值,存储一下字符串中,指定下标位字符的引用。

    char& c = str[0];
	cout << "c=" << c << endl; //c=H

//修改引用的值,因为这里引用的是字符串中的字符数组中的指定下标位的元素,所以这里c发生变更,也会影响到数组中的元素。

    c = '0';
	cout << str << endl; // 0ello World!

// c_str:将C++风格的字符串,转成C风格的字符串。

    cout << (int*)str.c_str() << endl; // 000000E4692FFAC0
    str = "123456789012345"; 
    // str = "1234567890123456", error,字符串中的字符数组内存重新分配。使用之前的空间访问,可能会出现问题。(不能超过16位)
	cout << (int*)str.c_str() << endl; // 00000079C9AFFC90

	c = 'A';
	cout << str << endl; //A23456789012345

// string的拼接

    string str = "hello";
	str += " world"; // += or +
	cout << str << endl; // hello world

// append函数(附加,增补)

    // string& append(const char* s);
	str.append("world"); // 把字符串S连接到当前字符串结尾。
	cout << str << endl; // hello worldworld

	// string& append(const char* s, int n);
	str.append("nihao", 3); // 将字符串前3个字符连接到当前字符串结尾。
	cout << str << endl; // hello worldworldnih

	// string& append(const string& s); 等同于 operator+=()
	// string& append(const string& s, int post, int n);
	str.append(" ");
	str.append("C++ is the best programming language.", 11, 4); // 从字符串第11号位置输出4个字符。
	cout << str << endl; // hello worldworldnih best

	//string& append(int n, char c);
	str.append(5, 'k'); //从字符串尾端输出5个k
	cout << str << endl; // hello worldworldnih bestkkkkk

//string的查找和替换
//查找:查找一个字符串或者是一个字符,在指定的字符串中出现的下标,如果找不到返回-1。
//替换:将一个字符串指定下标的范围的部分,替换成新的字符串。

    string str = "C++ is the most popular, most usful programing language in the world!";
	cout << str << endl;

	//查找:
	//int find(const string& str, int pos = 0) const;
	//查找str第一次出现的位置,从第pos位开始查找。
	//int find(const char* s, int pos = 0) const;
	//查找s第一次出现的位置,从第pos位开始查找。
	//int find(const char* s, int pos, int n) const;
	//从pos位置查找s的前n个字符的第一次位置。
	//int find(const char c, int pos = 0) const;
	//查找字符c第一次出现的位置。

	cout << str.find("most") << endl; // 11(从0位开始)
	cout << str.find("most", 20) << endl; //25(从20位开始查找)
	int res = str.find("mostmost", 0, 4); // 查找mostmost的前4个字符most出现的第一次的位置。
	cout << res << endl; // 11
	cout << str.find('s') << endl; // 5, 从0号位开始查找s字符第一次出现的第一个位置。
	cout << str.find('s', 10) << endl; //13

	//int rfind(const char* s, int pos = npos) const;
	//查找s最后一次出现的位置,从pos开始查找。
	//int rfind(const char* s, int pos, int n) const;
	//从pos开始查找s的前n个字符最后一次出现的位置。
	//int rfind(const char c, int pos = 0) const;
	//查找字符c最后一次出现的位置。
	cout << str.rfind("most") << endl;  // 25
	cout << str.rfind("most", 20) << endl; // 11,从第20位开始查找,从后往前查找。
	cout << str.rfind("mostmost", 20, 4) << endl; // 11
	cout << str.rfind('s') << endl; // 31
	cout << str.rfind('s', 20) << endl; // 13

	//替换:string& replace(int pos, int n, const string& str);
	//替换,从pos开始n个字符为字符串str。
	//string& replace(int pos, int n, const char* s);
	//替换,从pos开始的第n个字符为字符串s。
	cout << str.replace(11, 24, "best") << endl; // C++ is the best programing language in the world!
	cout << str.replace(0, 3, "go") << endl; // go is the best programing language in the world!

//string的比较操作

/*
    字符串的大小比较规则:
    比的是字典顺序;(字符在字符集中映射的数字)
    依次比较字符串中的每一个字符。
    如果字符相同,继续比较后面的一位字符,直到某一次的比较可以分出大小。
    */
    /*
    注意:字符串的比较,仍可用 >, <, >=, <=, ==, != 来比较,有一定的局限性。
    局限性:比较的结果是bool类型,无法充足表示每一种比较结果。
    解决方案:使用compare函数,返回值是一个int类型。
    1、前面的大于后面的。
    -1、前面的小于后面的。
    0、相等。
*/

    string str1 = "abc";
	string str2 = "abe";
	string str3 = "abc";
	cout << (str1 < str2) << endl; // 1, bool类型, 
	cout << (str2 < str1) << endl; // 0, bool类型, 

	cout << str1.compare(str2) << endl; // -1, str1 < str2
	cout << str1.compare(str3) << endl; // 0, str1 = str2

//string子串的获取

    //string subtr(int pos = 0, int n = npos) const;
	//返回由pos(起始下标,默认值为0)开始的n个字符组成的字符串。

    string str = "world";

	cout << str.substr(2, 1) << endl; // r, 产生的是一个新数组,不会对原数组进行修改。
	cout << str.substr(2, 10) << endl; // rld, n的值溢出不会报错,只会把字符串后面的字符输出。

	cout << str << endl;

    //string& insert(int pos, const char* s); // 插入字符串。
    //string& insert(int pos, const string& str); // 插入字符串。
    //string& insert(int pos, int n, char c); // 在指定位置插入n个字符c。
    //string& erase(int pos, int n = npos); // 删除从pos开始的n个字符。

    string str = "SOS SOS SOS";
	str.insert(4, "world "); // 从第4号下标开始插入一个字符串"world ",字符串数组被扩充。
	cout << str << endl; // SOS world SOS SOS

	str.insert(4, 5, 'h'); // 从第4号下标开始插入5个字符'h',字符串数组被扩充。
	cout << str << endl; // SOS hhhhhworld SOS SOS

	//删除
	str.erase(4, 5); // 删除从第4号下标开始的5个字符。
	cout << str << endl; // SOS world SOS SOS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值