【C++】string类的使用③(修改器Modifiers || 非成员函数重载Non-member function overloads)

7 篇文章 1 订阅

在这里插入图片描述

🔥个人主页: Forcible Bug Maker
🔥专栏: STL || C++

前言

本篇博客主要内容:STL库中string的修改器(Modifiers)和非成员函数重载(Non-member function overloads)

来到string类的使用第三篇,继续我们的内容,本篇博客将介绍如何使用STL库中string的成员函数修改串,以及重载给string的几个非成员函数。

🔥修改器(Modifiers)

在这里插入图片描述
顾名思义,就是一批能改动string串中内容的成员函数。

operator+=

这是一个成员函数的运算符重载。
在这里插入图片描述

简单说就是在串的末尾追加字符或字符串。

(1) string string对象
string& operator+= (const string& str);
(2) c-string 字符串指针
string& operator+= (const char* s);
(3) character 字符
string& operator+= (char c);
以上三个重载的功能用一句话概括:在当前string串的末尾追加字符或字符串

共同的返回值:当前对象的引用(string&)

使用样例:

// string::operator+=
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string name("John");
	string family("Smith");
	name += " K. ";         // c-string
	name += family;         // string
	name += '\n';           // character

	cout << name;
	return 0;
}

在这里插入图片描述

append

在这里插入图片描述

append的功能和操作符重载的+=类似,也是在串的末尾追加内容,不过append提供的重载更多,追加字符或字符串的方式也更多。

(1) string
string& append (const string& str);
在string对象串的末尾追加str串的拷贝
(2) substring
string& append (const string& str, size_t subpos, size_t sublen);
在string对象串的末尾追加一个str的子串,这个子串从str下标位置subpos开始取,取sublen个字符(如果sublen过大超出str串的范围,则取到str的末尾)
(3) c-string
string& append (const char* s);
在string对象串的末尾追加s指向的以’\0’字符结尾的字符串的拷贝
(4) buffer
string& append (const char* s, size_t n);
在string对象串的末尾追加s指向的前n个字符的拷贝
(5) fill
string& append (size_t n, char c);
在string对象串的末尾追加n个char类型的c字符
(6) range
template <class InputIterator>
string& append (InputIterator first, InputIterator last);
在string对象串的末尾按顺序追加迭代器区间[first,last)之间指向的字符序列

共同的返回值:当前对象的引用(string&)

使用案例:

// appending to string
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	string str2 = "Writing ";
	string str3 = "print 10 and then 5 more";

	// used in the same order as described above:
	str.append(str2);                       // "Writing "
	str.append(str3, 6, 3);                   // "10 "
	str.append("dots are cool", 5);          // "dots "
	str.append("here: ");                   // "here: "
	str.append(10, '.');                    // ".........."
	str.append(str3.begin() + 8, str3.end());  // " and then 5 more"

	cout << str << endl;
	return 0;
}

在这里插入图片描述

push_back和pop_back

在这里插入图片描述
void push_back (char c);
在string对象串的末尾追加一个char类型的字符c
无返回值。
在这里插入图片描述
void pop_back();
在string对象串的末尾删除一个字符
无返回值和参数。

使用案例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str("hello world");
	str.push_back('!');
	cout << str << endl;

	str.pop_back();
	cout << str << endl;
	return 0;
}

在这里插入图片描述

assign

在这里插入图片描述

大体功能是给当前串赋新值,将原来的内容覆盖。

(1) string
string& assign (const string& str);
拷贝str
(2) substring
string& assign (const string& str, size_t subpos, size_t sublen);
拷贝str从下标subpos开始的sublen个字符(如果sublen过大超出str串的范围,则取到str的末尾)
(3) c-string
string& assign (const char* s);
拷贝s指向的以’\0’字符结尾的字符串
(4) buffer
string& assign (const char* s, size_t n);
拷贝s指向的前n个字符
(5) fill
string& assign (size_t n, char c);
拷贝n个字符c组成的字符串
(6) range
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
按顺序拷贝迭代器区间[first,last)内的字符序列

使用案例:

// string::assign
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	string base = "The quick brown fox jumps over a lazy dog.";

	// used in the same order as described above:

	str.assign(base);
	cout << str << endl;

	str.assign(base, 10, 9);
	cout << str << endl;         // "brown fox"

	str.assign("pangrams are cool", 7);
	cout << str << endl;         // "pangram"

	str.assign("c-string");
	cout << str << endl;         // "c-string"

	str.assign(10, '*');
	cout << str << endl;         // "**********"
	
	str.assign(base.begin() + 16, base.end() - 12);
	cout << str << endl;         // "fox jumps over"

	return 0;
}

在这里插入图片描述

insert

