C++ string标准库的简单总结

C++字符串操作详解
本文详细介绍了C++标准库中的string类的各种操作方法,包括构造函数、比较操作符、长度获取、迭代器获取、字符串转换、子串提取等功能,并提供了丰富的示例代码。

1.构造函数

语法

  string();
  string( size_type length, char ch );
  string( const char *str );
  string( const char *str, size_type length );
  string( string &str, size_type index, size_type length );
  string( input_iterator start, input_iterator end );

例如:

string str1="abcd";
string str2("abcd");
string str3=(str2,1,3);//str3="bcd"

2. 操作符

语法

  ==    
  >
  <
  >=
  <=
  !=
  +
  +=
  []

例如:

string str1="abcd";
string str2="defg";
string str3=str1;
string str4=str1+str2;

if(str3==str1)  cout<<"true";  //true
if(str2>str1)   cout<<"true";  //true

//输出str4
//法一
cout<<str4;
//法二
for(int i=0;i<str4.length();i++)
{
    cout<<str4[i];
}

可以用==,>,<,>=,<=,!=比较两个字符串。可以用+,+=连接两个字符串。可以用[]来取特定的字符,就像数组一样。

3. length()函数

语法

 size_type length();

例如:

string str="abcd";
cout<<str.length(); //4

length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同.

4. size()函数

语法

 size_type size();

例如:

string str="abcd";
cout<<str.size(); //4

size()函数返回字符串中现在拥有的字符数。
注:size()和length()没有任何区别。

5. begin()函数

语法

iterator begin();

begin()函数返回一个迭代器,指向字符串的第一个元素.

6. end()函数

语法

 iterator end();

end()函数返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置’\0’).

7. c_str()函数

语法

const char *c_str();

例如:

//将string字符串转换为字符串数组
#include<string.h>  //调用strcpy()函数所需头文件
char c[20];
string str="abcd";
strcpy(c,str.c_str());

c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.

注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针

8. data()函数

语法

const char *data();

data()函数返回指向自己的第一个字符的指针.

9. compare()函数

语法

int compare( const basic_string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const basic_string &str );
int compare( size_type index, size_type length, const basic_string &str, size_type index2,
size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );

例如:

string str1="this is a nice day!";
string str2="nice";
string str3="it is nice!";
char s[]="nice";
cout<<str1.compare(str2)<<endl;   //1
cout<<str2.compare(s)<<endl;       //0
cout<<str1.compare(10,4,str2)<<endl;  //0
cout<<str1.compare(10,4,str3,6,4);    //0

compare()函数以多种方式比较本字符串和str

返回值情况
小于零this< str
this=str
大于零this>str

不同的函数:

  • 比较自己和string类的str
  • 比较自己和指针类型的str,例如:字符串数组
  • 比较自己的子串和str,子串以index索引开始,长度为length
  • 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

9、copy()函数

语法

size_type copy( char *str, size_type num, size_type index );

例如:

string str1="this is a nice day!";
char str2[20];
str1.copy(str2,4,10);
//输出
for(int i=0;i<4;i++)
{
    cout<<str2[i];
}

copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数

10. empty()函数

语法

bool empty();

例如:

string str="abc";
str.empty();  //false

如果字符串为空则empty()返回真(true),否则返回假(false).

11. max_size()函数

语法

 size_type max_size();

例如:

string str;
str.max_size(); //输出string字符串能包含的最大字符数

max_size()函数返回字符串能保存的最大字符数。

12. resize()函数

语法

void resize( size_type num );
void resize( size_type num, char ch );

例如:

string str="success";
str.length();  //  7
str.resize(20);
//str.resize(20,'#');  
str.length();  //20

resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。

13. substr()函数

语法

basic_string substr( size_type index, size_type num = npos );

例如:

string str1="it is a nice day";
string str2=str1.substr(8,4); //str2="nice";

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

14. swap()函数

语法

void swap( basic_string &str );

例如:

string str1="good";
string str2="great";
str1.swap(str2);//str1="great";str2="good";

swap()函数把str和本字符串交换。

对于c++中string类中是否要包含#include <string>头文件的问题。首先#include<iostream>头文件中包含了对string头文件的引用,所以不加编译器也不会报错,不过这与编译器有关,所以在引用string类时,最好加上#include<string>头文件,这样使代码清晰明了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值