C++ stl中的string的相关用法
文章目录
- string的定义
- string的插入与拼接
- string的删除
- string的查找
- string的比较
- string的替换
- string的交换
- string的大小和容量
- string对元素进行访问
- string相关运算符
- string中与迭代器相关的函数
- string与字符串之间的转换
- string中字符串的提取
- getline函数
string类的使用,首先要加上头文件
#include <string>
string的定义
string类有多个构造函数的重载,以下为常用的构造函数:
string(); //构造一个空字符串string(const char* s); //复制s所指的字符串string(const char* s, size_t n); //复制s所指字符串的前n个字符string(size_t n, char c); //生成有n个c字符的字符串string(const string& str); //复制strstring(const string& str, size_t pos, size_t len = npos); //str中从字符下标pos开始的len个字符构成的子串
示例:
string s1; //空字符串
string s2("hello string"); //复制"hello string"
string s3("hello string", 3); //复制"hello string"的前3个字符 为"hel"
string s4(10, 'h'); //生成10个'h'字符的字符串 为"hhhhhhhhhh"
string s5(s2); //s5为复制s2的结果
string s6(s2, 0, 5); //复制s2中从字符位置0开始5个字符的部分 为"hello"
string的插入与拼接
1.push_back
用于末尾插入
void push_back (char c);
例子:
int main()
{string s;s.push_back('f');s.push_back('a');s.push_back('l');s.push_back('l');cout << s << endl; //fallreturn 0;
}
2.insert
能够指定位置插入字符或字符串
string& insert (size_t pos, const string& str);
string& insert (size_t pos, const char* s);
iterator insert (iterator p, char c);
例子:
int main()
{string s("fall"); //fall 初始化字符串s为fall//insert(pos, str)在pos位置插入字符串strs.insert(4, "zz"); //fallzz//insert(pos, string)在pos位置插入string对象string tmp("zzz");s.insert(6, tmp); //fallzzzzz//insert(pos, char)在pos位置插入字符char s.end()获取末尾位置s.insert(s.end(), '!'); //fallzzzzz!cout << s << endl; //fallzzzzz!return 0;
}
3.append
能够完成string的拼接
string& append (const string& str);
string& append (const char* s);
string& append (size_t n, char c);
例子:
int main()
{string s1("fall");string s2(" zzzzz");//append(string)完成两个string对象的拼接s1.append(s2); //fall zzzzz//append(str)完成string对象和字符串str的拼接s1.append(" ZZ"); //fall zzzzz ZZ//append(n, char)将n个字符char拼接到string对象后面s1.append(3, '!'); //fall zzzzz ZZ!!!cout << s1 << endl; //fall zzzzz ZZ!!!return 0;
}
4.operator+=
使用+=能在字符串后追加字符串或者字符
string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
例子:
int main()
{string s1("fall");string s2(" zzzzz");// operator += str 在字符串s1后追加s2s1+=s2; //fall zzzzz//operator += c 在字符串s1后追加字符!s1+='!'; //fall zzzzz!cout << s1 << endl; //fall zzzzz!return 0;
}
string的删除
1.pop_back
能够进行尾删
void pop_back();
例子:
int main()
{string s("C++");s.pop_back();cout << s << endl; //C+return 0;
}
2.erase
能够对指定位置进行删除
string& erase (size_t pos = 0, size_t len = npos);
iterator erase (iterator p);
iterator erase (iterator first, iterator last);
例子:
int main()
{string s("I lovefall!");//erase(pos, n)删除pos位置开始的n个字符s.erase(6, 5); //I love //erase(pos)删除pos位置的字符s.erase(s.end()); //I lov//erase(pos1, pos2)删除[pos1pos2)上所有字符 注意左闭右开s.erase(s.begin() + 1, s.end()); //Icout << s << endl; //Ireturn 0;
}
string的查找
1.find
能够正向查找第一个匹配项
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;
例子:
int main()
{string s1("There are two needles in this haystack with needles");//find(string)正向搜索与string对象所匹配的第一个位置string s2("two");size_t pos1 = s1.find(s2);cout << pos1 << endl; //10//find(str)正向搜索与字符串str所匹配的第一个位置char str[] = "needles";size_t pos2 = s1.find(str);cout << pos2 << endl; //14//find(char)正向搜索与字符char所匹配的第一个位置size_t pos3 = s1.find('l');cout << pos3 << endl; //18return 0;
}
注意: find()函数一般配合npos使用,当函数找到传入的参数元素时,返回相应下标;未找到时,返回npos
2.rfind
能够反向查找第一个匹配项
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (char c, size_t pos = npos) const;
例子:
int main()
{string s1("There are two needles in this haystack with needles");//find(string)正向搜索与string对象所匹配的第一个位置string s2("needles");size_t pos1 = s1.find(s2);cout << pos1 << endl; //44//find(str)正向搜索与字符串str所匹配的第一个位置char str[] = "two";size_t pos2 = s1.find(str);cout << pos2 << endl; //10//find(char)正向搜索与字符char所匹配的第一个位置size_t pos3 = s1.find('l');cout << pos3 << endl; //18return 0;
}
string的比较
compare
比较两个字符串的大小
int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
比较规则:
假设有字符串str1和str2
1、str1中第一个不相同的字符值较小,或者所有比较字符都相同,但str1较短,则返回小于0的值。
2、str1中第一个不相同的字符值较大,或者所有比较字符都相同,但str1较长,则返回大于0的值。
3、比较的两个字符串相等(包括长度),则返回0。
例子:
int main()
{string s1("hello world");string s2("hello fall");//"hello world"和"hello fall"比较cout << s1.compare(s2) << endl; //-1//"ello"和"hello fall"比较cout << s1.compare(1, 4, s2) << endl; //-1//"hello"和"hello"比较cout << s1.compare(0, 4, s2, 0, 4) << endl; //0return 0;
}
注意: string类可以对象与对象之间比较,也可以string类对象和字符串进行比较
string的替换
replace
能够对string进行替换
string& replace (size_t pos, size_t len, const char* s);
string& replace (size_t pos, size_t len, size_t n, char c);
例子:
int main()
{string s("hello world");//replace(pos, len, str)将pos位置开始的len个字符替换为字符串strs.replace(6, 4, "fall"); //hello falld//replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符chars.replace(10, 1, 3, '!'); //hello fall!!!cout << s << endl;return 0;
}
string的交换
swap
能够完成两个string的交换
void swap (string& x, string& y);
void swap (string& str);
例子:
int main()
{string s1("hello");string s2("fall");//使用string类的成员函数swap交换s1和s2s1.swap(s2);cout << s1 << endl; //fallcout << s2 << endl; //hello//使用非成员函数swap交换s1和s2swap(s1, s2);cout << s1 << endl; //hellocout << s2 << endl; //fallreturn 0;
}
string的大小和容量
1.size或者length
能够获取当前有效字符的个数,即长度
size_t size() const;
size_t length() const;
例子:
int main()
{string s("fall");cout << s.size() << endl; //4cout << s.length() << endl; //4return 0;
}
2.max_size
能够获取string对象最多可包含的字符数
size_t max_size() const;
例子:
int main()
{string s("fall");cout << s.max_size() << endl; //4294967294return 0;
}
3.capacity
能够获取当前对象所分配的存储空间的大小
size_t capacity() const;
例子:
int main()
{string s("fall");cout << s.capacity() << endl; //15return 0;
}
4.resize
能够改变当前对象的有效字符的个数
void resize (size_t n);
void resize (size_t n, char c);
resize使用规则:
1、当n大于对象当前的size时(即有效字符个数),将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’。
2、当n小于对象当前的size时,将size缩小到n。
例子:
int main()
{string s1("fall");//resize(n)n大于对象当前的size时,将size扩大到n,扩大的字符默认为'\0's1.resize(20);cout << s1 << endl; //fallcout << s1.size() << endl; //20cout << s1.capacity() << endl; //31string s2("fall");//resize(n, char)n大于对象当前的size时,将size扩大到n,扩大的字符为chars2.resize(20, 'x');cout << s2 << endl; //fallxxxxxxxxxxxxxxxxcout << s2.size() << endl; //20cout << s2.capacity() << endl; //31string s3("fall");//resize(n)n小于对象当前的size时,将size缩小到ns3.resize(2);cout << s3 << endl; //facout << s3.size() << endl; //2cout << s3.capacity() << endl; //15return 0;
}
注意:若给出的n大于对象当前的capacity,则capacity也会根据自己的增长规则进行扩大
5.reserve
能够改变当前对象的容量大小
void reserve (size_t n = 0);
reserve使用规则:
1、当n大于对象当前的capacity时,将capacity扩大到n或大于n。
2、当n小于对象当前的capacity时,什么也不做。
例子:
int main()
{string s("fall");cout << s.size() << endl; //4cout << s.capacity() << endl; //15//reverse(n)当n大于对象当前的capacity时,将当前对象的capacity扩大为n或大于ns.reserve(20); cout << s << endl; //fallcout << s.size() << endl; //4cout << s.capacity() << endl; //31//reverse(n)当n小于对象当前的capacity时,什么也不做s.reserve(2);cout << s << endl; //fallcout << s.size() << endl; //4cout << s.capacity() << endl; //31return 0;
}
注意:此函数对字符串的size没有影响,并且无法更改其内容。主要会对capacity有影响
6.clear
删除对象的内容,删除后对象变为空字符串
void clear();
例子:
int main()
{string s("fall");//clear()删除对象的内容,该对象将变为空字符串s.clear();cout << s << endl; //空字符串return 0;
}
7.empty
判断对象是否为空字符串
bool empty() const;
例子:
int main()
{string s("fall");cout << s.empty() << endl; // false 不为空//clear()删除对象的内容,该对象将变为空字符串s.clear();cout << s.empty() << endl; // true 为空return 0;
}
string对元素进行访问
1.[ ]+下标
由于string类中对[ ]运算符进行了重载,所以能够直接使用这种方式访问对象中的元素,为字符。
并且由于该重载是引用返回,所以我们可以通过该方式修改对应位置的元素
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
例子:
int main()
{string s("fall");//[]+下标访问对象元素for (int i = 0; i < s.size(); i++){cout << s[i];}cout << endl; // fall//[]+下标修改对象元素内容for (int i = 0; i < s.size(); i++){s[i] = 'x';}cout << s << endl; //xxxxreturn 0;
}
2.at
能够访问对象中的元素,并且也是使用引用返回,所以可以通过该函数修改对应位置的元素
char& at (size_t pos);
const char& at (size_t pos) const;
例子:
int main()
{string s("fall");for (int i = 0; i < s.size(); i++){//at(pos)访问pos位置的元素cout << s.at(i);}cout << endl;for (int i = 0; i < s.size(); i++){//at(pos)访问pos位置的元素,并对其进行修改s.at(i) = 'y';}cout << s << endl; //yyyyreturn 0;
}
3.范围for
如果需要通过范围for对对象的元素进行修改,那么用于接收元素的变量的类型需要为引用类型,否则用于接收元素的变量只是对象元素的拷贝,不会影响到对象中的元素
例子:
int main()
{string s("fall");//使用范围for访问对象元素for (auto n : s){cout << n;}cout << endl; //fall//使用范围for访问对象元素,并对其进行修改for (auto& n : s) //需要修改对象的元素,n必须是引用类型{n = 'x';}cout << s << endl; //xxxxreturn 0;
}
4.使用迭代器访问对象中的元素
例子:
int main()
{string s("fall");//使用迭代器访问对象元素string::iterator it1 = s.begin(); // 获得起始位置while (it1 != s.end()) // 当迭代器的指向不为末尾位置时 就循环打印{cout << *it1;it1++;}cout << endl; //fall//使用迭代器访问对象元素,并对其进行修改string::iterator it2 = s.begin();while (it2 != s.end()){*it2 += 1;it2++;}cout << s << endl; // gbmmreturn 0;
}
string相关运算符
1.operator=
string类中对=运算符进行了重载,能够对string类进行赋值
例子:
int main()
{string s1;string s2("fall");//支持string类的赋值s1 = s2;cout << s1 << endl; // fall//支持字符串的赋值s1 = "haha";cout << s1 << endl; // haha//支持字符的赋值s1 = 'x';cout << s1 << endl; // xreturn 0;
}
2.operator+=
string类中对+=运算符进行了重载,能够对字符串进行拼接
例子:
int main()
{string s1("fall");string s2(" zzzzz");// operator += str 在字符串s1后追加s2s1+=s2; //fall zzzzz//operator += c 在字符串s1后追加字符!s1+='!'; //fall zzzzz!cout << s1 << endl; //fall zzzzz!return 0;
}
3.operator+
string类中对+运算符进行了重载,能够支持string类对象,字符串,字符之间的相加
例子:
int main()
{string s;string s1("best");string s2("friend");char str[] = "you";char ch = '!';//string类 + string类s = s1 + s2;cout << s << endl; //bestfriend//string类 + 字符串s = s1 + str;cout << s << endl; //bestyou//字符串 + string类s = str + s1;cout << s << endl; //youbest//string类 + 字符s = s1 + ch;cout << s << endl; //best!//字符 + string类s = ch + s1;cout << s << endl; //!bestreturn 0;
}
4.operator>> 和 operator<<
string类中对<<和>>运算符进行了重载,能够直接使用<<和>>对string类对象进行输入和输出
例子:
int main()
{string s; // 创建string类对象cin >> s; //输入cout << s << endl; //输出return 0;
}
5.relational operators
relational operator是关系比较运算符,包括了==、!=、<、>、<=、>=。能够支持string类对象和字符串之间关系的比较
例子:
int main()
{string s1("abc");string s2("abd");cout << (s1 > s2) << endl; // 0 返回falsecout << (s1 < s2) << endl; // 1 返回truecout << (s1 == s2) << endl; // 0 返回falsereturn 0;
}
注意:重载的关系比较运算符的比较是根据对应字符的ASCII码值进行比较的,如果匹配的字符一样,就看长度
string中与迭代器相关的函数
1.与正向迭代器相关
begin
返回一个指向字符串第一个字符的迭代器
iterator begin();
const_iterator begin() const;
end
返回一个指向字符串结束字符的迭代器,即指向末尾’\0’的位置
iterator end();
const_iterator end() const;
例子:
int main()
{string s("hello world");// 正向迭代器 用于遍历string::iterator it = s.begin();while (it != s.end()){cout << *it;it++;}cout << endl; //hello worldreturn 0;
}
2.与反向迭代器相关
rbegin
返回指向字符串最后一个字符的反向迭代器
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
rend
返回指向字符串第一个字符的前面的理论元素的反向迭代器
reverse_iterator rend();
const_reverse_iterator rend() const;
例子:
int main()
{string s("hello world");//反向迭代器 反向遍历string::reverse_iterator rit = s.rbegin();while (rit != s.rend()){cout << *rit;rit++;}cout << endl; //dlrod ollehreturn 0;
}
string与字符串之间的转换
1.将字符串转换为string
int main()
{//方式一string s1("hello world");//方式二char str[] = "hello world";string s2(str);cout << s1 << endl; //hello worldcout << s2 << endl; //hello worldreturn 0;
}
2.将string转换为字符串
有两种方式,分别是使用c_str或者data
const char* c_str() const;
const char* data() const;
两者的区别:
在C++98中,c_str()返回 const char* 类型,返回的字符串以空字符结尾。
在C++98中,data()返回 const char* 类型,返回的字符串 不以空字符结尾。
在C++11版本中,c_str()与data()用法相同。
例子:
int main()
{string s("hello world ");const char* str1 = s.data();const char* str2 = s.c_str();cout << str1 << endl;cout << str2 << endl;return 0;
}
string中字符串的提取
1.substr
能够提取string中的子字符串
string substr (size_t pos = 0, size_t len = npos) const;
例子:
int main()
{string s1("helloworld");string s2;//substr(pos, n)提取pos位置开始的n个字符序列作为返回值s2 = s1.substr(2, 3);cout << s2 << endl; //lloreturn 0;
}
2.copy
能够将string中的子字符串拷贝到字符数组中
size_t copy (char* s, size_t len, size_t pos = 0) const;
例子:
int main()
{string s("helloworld");char str[20];//copy(str, n, pos)复制pos位置开始的n个字符到str字符串size_t length = s.copy(str,2, 3);//copy函数不会在复制内容的末尾附加'\0',需要手动加str[length] = '\0';cout << str << endl; //lloreturn 0;
}
getline函数
当我们使用>>进行输入操作时,读取到空格就会停止,但是在一些场景下我们是不想停止的,getline能够解决这个问题
示例:
int main()
{string s;cin >> s; //输入:hello worldcout << s << endl; //输出:helloreturn 0;
}
使用方法一:
istream& getline (istream& is, string& str);
getline函数将从is中提取到的字符存储到str中,直到读取到换行符’\n’为止
例子:
int main()
{string s;getline(cin, s); //输入:hello worldcout << s << endl; //输出:hello worldreturn 0;
}
使用方法二:
istream& getline (istream& is, string& str, char delim);
getline函数将从is中提取到的字符存储到str中,直到读取到分隔符delim(自定义)或换行符’\n’为止
例子:
int main()
{string s;getline(cin, s, 'r'); //输入:hello worldcout << s << endl; //输出:hello woreturn 0;
}