c++ string 字符串(包含string所有内容)

C++ 字符串

C++ 提供了以下两种类型的字符串表示形式:C风格字符串和C++ 引入的 string 类类型两种。主要讲的是c++中引入的string类型;C++ 中提供了专门的头文件 string,来支持 string 类型。string 类定义隐藏了字符串的数组性质,让我们处理字符串更方便。

string类是使用char(即,以其默认的char_traits和分配器类型作为其字符类型(有关模板的更多信息,请参阅basic_string)。

typedef basic_string string;
template<
class CharT,
class Traits = std::char_traits,
class Allocator = std::allocator> class basic_string;

模板形参

CharT 	- 	字符类型
Traits 	- 	指定字符类型上操作的特性类
Allocator 	- 	用于分配内部存储的分配器 (Allocator) 类型 

string常用字符类型的 typedef

类型 	           定义
std::string 	std::basic_string<char>
std::wstring 	std::basic_string<wchar_t>
std::u16string (C++11) 	std::basic_string<char16_t>
std::u32string (C++11) 	std::basic_string<char32_t>

迭代器

begin/cbegin:返回指向起始的迭代器(公开成员函数)
end /cend:返回指向末尾的迭代器(公开成员函数)
rbegin /crbegin:返回指向起始的逆向迭代器(公开成员函数)
rend/ crend:返回指向末尾的逆向迭代器(公开成员函数)

begin/end

iterator begin();
const_iterator begin() const;
返回指向字符串首字符的迭代器。

iterator end();
const_iterator end() const;
返回指向后随字符串末字符的字符的迭代器。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("my string");

	for (auto it = str.begin(); it != str.end(); ++it)
	{
		cout << *it;
	}
	cout << endl;

	return 0;
}

输出结果
在这里插入图片描述
rbegin/rend

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
返回指向逆转字符串首字符的逆向迭代器。它对应非逆向字符串的末字符。

reverse_iterator rend();
const_reverse_iterator rend() const;
返回指向后随逆向字符串末字符的字符的逆向迭代器

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("abcde");

	for (auto it = str.rbegin(); it != str.rend(); ++it)
	{
		cout << *it;
	}
	cout << endl;

	return 0;
}

输出结果
在这里插入图片描述

容量

empty:检查字符串是否为空(公开成员函数)
size/length:返回字符数(公开成员函数)
max_size:返回字符数的最大值(公开成员函数)
reserve:保留存储(公开成员函数)
capacity:返回当前对象分配的存储空间能保存的字符数量(公开成员函数)

empty

bool empty() const;

返回字符串是否为空(即长度是否为0)。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str;

	cout <<"str.empty:"<<str.empty()<<"\tstr:"<<str<<endl;

	str = "abcde";
	cout <<"str.empty:"<<str.empty()<<"\tstr:"<<str<<endl;


	return 0;
}

输出结果
在这里插入图片描述

size/length

size_t size() const;
返回字符串的长度,以字节为单位
size_t length() const;
返回字符串的长度,以字节为单位。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str="abcdef";

	cout <<"str size is:"<<str.size()<<endl;
	//cout <<"str size is:"<<str.length()<<endl;

	return 0;
}

输出结果
在这里插入图片描述

max_size

size_type max_size() const;
size_type max_size() const noexcept;

返回字符串可以达到的最大长度。

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str;

	cout <<"max_size is:"<<str.max_size()<<endl;


	return 0;
}

输出结果
在这里插入图片描述

capacity

size_t capacity() const;
返回当前分配给字符串的存储空间的大小,以字节表示。

举例

#include <iostream>
#include <string>

using namespace std;

void show_capacity(string const &str)
{

	cout << "'" << str << "' has capacity " << str.capacity() << endl;

}


int main(int argc, char const *argv[])
{

	string str("test");

	show_capacity(str);

	str += " is an example string.";
	show_capacity(str);


	return 0;
}

输出结果
在这里插入图片描述

元素访问

at:访问指定字符,有边界检查(公开成员函数)
operator[]:访问指定字符(公开成员函数)
front:访问首字符(公开成员函数)
back:访问最后的字符(公开成员函数)
data:返回指向字符串首字符的指针(公开成员函数)

at

char& at (size_t pos);
const char& at (size_t pos) const;

返回对字符串中pos位置的字符的引用。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("at strng");

	for (unsigned i = 0; i < str.size(); ++i)
	{
		cout <<str.at(i);
	}
	cout << endl;
	return 0;
}

输出结果
在这里插入图片描述

front

char& front();
const char& front() const;
返回对字符串第一个字符的引用。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("at strng");

	str.front() = 'T'; //返回对字符串第一个字符的引用。
	cout << str << endl;

	return 0;
}