在这里插入图片描述

大体功能是往串中插入字符或字符串

(1) string
string& insert (size_t pos, const string& str);
在pos位置之前插入str串的拷贝
(2) substring
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
在pos位置之前插入str的子串的拷贝(由subpos开始,取sublen个字符组成的字符串。如果sublen过大超出str串的范围,则取到str的末尾)
(3) c-string
string& insert (size_t pos, const char* s);
在pos位置前插入s指向(由’\0’字符结尾)的字符串的拷贝
(4) buffer
string& insert (size_t pos, const char* s, size_t n);
在pos位置前插入s指向的前n个字符组成字符串的拷贝
(5) fill
string& insert(size_t pos,size_t n,char c);
void insert (iterator p,size_t n,char c);
在pos位置前(或迭代器p指向位置前,此时无返回值)插入n个char类型的c字符
(6) single character
iterator insert (iterator p, char c);
迭代器p指向位置前插入一个char类型的c字符
(7) range
template <class InputIterator>
void insert (iterator p, InputIterator first, InputIterator last);
迭代器p指向的位置前按顺序插入迭代器区间[first,last)内的字符序列

使用样例:

// inserting into a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str = "to be question";
	string str2 = "the ";
	string str3 = "or not to be";
	string::iterator it;

	// used in the same order as described above:
	str.insert(6, str2);                 // to be (the )question
	str.insert(6, str3, 3, 4);             // to be (not )the question
	str.insert(10, "that is cool", 8);    // to be not (that is )the question
	str.insert(10, "to be ");            // to be not (to be )that is the question
	str.insert(15, 1, ':');               // to be not to be(:) that is the question
	it = str.insert(str.begin() + 5, ','); // to be(,) not to be: that is the question
	str.insert(str.end(), 3, '.');       // to be, not to be: that is the question(...)
	str.insert(it + 2, str3.begin(), str3.begin() + 3); // (or )

	cout << str << endl;
	return 0;
}

在这里插入图片描述

erase

在这里插入图片描述

将串中指定内容删除,使串的长度减少

(1) sequence
string& erase (size_t pos = 0, size_t len = npos);
将从pos开始的len个长度的字符删除(如果len过大超出串的范围或未提供第二个参数,则删到str的末尾)
(2) character
iterator erase (iterator p);
删除迭代器p指向的一个字符
(3) range
iterator erase (iterator first, iterator last);
删除迭代器区间[first,last)之间的元素

注:除了第一个返回当前对象,其他两个的返回值为第一个被删除字符位置的迭代器,如果此时这个位置没有字符,则返回指向末尾的迭代器string::end。

使用案例:

// string::erase
#include <iostream>
#include <string>
using namespace std;
int main()
{
    std::string str("This is an example sentence.");
    cout << str << '\n'; 
                                             // "This is an example sentence."
    str.erase(10, 8);                        //            ^^^^^^^^
    cout << str << '\n';
                                              // "This is an sentence."
    str.erase(str.begin() + 9);               //           ^
    cout << str << '\n';
                                                // "This is a sentence."
    str.erase(str.begin() + 5, str.end() - 9);  //       ^^^^^
    cout << str << '\n';
    // "This sentence."
    return 0;
}

在这里插入图片描述

replace

在这里插入图片描述

函数大体功能是将当前对象串中的一段字符串或字符用另一段字符串或字符替换。

(1) string
string& replace (size_t pos, size_t len, const string& str);
string& replace (iterator i1, iterator i2, const string& str);
将当前对象相应位置替换为str
(2) substring
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
将当前对象相应位置替换为str对象由subpos位置开始,跨越sublen的子串(如果len过大超出串的范围,则取到str的末尾)
(3) c-string
string& replace (size_t pos, size_t len, const char* s);
string& replace (iterator i1, iterator i2, const char* s);
将当前对象相应位置替换为s指向(由’\0’字符结尾)的字符串
(4) buffer
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (iterator i1, iterator i2, const char* s, size_t n);
将当前对象相应位置替换为s指向的前n个字符组成字符串
(5) fill
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (iterator i1, iterator i2, size_t n, char c);
将当前对象相应位置替换为n个char类型的c字符
(6) range
template <class InputIterator>
string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last);
将当前对象相应位置替换为迭代器区间[first,last)内的字符序列*。

使用案例:

