C++ STL -->string类

文章目录

  • STL
    • 什么是STL
  • String类
    • string类对象的构造方式
    • string类对象的容量操作
    • string类对象的访问及遍历操作
      • string迭代器函数遍历类对象
    • stirng类对象的修改操作
    • string类非成员函数

STL

什么是STL

STL全称standard template libaray-标准模板库 是C++标准库的重要组成部分 不仅是一个可复用的组件库 更是一个保罗数据结构与算法的软件框架
STL是C++中的优秀的作品 有了它 许多底层数据结构以及算法都不需要自己重新造轮子 直接站在巨人的肩膀上 健步如飞快速进行开发

String类

标准库中的string类string类的文档介绍

string类对象的构造方式

string类实现了多个构造函数的重载 常用的构造函数有:
C++ STL -->string类_第1张图片
示例:

string s1();//构造空的string类对象 即空字符串s1
string s2("Hello stirg"); //用常量字符串构造string类对象s2
string s3(10, 'x');//用10个x构造string类对象s3
string s4(s2);//用s2 拷贝构造s4

string类对象的容量操作

C++ STL -->string类_第2张图片

  1. size函数:返回字符串有效字符长度 不算\0
// size_t size() const; 函数接口
string s2("Hello string");
cout << s2.size() << endl;//12
  1. length函数:和size函数一样 返回字符串有效的字符长度
    两者没区别引入size()的原因是为了与其他容器的接口保持一 致,一般情况下基本都是用size()。
//size_t length() const;函数接口
string s2("Hello string");
cout << s2.length() << endl; //12
  1. max_size: 返回字符串的最大容量(没发现有什么用)
//size_t max_size() const; 函数接口
  1. resize: 改变当前对象的有效字符个数
//void resize (size_t n); 函数接口
//void resize (size_t n, char c); 函数接口
string s2("Hello string");
cout << s2.size() << endl;//12

s2.resize(20);
cout << s2.size() << endl;//20
cout << s2 << endl; //Hello string

string s3("Hello string");
s3.resize(20, 'x');
cout << s3.size() << endl; //20
cout << s3 << endl; //Hello stringxxxxxxxx

string s4("Hello string");
s4.resize(2);
cout << s4.size() << endl; //2
cout << s4 << endl; //He

【resize规则】:

  • 当n大于当前对象的size时 将size扩大到n 若未指定字符 默认为’\0’
  • 当n小于当前对象的size时 将size缩小到n
  1. capacity:返回总空间的大小
//size_t capacity() const; 函数接口
string s5("Hello string");
cout << s5.capacity() << endl; //15
  1. reserve: 改变对象容量的大小
//void reserve (size_t n = 0); 函数接口
string s5("Hello string");
cout << s5.capacity() << endl;//15

s5.reserve(50);
cout << s5.capacity() << endl;//63

s5.reserve(10);
cout << s5.capacity() << endl;//63

【reserve规则】:

  • 当n大于当前对象的capacity时 将capacity扩大到n或大于n(不同的编译器实现的不一样)
  • 当n小于当前对象的capacity时 什么也不做
  1. clear:清空有效字符
//void clear(); 函数接口
string s6("Hello string");
s6.clear();
cout << s6 << endl;//什么都不打印 已经清空了

clear()只是将string中有效字符清空,不改变底层空间大小。
8. empty: 检测字符串是否为空串 是返回true 否返回false

//bool empty() const; 函数接口
string s7("Hello string");
cout << s7.empty() << endl;//0

string s8("");
cout << s8.empty() << endl; //1

string类对象的访问及遍历操作

image.png

  1. operator[]返回pos位置的字符(pos为下标)
//char& operator[] (size_t pos); 函数接口
//const char& operator[] (size_t pos) const; 函数接口 
string s9("Hello string");
cout << s9[0] << endl; //H
cout << s9[1] << endl; //e
s9[0] = 'X';
cout << s9 << endl; //Xello string
  1. at与操作符[]功能一样 区别在于一个是操作符重载 一个是成员函数
//char& at (size_t pos); 函数接口
//const char& at (size_t pos) const;  函数接口
string s10("Hello string");
cout << s10.at(0) << endl; //H
s10.at(0) = 'X'; 
cout << s10 << endl;//Xello string
  1. back和front

返回字符串的尾部和头部位置的字符

//char& back();函数接口
//const char& back() const;函数接口
//char& front();函数接口
//const char& front() const; 函数接口
string s11("Hello string");
s11.back() = '!';
s11.front() = 'X';
cout << s11 << endl;//Xello strin!

string迭代器函数遍历类对象

C++ STL -->string类_第3张图片

  1. begin和endbegin获取第一个字符的迭代器 end获取最后一个字符的下一个位置的迭代器 一般用来遍历对象
//iterator begin(); 函数接口
//const_iterator begin() const; 函数接口
//iterator end(); 函数接口
//const_iterator end() const; 函数接口
string s12("Hello string");
//返回值是一个迭代器类型 用法和指针有点类似
string::iterator  it = s12.begin();
while (it != s12.end())
{
        cout << *it;
        it++;
}//Hello string
  1. rebegin和rendrbegin获取最后一个字符的下一个位置的迭代器 rend获取第一个字符的迭代器 一般用来逆向遍历
//reverse_iterator rbegin(); 函数接口
//const_reverse_iterator rbegin() const; 函数接口
//reverse_iterator rend(); 函数接口
//const_reverse_iterator rend() const; 函数接口
string s13("Hello string");
auto rit = s13.rbegin();

while (rit != s13.rend())
{
        cout << *rit;
        ++rit;
}//gnirts olleH

