zoukankan      html  css  js  c++  java
  • 学习:SLT_string容器

    前言:这个学了感觉没多大用,自己只需要了解就好,忘记了可以参考以下网站的示例

    用途:至少自己在pe解析的时候这个帮助很大!

    参考网站:https://github.com/AnkerLeng/Cpp-0-1-Resource/blob/master/第5阶段-C%2B%2B提高编程资料/提高编程能力资料/讲义/C%2B%2B提高编程.md

    string容器:

    string基本概念:

    本质:

    string是C++风格的字符串,而string本质上是一个类

    string和char * 区别:

    char * 是一个指针
    string 是一个类,类内部封装了char*,管理这个字符串,是一个char * 型的容器。

    特点:

    string 类内部封装了很多成员方法

    例如:查找find,拷贝copy,删除delete 替换replace,插入insert

    string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责


    string构造函数:

    构造函数原型:有三种

    1、string(); //创建一个空的字符串 例如: string str; string(const char* s); //使用字符串s初始化
    2、string(const string& str); //使用一个string对象初始化另一个string对象
    3、string(int n, char 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;
    }
    

    string赋值操作:

    功能描述:给string字符串进行赋值

    赋值的函数原型:

    1、string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
    2、string& operator=(const string &s); //把字符串s赋给当前的字符串
    3、string& operator=(char c); //字符赋值给当前的字符串
    4、string& assign(const char *s); //把字符串s赋给当前的字符串
    5、string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
    6、string& assign(const string &s); //把字符串s赋给当前字符串
    7、string& assign(int n, char c); //用n个字符c赋给当前字符串

    示例代码:

    #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;
    }
    

    实现在字符串末尾拼接字符串:

    好枯燥,个人感觉有用的就是能够自定义位置进行拼接的append重载函数

    示例代码:

    #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;
    }
    

    string查找和替换:

    功能描述:

    查找:查找指定字符串是否存在
    替换:在指定的位置替换字符串

    函数原型有很多,就不一一举例了,挑两个常用的来写

    #include<iostream>
    #include<string>
    
    using namespace std;
    /*
    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第一次出现位置
    int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
    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最后一次出现位置
    string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
    string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s*/
    int main() {
    
    
    	string name = "thisisname";
    	
    	//实现查找操作
    	int pos = name.find("is", 0); //这里注意的两个地方:1、从左往右下标从0开始 2、先找到就哪个 如果没找到返回值为-1
    	cout << pos << endl;
    
    	int poc = name.rfind("is");  // 跟上面一样 唯一不同的是  找最后一个 例如isis 会找后面的is 那么返回值为2
    	cout << poc << endl;
    
    	//替换操作
    	name.replace(1, 2, "1111"); // 从下标一开始的两个字符替换为1111
    	cout << name << endl;
    
    	system("pause");
    	return 0;
    }
    

    string字符串比较:

    示例代码:

    //字符串比较
    void test01()
    {
    
    	string s1 = "hello";
    	string s2 = "aello";
    
    	int ret = s1.compare(s2);
    
    	if (ret == 0) {
    		cout << "s1 等于 s2" << endl;
    	}
    	else if (ret > 0)
    	{
    		cout << "s1 大于 s2" << endl;
    	}
    	else
    	{
    		cout << "s1 小于 s2" << endl;
    	}
    
    }
    
    int main() {
    
    	test01();
    
    	system("pause");
    
    	return 0;
    }
    

    string字符存取:

    string中单个字符存取方式有两种

    1、char& operator[](int n); //通过[]方式取字符
    2、char& at(int n); //通过at方法获取字符

    示例代码:

    void test01()
    {
    	string str = "hello world";
    
    	for (int i = 0; i < str.size(); i++)
    	{
    		cout << str[i] << " ";
    	}
    	cout << endl;
    
    	for (int i = 0; i < str.size(); i++)
    	{
    		cout << str.at(i) << " ";
    	}
    	cout << endl;
    
    
    	//字符修改
    	str[0] = 'x';
    	str.at(1) = 'x';
    	cout << str << endl;
    	
    }
    
    int main() {
    
    	test01();
    
    	system("pause");
    
    	return 0;
    }
    

    string插入和删除:

    示例代码:

    //字符串插入和删除
    void test01()
    {
    	string str = "hello";
    	str.insert(1, "111");
    	cout << str << endl;
    
    	str.erase(1, 3);  //从1号位置开始3个字符
    	cout << str << endl;
    }
    
    int main() {
    
    	test01();
    
    	system("pause");
    
    	return 0;
    }
    

    string子串:

    示例代码:

    //子串
    void test01()
    {
    
    	string str = "abcdefg";
    	string subStr = str.substr(1, 3);
    	cout << "subStr = " << subStr << endl;
    
    	string email = "hello@sina.com";
    	int pos = email.find("@");
    	string username = email.substr(0, pos);
    	cout << "username: " << username << endl;
    
    }
    
    int main() {
    
    	test01();
    
    	system("pause");
    
    	return 0;
    }
    
  • 相关阅读:
    这个 bug 让我更加理解 Spring 单例了
    SpringBoot
    codeblocks笔记
    https://docs.platformio.org/en/latest/boards/index.html
    外部存储的烧写
    嵌入式AI
    python的一些库
    语音芯片及解决方案
    神奇的调试值“DEADBEEF”
    【12月】+我与rt_thread的“江湖恩怨”
  • 原文地址:https://www.cnblogs.com/zpchcbd/p/11950516.html
Copyright © 2011-2022 走看看