输出结果
在这里插入图片描述

back

char& back();
const char& back() const;
返回对字符串最后一个字符的引用。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("at strng.");

	str.back() = '!'; //返回对字符串第一个字符的引用。
	cout << str << endl;

	return 0;
}

输出结果
在这里插入图片描述

查找

find:于字符串中寻找字符(公开成员函数)
rfind:寻找子串的最后一次出现(公开成员函数)
find_first_of:寻找字符的首次出现(公开成员函数)
find_first_not_of:寻找字符的首次缺失(公开成员函数)
find_last_of:寻找字符的最后一次出现(公开成员函数)
find_last_not_of:寻找字符的最后一次缺失(公开成员函数)

find

string (1)	size_t find (const string& str, size_t pos = 0) const;
c-string (2)	size_t find (const char* s, size_t pos = 0) const;
buffer (3)	size_t find (const char* s, size_t pos, size_t n) const;
character (4)	size_t find (char c, size_t pos = 0) const;

参数

str - 要搜索的 string
pos - 开始搜索的位置

搜索字符串,查找其参数指定的序列的第一个出现项。
当指定pos时,搜索只包含pos位置处或pos位置后的字符,忽略pos位置前包含字符的任何可能出现。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("This is an example");
	string str2("an");

	size_t found = str.find(str2);

	if (found != string::npos)
	{
		cout <<"Find the element is:"<<found<<endl;
	}

	return 0;
}

输出结果
在这里插入图片描述

rfind

string (1)	size_t rfind (const string& str, size_t pos = npos) const;
c-string (2)	size_t rfind (const char* s, size_t pos = npos) const;
buffer (3)	size_t rfind (const char* s, size_t pos, size_t n) const;
character (4)size_t rfind (char c, size_t pos = npos) const;

参数

str - 要搜索的 string
pos - 开始搜索的位置

搜索字符串,查找其参数指定的序列的最后一次出现。
当指定pos时,搜索只包含以pos位置或pos之前开始的字符序列,忽略pos之后开始的任何可能匹配。

举例

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{

	string str("Cats and dogs are good friends");
	string str2("dogs");

	size_t found = str.rfind(str2);

	if (found != string::npos)
	{
		str.replace(found,str2.size(),"lamb");
	}
	cout <<str<<endl;

	return 0;
}

输出结果
在这里插入图片描述

find_first_of

string (1)	size_t find_first_of (const string& str, size_t pos = 0) const;
c-string (2)	size_t find_first_of (const char* s, size_t pos = 0) const;
buffer (3)	size_t find_first_of (const char* s, size_t pos, size_t n) const;
character (4)	size_t find_first_of (char c, size_t pos = 0) const;

参数

str - 鉴别要搜索的字符的 string
pos - 搜索开始的位置

搜索字符串中匹配其参数中指定的任何字符的第一个字符。当指定pos时,搜索只包含pos位置处或pos位置后的字符,忽略pos位置前可能出现的任何字符。注意,序列中的一个字符匹配就足够了(不是所有字符)。查看string::查找匹配整个序列的函数。

举例

#include <iostream>
#include <string>
#include <cstddef>
using namespace std;

int main(int argc, char const *argv[])
{

	string str("Please, replace the vowels in this sentence by asterisks");
	string str2("aeiou");

	size_t found = str.find_first_of(str2);

	while (found != string::npos)
	{
		str[found] ='*';
		found = str.find_first_of(str2,found);
	}
	cout <<str<<endl;

	return 0;
}

输出结果
在这里插入图片描述

find_last_of

string (1)	size_t find_last_of (const string& str, size_t pos = npos) const;
c-string (2)	size_t find_last_of (const char* s, size_t pos = npos) const;
buffer (3)	size_t find_last_of (const char* s, size_t pos, size_t n) const;
character (4)	size_t find_last_of (char c, size_t pos = npos) const;

参数

str - 鉴别要搜索的字符的 string
pos - 搜索结束的位置

搜索字符串中与参数中指定的任何字符匹配的最后一个字符。
当指定pos时,搜索只包含pos位置或pos之前的字符,忽略pos之后可能出现的任何字符。

举例

#include <iostream>
#include <string>
#include <cstddef>
using namespace std;

int main(int argc, char const *argv[])
{

	string path("/home/xxx/share");
	auto  pos = path.find_last_of('/');
	auto file = path.substr(pos+1); // 

	cout << "file: "<<file << endl;


	return 0;
}

输出结果
在这里插入图片描述

操作