// replacing in a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string base = "this is a test string.";
	string str2 = "n example";
	string str3 = "sample phrase";
	string str4 = "useful.";

	// replace signatures used in the same order as described above:

	// Using positions:                 0123456789*123456789*12345
	string str = base;           // "this is a test string."
	str.replace(9, 5, str2);          // "this is an example string." (1)
	str.replace(19, 6, str3, 7, 6);     // "this is an example phrase." (2)
	str.replace(8, 10, "just a");     // "this is just a phrase."     (3)
	str.replace(8, 6, "a shorty", 7);  // "this is a short phrase."    (4)
	str.replace(22, 1, 3, '!');        // "this is a short phrase!!!"  (5)

	// Using iterators:                                               0123456789*123456789*
	str.replace(str.begin(), str.end() - 3, str3);                    // "sample phrase!!!"      (1)
	str.replace(str.begin(), str.begin() + 6, "replace");             // "replace phrase!!!"     (3)
	str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7);    // "replace is cool!!!"    (4)
	str.replace(str.begin() + 12, str.end() - 4, 4, 'o');                // "replace is cooool!!!"  (5)
	str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end());// "replace is useful."    (6)
	cout << str << '\n';
	return 0;
}

在这里插入图片描述

swap

在这里插入图片描述
void swap (string& str);
交换两个string对象的内容

使用案例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1("hello");
	string str2("world");
	cout << str1 << endl;
	cout << str2 << endl;

	str1.swap(str2);

	cout << endl;
	cout << str1 << endl;
	cout << str2 << endl;
	return 0;
}

在这里插入图片描述

注:此函数比algorithm提供的swap交换更优,是string交换的最优方式。故交换string对象时尽量使用此方法。

🔥非成员函数重载(Non-member function overloads)

在这里插入图片描述

非成员函数重载,是作为string类的友元函数,在类的外面定义和实现,通过类型来匹配非成员函数。

operator+

在这里插入图片描述
这里其实不用过多赘述,六个重载实现了string对象相加的功能(string对象+字符串,string对象+字符,字符串+string对象,字符+string对象,string对象+string对象)。

注:没有 -> 字符串+字符串

代码案例:

// concatenating strings
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string firstlevel("com");
	string secondlevel("cplusplus");
	string scheme("http://");
	string hostname;
	string url;

	hostname = "www." + secondlevel + '.' + firstlevel;
	url = scheme + hostname;

	cout << url << '\n';
	return 0;
}

在这里插入图片描述

relation operators(string)

在这里插入图片描述

重载了string对象之间进行比较大小的功能,比较规则就是字典序。

代码案例:

// string comparisons
#include <iostream>
#include <vector>
using namespace std;
int main()
{
	string foo = "alpha";
	string bar = "beta";

	if (foo == bar) cout << "foo and bar are equal\n";
	if (foo != bar) cout << "foo and bar are not equal\n";
	if (foo < bar) cout << "foo is less than bar\n";
	if (foo > bar) cout << "foo is greater than bar\n";
	if (foo <= bar) cout << "foo is less than or equal to bar\n";
	if (foo >= bar) cout << "foo is greater than or equal to bar\n";

	return 0;
}

在这里插入图片描述

swap(string)

在这里插入图片描述
交换strirng对象

代码案例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1("hello");
	string str2("world");
	cout << str1 << endl;
	cout << str2 << endl;

	swap(str1, str2);

	cout << endl;
	cout << str1 << endl;
	cout << str2 << endl;
	return 0;
}

在这里插入图片描述

此非成员函数也可以实现string对象的交换,但是这个实现与重载在string类中的不同,string对象在交换串的时候是改变指针指向交换的。而这里的swap是使用tmp存储临时对象的方式实现交换,增大了内存开销。

流插入和流提取重载

在这里插入图片描述
控制台输入将内容读取到string对象中
在这里插入图片描述
将内容输入到控制台

使用案例:

// inserting strings into output streams
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	cin >> str;
	cout << str << '\n';
	return 0;
}

在这里插入图片描述

注:cin的读入是按照空格和换行进行分隔的,当你输入hello world时,使用上面的cin只能读取到hello。
如下:
在这里插入图片描述
当面对这种情况时,就需要我们的getline出手了。

getline

在这里插入图片描述

功能和cin相同,但可以通过此函数更改字符串读取时的分隔方式。

(1)
istream& getline (istream& is, string& str, char delim);
当你传第三个参数时,调用此函数,会以delim为分隔符进行分隔
(2)
istream& getline (istream& is, string& str);
当不传第三个参数,会默认以’\n’为分隔符

使用案例:

// extract to string
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string name;

	cout << "Please, enter your full name: ";
	getline(cin, name);
	cout << "Hello, " << name << "!\n";

	return 0;
}

在这里插入图片描述

结语

本篇博客,介绍了关于string的修改器,能修改string串中的内容;以及非成员函数的重载,实现了一些成员函数无法完成的功能和任务
博主会继续分享关于string类的使用以及STL更多的内容,感谢大家的支持。♥

  • 116
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 158
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Forcible Bug Maker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值