剩下的用处很少 就不介绍了

stirng类对象的修改操作

C++ STL -->string类_第4张图片

  1. operator+= 在字符串后面追加字符串str
//string& operator+= (const string& str); 函数接口
//string& operator+= (const char* s); 函数接口
//string& operator+= (char c); 函数接口
string s14("Hello string");
string s15("!");
s14 += s15;
cout << s14 << endl; //Hello string!
s14 += "!!!";
cout << s14 << endl; //Hello string!!!!
s14 += '!';
cout << s14 << endl; //Hello string!!!!!
  1. append 在字符串后面追加字符串
//string& append (const string& str);   函数接口
//string& append (const char* s);   函数接口
//string& append (size_t n, char c); 函数接口
string s16("Hello string");
string s17("!!!");
s16.append(s17);
cout << s16 << endl;//Hello string!!!
s16.append("!!!"); 
cout << s16 << endl;//Hello string!!!!!!
s16.append(10,'x'); 
cout << s16 << endl;//Hello string!!!!!!xxxxxxxxxx
  1. push_back:在字符串后面尾插字符c
//void push_back (char c); 函数接口
string s18("Hello stirng");
s18.push_back('!');
s18.push_back('!');
s18.push_back('!');
cout << s18 << endl;// Hello stirng!!!

4.assign:分配一个新的字符串替换当前内容

//string& assign (const string& str); 函数接口
//string& assign (const char* s); 函数接口
//string& assign (size_t n, char c); 函数接口

string s19("Hello");
string s20("string");
s19.assign(s20);
cout << s19 << endl;//string
s19.assign("XXXXX");
cout << s19 << endl; //XXXXX
s19.assign(10, '!');
cout << s19 << endl;//!!!!!!!!!!
  1. insert在指定的位置后面插入字符串
//string& insert (size_t pos, const string& str);  函数接口
//string& insert (size_t pos, const char* s);  函数接口
//iterator insert (iterator p, char c); 函数接口
string s21("H");
s21.insert(1, "e");//He
s21.insert(2, "llo"); //Hello
s21.insert(s21.end(), '!');//Hello!

6.erase删除指定位置的字符

//string& erase (size_t pos = 0, size_t len = npos);   函数接口
//iterator erase (iterator p);   函数接口
//iterator erase (iterator first, iterator last); 函数接口
string s22("Hello string");
s22.erase(6, 7);
cout << s22 << endl; //Hello
s22.erase(s22.begin());
cout << s22 << endl; //ello
s22.erase(s22.begin(),s22.end());
cout << s22 << endl;//空

7.replace替换字符串的一部分

//string& replace (size_t pos, size_t len, const char* s);  函数接口
//string& replace (size_t pos, size_t len, size_t n, char c); 函数接口
string s22("Hello string");
s22.replace(6, 7,"juejing");
cout << s22 << endl;//Hello juejing
s22.replace(6, 7, 8, 'x');
cout << s22 << endl;//Hello xxxxxxxx

8.swap交换两个string类对象

//void swap (string& str); 函数接口
string s23("hello");
string s24("string");
s23.swap(s24);
cout << s23 << endl; //string
cout << s24 << endl; //hello

string类非成员函数

C++ STL -->string类_第5张图片

  1. operator+:连接字符串
//string operator+ (const string& lhs, const string& rhs); 函数接口string类+string类
//string operator+ (const string& lhs, const char*   rhs);函数接口 string类+字符串
//string operator+ (const char*   lhs, const string& rhs);函数接口 字符串+string类
//string operator+ (const string& lhs, char          rhs);函数接口 string类+字符
//string operator+ (char          lhs, const string& rhs);函数接口 字符+string类
string s24("Hello ");
string s25("string");
cout << (s24 + s25) << endl;//Hello string
cout << (s24 + "string") << endl;//Hello string
cout << ("Hello " + s25) << endl;//Hello string
cout << (s24 + '!') << endl; //Hello !
cout << ('!' + s25) << endl; //!string

【建议】:尽量少用 传值返回 深拷贝 效率太低

  1. relational operator 大小比较
    函数接口:
    C++ STL -->string类_第6张图片
    用法与内置类型一样
string s24("Hello ");
string s25("string");
if (s24 > s25)
{
        cout << "s24 > s25" << endl;
}
else
{
        cout << "s24 < s25" << endl;//执行此分支
}

3.swap:交换两个string类对象
之前的swap是string类的成员函数 这个swap是非成员函数

//void swap (string& x, string& y); 函数接口
string s26("Hello string");
string s27("Hello world");
swap(s26, s27);
cout << s26 << endl;//Hello world
cout << s27 << endl;//Hello string

4.operator>> 和 operator<<:流插入和流提取操作符重载

//istream& operator>> (istream& is, string& str); 函数接口
//ostream& operator<< (ostream& os, const string& str); 函数接口
string s28;
cin >> s28;//输入
cout << s28 << endl;

5.getline获取一行字符串

//istream& getline (istream& is, string& str, char delim); 从is中提取到的字符存储到str中,直到读取到分隔符delim或换行符’\n’为止。
//istream& getline (istream& is, string& str);从is中提取到的字符存储到str中,直到读取到换行符’\n’为止。
string s29;
getline(cin,s29,'D'); //输入ABCDEF
cout << s29;//输出ABC

string s30;
getline(cin, s30);//输出Hello string
cout << s30; //输出Hello string

【注意】:使用>>进行输入时 当读取到空格时就会停止读取 所以不能使用>>将带有空格的字符串读到string类对象中 使用getline函数就可以解决这一问题

你可能感兴趣的:(C++,c++,开发语言)