clear:清除内容(公开成员函数)
insert:插入字符(公开成员函数)
erase:移除字符(公开成员函数)
push_back:后附字符到结尾(公开成员函数)
pop_back:移除末尾字符(公开成员函数)
append:后附字符到结尾(公开成员函数)
compare:比较二个字符串(公开成员函数)
replace:替换字符串的指定部分(公开成员函数)
substr:返回子串(公开成员函数)
copy:复制字符(公开成员函数)
resize:更改存储的字符数(公开成员函数)
swap:交换内容(公开成员函数)

insert

string (1)	string& insert (size_t pos, const string& str);

substring (2)	string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);

c-string (3)	 string& insert (size_t pos, const char* s);

buffer (4)	string& insert (size_t pos, const char* s, size_t n);

fill (5)	string& insert (size_t pos, size_t n, char c);

void insert (iterator p, size_t n, char c);

single character (6)	iterator insert (iterator p, char c);

range (7)	template <class InputIterator>
void insert (iterator p, InputIterator first, InputIterator last);

在pos(或p)表示的字符之前插入额外的字符,

举例

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str1 = "to be guestion.";
	string str2 = "the ";

	str1.insert(6,str2);
	cout << str1 << endl;

	return 0;
}

输出结果
在这里插入图片描述

erase


(1)	 string& erase (size_t pos = 0, size_t len = npos);
(2)	iterator erase (iterator p);
(3)	iterator erase (iterator first, iterator last);

参数
pos - 要移除的首个字符
len - 要移除的字符数
p - 指向要移除的字符的迭代器
first, last - 要移除的字符范围

从 string 移除指定的字符。

举例

#include <iostream>
#include <algorithm> //find 
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str1 ("Cats and dogs are not good friends.");

	cout <<"before: ";
	cout << str1<<endl;
	
	 str1.erase(0,5); // 擦除 ' '
	cout <<"after: ";
	cout << str1 << endl;

	return 0;
}

输出结果
在这里插入图片描述

push_back

void push_back (char c);

将字符c追加到字符串末尾,将其长度增加1。

举例

#include <iostream>
#include <algorithm> //find 
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str;

	ifstream file("my_file.txt"); //逐字符读取整个文件

	if (file)
	{
		while (!file.eof())
		{
			str.push_back(file.get()); //使用push_back将每个字符附加到字符串对象。
		}

	}

	cout << str << endl;

	return 0;
}

输出结果
在这里插入图片描述

append

string (1)	string& append (const string& str);
substring (2)	string& append (const string& str, size_t subpos, size_t sublen);
c-string (3)	string& append (const char* s);
buffer (4)	string& append (const char* s, size_t n);
fill (5)	string& append (size_t n, char c);
range (6)	template <class InputIterator>
string& append (InputIterator first, InputIterator last);

通过在字符串当前值的末尾附加额外字符来扩展字符串:

(1) string
附加str的副本。
(2) substring
附加str子字符串的副本。子字符串是str的一部分,从字符位置的子字符串开始,跨越子字符串(或者直到str的结尾,如果str太短或者子字符串是string::npos)。
(3) c-string
附加由s指向的以null结尾的字符序列(C-string)组成的字符串的副本。
(4) buffer
在由s指向的字符数组中追加前n个字符的副本。
(5) fill
附加字符c的n个连续副本。
(6) range
以相同的顺序追加范围[first,last]中的字符序列的副本。
(7) initializer list
在il中以相同的顺序附加每个字符的副本。

举例

#include <iostream>
#include <algorithm> //find 
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str1;
	string str2("abcd");
	string str3("efgh");

	str1.append(str2);
	str1.append(str3);

	cout << str1 << endl;

	return 0;
}

输出结果
在这里插入图片描述

replace

string& replace (size_t pos,  size_t len,  const string& str);

用新内容替换以字符pos开头并跨越len字符(或字符串在[i1,i2]之间的部分)的部分.
举例

#include <iostream>
#include <algorithm> //find 
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str1("Cats and dogs are good friends.");

	str1.replace(0,4,"Lamd");

	cout <<str1<<endl;

	return 0;
}

输出结果
在这里插入图片描述

substr

string substr (size_t pos = 0, size_t len = npos) const;

返回一个新构造的字符串对象,其值初始化为该对象子字符串的副本。

举例

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{


	string str1 ("12345abcde");

	string str2;
	str2 = str1.substr(5);

	cout << str2<<endl;

	return 0;
}

输出结果
在这里插入图片描述

pop_back

void pop_back();
擦除字符串的最后一个字符,有效地将其长度减少1。

举例

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{

	string str1 ("What are you doing!");
	str1.pop_back();

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

输出结果
在这里插入图片描述
在这里插入图片描述

扫二维码关注微信公众号,获取技术干货


版权声明:本文为chen1415886044原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。