C++练习笔记STL之string容器

STL(Standard Template Library)标准模板库

STL从广义上分为容器(Container)、算法(algorithm)、迭代器(iterator)
容器和算法之间通过迭代器进行无缝连接
STL几乎所有的代码都采用了模板类或者模板函数

STL六大组件:容器、算法、迭代器、仿函数、适配器、空间配置器

容器:各种数据结构如verctor、list、deque、set、map等用来存放数据
序列式容器:强调值的排序,序列式容器的每个元素均有固定的位置
关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系

算法:各种常用的算法如sort、find、copy、for_each等
质变算法:是指运算过程中会更改区间内的元素的内容。如拷贝,替换,删除等
非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找,计数,遍历,寻找极值等等

迭代器:扮演了容器与算法之间的胶合剂
常用的迭代器有双向迭代器和随机访问迭代器
种类 功能 支持运算
输入迭代器 对数据的只读访问 只读,支持++ == !=
输出迭代器 对数据的只写访问 只写支持++
前向迭代器 读写操作,并能向前推进迭代器 读写,支持++ == !=
双向迭代器 读写操作,并能向前和向后操作 读写,++ –
随机访问迭代器 读写操作,可以以跳跃方式访问任意数据
功能最强的迭代器 读写支持++ – 【n】 -n < <= > >=

仿函数:行为类似函数,可作为算法的某种策略
适配器:一种用来修饰容器或者仿函数或迭代器接口的东西
空间配置器:负责空间的配置与管理

string字符串容器

char 是一个指针
string是一个类,类内部封装了char
,管理这个字符串,是一个char型的容器
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char
所分配的内存,不用担心复制越界和取值越界等,由类内部负责

构造函数原型:
string() //创建一个空的字符串
string(const char* s); //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化

void test01()
{
    string s1; //默认构造

    const char * str = "hello world";
    string s2(str);

    cout << "s2:" << s2 << endl;

    string s3(s2);
    cout << "s3:" << s3 << endl;

    string s4(10, 'a');
    cout << "s4:" << s4 << endl;
        
}

int main()
{
    test01();
    std::cout << "Hello World!\n";
}

运行结果:
s2:hello world
s3:hello world
s4:aaaaaaaaaa
Hello World!

string赋值操作

函数原型:
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赋给当前字符串

示例代码

#include <iostream>
#include<string>
using namespace std;
void test201()
{
	string str1;
	str1 = "hello world";
	cout << "str1=" << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2=" << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3=" << str3 << endl;
	
	string str4;
	str4.assign("hello C++");
	cout << "str4=" << str4 << endl;

	string str5;
	str5.assign("hello C++", 5);
	cout << "str5=" << str5 << endl;
	
	string str6;
	str6.assign(str5);
	cout << "str6=" << str6 << endl;

	string str7;
	str7.assign(10, 'w');
	cout << "str7=" << str7 << endl;
}


int main()
{
  test201();
	system("pause");
	return 0;
}

运行结果:
str1=hello world
str2=hello world
str3=a
str4=hello C++
str5=hello
str6=hello
str7=wwwwwwwwww

string字符串拼接

函数原型:
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个字符连接到字符串结尾

示例代码:

#include <iostream>
#include<string>
using namespace std;
void test301()
{
	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 ");
	cout << "str3=" << str3 << endl;

	str3.append("game abcde", 4);
	cout << "str3=" << str3 << endl;

	str3.append(str2);
	cout << "str3=" << str3 << endl;

	str3.append(str2, 0, 3);   //str2 从0号位置开始截取3个字符,拼接到str3
	cout << "str3=" << str3 << endl;
}

int main()
{
	test301();
	system("pause");
	return 0;
}

运行结果
str1=我爱玩游戏
str1=我爱玩游戏:
str1=我爱玩游戏:LOL DNF
str3=I love
str3=I love game
str3=I love gameLOL DNF
str3=I love gameLOL DNFLOL

string字符串查找和替换

函数原型
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 chars, 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* s, 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

示例代码

#include <iostream>
#include<string>
using namespace std;
//1.查找
void test401()
{
	string str1 = "abcdefg";
	int pos = str1.find("de");
	if(pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串,pos = " << pos << endl;
	}

	//find
	pos = str1.rfind("de");
	cout << "找到字符串,pos = " << pos << endl;

}


//2.替换
void test402()
{
	string str1 = "abcdefg";
	str1.replace(1, 3, "1111");
	cout << "str1=" << str1 << endl;
}

int main()
{
	test401();
	test402();
	system("pause");
	return 0;
}

运行结果:
找到字符串,pos = 3
找到字符串,pos = 3
str1=a1111efg

string字符串的比较

字符串比较是按字符的ASCII码进行对比
= 返回 0

返回 1
< 返回 -1

函数原型:
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较

string字符串的存取

string中单个字符存取方式有两种
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符

示例代码:

#include <iostream>
#include<string>
using namespace std;
void test601()
{
	string str = "hello world";
	//通过[]方式取字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	//通过at方法获取字符
	for (int i = 0; i < str.size(); i++)   //str.size访问字符串长度
	{
		cout << str.at(i) << " ";
	}

	//修改单个字符
	str[0] = 'x';
	cout << "str = " << str << endl;

	str.at(1) = 'x';
	cout << "str = " << str << endl;
}

int main()
{
	test601();
	system("pause");
	return 0;
}

运行结果:
h e l l o w o r l d
h e l l o w o r l d str = xello world
str = xxllo world

string字符串插入和删除

功能描述:对string字符串进行插入和删除字符操作
函数原型:
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 substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串

示例代码:

#include <iostream>
#include<string>
using namespace std;
//字符串插入和删除
void test801()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	char s = 's';
	str.insert(1, 5, s);
	cout << str << endl;

	//删除
	str.erase(1, 8);
	cout << str << endl;
	
	//截取
	string subStr = str.substr(1, 3);
	cout << subStr << endl;
}

int main()
{
	test801();
	system("pause");
	return 0;
}

运行结果:
h111ello
hsssss111ello
hello
ell

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值