【C++】string类常用函数用法总结

目录

常用函数一览

默认成员函数

与容量有关的函数

part 1

part 2

part 3

与访问和遍历有关的函数

与修改有关的函数

npos

与string相关的其它常用函数

常用非成员函数

getline和cin的区别


常用函数一览

//默认成员函数
string();

string(const char* s);

string(size_t n, char c);

string(const string& str);

//与容量有关的函数
size_t size() const;

size_t capacity() const;

void reserve(size_t n);

void resize(size_t n, char c);

void reserve(size_t n = 0);

bool empty() const;

void clear();

//与访问及遍历有关的操作
char& operator[] (size_t pos);

const char& operator[] (size_t pos) const;

iterator begin();

const_iterator begin() const;

iterator end();

const_iterator end() const;

// 与修改有关的操作
string& operator+= (const string& str);

string& operator+= (const char* s);

string& operator+= (char c);

string& append(const char* s);

void push_back(char c);

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

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

string& erase(size_t pos = 0, size_t len = npos);

//与string相关的其他常用函数
const char* c_str() const;

size_t find(char c, size_t pos = 0) const;

size_t rfind(char c, size_t pos = npos) const;

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

//常用非成员函数
istream& operator>> (istream& is, string& str);

ostream& operator<< (ostream& os, const string& str);

istream& getline(istream& is, string& str);

默认成员函数

1)string();//构造空的string类对象,即空的字符串

2)string(const char* s);//用C语言形式的字符串来构造string类对象

3)string(size_t n, char c);//用n个字符c来构造string类对象

4)string(const string& str); //用已有对象去构造新的对象

5)string& operator= (const string& str);//赋值重载

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

void test()
{
	string s1;
	string s2("hello world!");
	string s3(10, 'x');

	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;
	cout << "s3:" << s3 << endl;

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

    s1 = s2;
	cout << "s1:" << s1 << endl;
}

int main()
{
	test();
	return 0;
}

运行结果: 

与容量有关的函数

part 1

1)size_t size() const;

2)size_t capacity() const;

3)bool empty() const;

4)void clear();

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

void test()
{
	string s1("hello world!");
	cout << "size:" << s1.size() << endl;
	cout << "capacity:" << s1.capacity() << endl;

    cout << "empty:" << s1.empty() << endl;

	s1.clear();
	cout << "After clear check empty:" << s1.empty() << endl;
}

int main()
{
	test();
	return 0;
}

运行结果:

part 2

1)void resize(size_t n);

2)void resize(size_t n, char c);

 resize(size_t n)和resize(size_t n, char c)都是将字符串的有效个数改变到n个,不同的是,当使用 std::string::resize(size_t n) 来增加字符串的大小时,新增加的字符默认是用空字符(null character,即 \0)来填充,resize(size_t n, char c)则用字符c来填充多出来的元素空间。

当n小于原来字符串的个数时,相当于删除操作,字符串将保留n个字符。

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

void test()
{
	string s1("hello world!");
	s1.resize(20);
	cout << "s1:" << s1 << endl;

	string s2("welcom to C++");
	s2.resize(20, 'x');
	cout << "s2:" << s2 << endl;

	s1.resize(5);
	cout << "s1:" << s1 << endl;
}

int main()
{
	test();
	return 0;
}

运行结果:

part 3

void reserve(size_t n = 0);

 std::string::reserve(size_t n)函数用于请求字符串分配足够的内存空间来存储至少n个字符(不包括终止的空字符'\0')。这并不意味着字符串的大小(即其中包含的字符数)会增加,它仅仅是预先分配了内存,以避免在未来添加字符时频繁地重新分配内存。这样做可以提高性能,因为内存分配通常是一个昂贵的操作。

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

void test()
{
	string s1("hello");
	cout << "s1.size:" << s1.size() << endl;
	cout << "s1.capacity:" << s1.capacity() << endl;

	s1.reserve(20);
	cout << "After reserve s1.size:" << s1.size() << endl;
	cout << "After reserve s1.capacity:" << s1.capacity() << endl;
}

int main()
{
	test();
	return 0;
}

运行结果:

reserve之后的capacity为31,是因为vs自己的机制导致的,不同的编译器结果可能会不一样。

与访问和遍历有关的函数

1)char& operator[] (size_t pos);

2)const char& operator[] (size_t pos) const;

3)iterator begin();

4)const_iterator begin() const;

5)iterator end();

6)const_iterator end() const;

 const char& operator[] (size_t pos) const,const_iterator begin() const,const_iterator end() const,const修饰*this,即this指针指向的内容,所以this指针指向的内容不可修改,用法上和其他函数是一样的。

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

void test()
{
	string s1("hello world!");
	cout << s1[2] << endl;//类似数组的下标访问

	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << ' ';
		++it;
	}
	cout << endl;
}
int main()
{
	test();
	return 0;
}

运行结果: 

与修改有关的函数

1)string& operator+= (const string& str);

2)string& operator+= (const char* s);

3)string& operator+= (char c);

4)string& append(const char* s);

5)void push_back(char c);

6)string& insert(size_t pos, const char* s);

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

8)string& erase(size_t pos = 0, size_t len = npos);

这里列举了较多的函数,但其实用法都大致相同,很容易做到举一反三,所以这里就不一一举例了。

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

void test()
{
	string s1("hello ");
	cout << "s1:" << s1 << endl;
	s1 += "world";
	cout << "s1 after +=: " << s1 << endl;

	cout << endl;

	string s2("Urspeacil");
	cout << "s2: " << s2 << endl;
	s2.append("chenyilan");
	cout << "s2 after append: " << s2 << endl;

	cout << endl;

	string s3("yyds");
	cout << "s3: " << s3 << endl;
	s3.push_back('y');
	cout << "s3 after push_back: " << s3 << endl;

	cout << endl;

	string s4("world peace");
	cout << "s4: " << s4 << endl;
	s4.insert(0, "I hope ");
	cout << "s4 after insert: " << s4 << endl;

	cout << endl;

	string s5("boy");
	cout << "s5: " << s5 << endl;
	s5.insert(1, 1, 'o');
	cout << "s5 after insert: " << s5 << endl;

	cout << endl;

	string s6("I love you!");
	cout << "s6: " << s6 << endl;
	s6.erase(6);
	cout << "s6 after erase: " << s6 << endl;
}
int main()
{
	test();
	return 0;
}

 运行结果:

npos

npos是一个静态成员常量,表示一个不可能的位置。

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

int main()
{
	cout << "npos: " << string::npos << endl;
	return 0;
}

 运行结果:

string& erase(size_t pos = 0, size_t len = npos)上面讲的的这个函数,形参len就给了npos为缺省值(默认值),表示如果不指定形参len,那么将删除pos位置到字符串结尾的字符。

与string相关的其它常用函数

1)const char* c_str() const;

2)size_t find(char c, size_t pos = 0) const;

3)size_t rfind(char c, size_t pos = npos) const;

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

c_str() ,它主要用于将字符串类型的字符数组(包括C++字符串和C字符数组)转换为C类型字符串,即以'\0'结尾的字符数组。

substr(),用于从字符串中提取子串。其函数原型为:std::string substr (size_t pos = 0, size_t len = npos) const;,其中pos是子串的起始位置,len是子串的长度。如果len为std::string::npos或省略该参数,则子串将延续到原始字符串的末尾。

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

void test()
{
	string s1("hello world");
	cout << "c_str: " << s1.c_str() << endl;

	//找s1中的第一次出现的o并返回下标,可以用find()正着遍历s1
	cout << "find: " << s1.find('o') << endl;

	//找s1中的最后出现的o并返回下标,可以用rfind()反着遍历s1
	cout << "rfind: " << s1.rfind('o') << endl;

	string s2 = s1.substr(0, 5);
	cout << "s2: " << s2 << endl;
}

int main()
{
	test();
	return 0;
}

运行结果:

常用非成员函数

1)istream& operator>> (istream& is, string& str);//流提取

2)ostream& operator<< (ostream& os, const string& str);//流插入

3)istream& getline(istream& is, string& str);

 流插入和流提取的使用很简单,这里就不赘述了,讨论getline和cin的区别更有意义。

getline和cin的区别

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

void test()
{
	string str;
	cin >> str;//输入world peace
	cout << str;
}
int main()
{
	test();
	return 0;
}

运行结果:

可以看到,peace并未被读取。原因在于:cin在遇到空白字符(空格、制表符、换行符)时会停止读取,导致空白字符后面的内容无法读取。

下面看看getline的表现:

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

void test()
{
	string str;
	getline(cin, str);//输入world peace
	cout << str;
}
int main()
{
	test();
	return 0;
}

运行结果:

可以看到,getline可以读取到空格后面的内容,原因在于getline一次从输入流中读取一行,直到遇到换行符(\n)才停止读取。


完~ 

  • 11
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++string提供了许多常用函数。其中一些函数包括: 1. c_str():将string字符串转换为C风格的字符串,并返回该字符串的const指针(const char*)。这个函数可以用于将const string转换为const char*。例如: ```cpp string s1("1234567"); const char* s2 = s1.c_str(); ``` 2. operator\[\]和at():这两个函数都用于访问string中的字符。operator\[\]使用索引来访问字符,而at()使用位置来访问字符。例如: ```cpp string str = "abcdefg"; char c1 = str\[0\]; // 使用operator\[\] char c2 = str.at(1); // 使用at() ``` 3. to_string():将数值转换为对应的字符串。这个函数可以将整数、浮点数等数值型转换为string型。例如: ```cpp int a = 4; double b = 3.14; string str1 = to_string(a); string str2 = to_string(b); ``` 4. 构造函数string提供了多个构造函数,用于创建string对象。其中包括默认构造函数、使用字符常量构造、拷贝构造和数量*字符构造等。例如: ```cpp const char* str = "Hello World"; string s1; // 默认构造 string s2(str); // 使用字符常量构造 string s3("hello World"); // 同上 string s4(s2); // 拷贝构造 string s5(10, 'a'); // 数量*字符 ``` 这些是C++ string的一些常用函数,可以帮助你在处理字符串时更加方便和灵活。 #### 引用[.reference_title] - *1* [C++ string常用函数用法总结](https://blog.csdn.net/qq_61514490/article/details/126165076)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C++String常用函数总结](https://blog.csdn.net/weixin_51954217/article/details/127990342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值