STL—string

举报
辰chen 发表于 2022/06/14 23:59:02 2022/06/14
【摘要】 文章目录 一、什么是string二、string的操作1.string的定义2.string中内容的访问(1)通过下标访问(2)通过迭代器访问 3.string中的函数(1)operator...


一、什么是string

我们在C语言中,存储字符数组是用char str[]来存放字符串,但是用这个存储的时候一般会显得麻烦,还容易产生一些错误,C++在STL中加入了string类型,使得操作起来更加的方便

要使用string,需要添加头文件#include <string>


二、string的操作

1.string的定义

定义string的方式跟基本类型相同:

string str;

  
 
  • 1

初始化:

string str = "abcd";

  
 
  • 1

2.string中内容的访问

(1)通过下标访问

可以像字符数组那样去访问string
str.length();为str的长度

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcd";
    
    for (int i = 0; i< str.length(); i ++ ) 
        cout << str[i];
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果为:abcd

这里再说一下输入输出

输入有两种方式,一种是不能读入空格cin,另一种是能读入空格getline(cin, str);,即如果用cin读入一个字符串的话,碰到空格就会停止读入,而getline(cin, str);则是碰到回车(换行)才会停止读入

输出的话可以用cout来输出,当然也可以通过printf进行输出,但是过程有点繁琐,这里不介绍

(2)通过迭代器访问

一般通过(1)就可以满足访问的要求,但是string中的某些函数,比如insert()erase()则要求用迭代器作为参数,所以我们还是要介绍一下迭代器的用法

定义一个迭代器

string::iteratoer it;

  
 
  • 1

通过上述定义我们就得到了迭代器it,我们就可以通过*it去访问string里的每一位

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcd";
    
    for (string::iterator it = str.begin(); it != str.end(); it ++ )
        cout << *it;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

当然我们也可以简写成:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcd";
    
    for (auto it = str.begin(); it != str.end(); it ++ )
        cout << *it;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果为:abcd

3.string中的函数

(1)operator+=

这是string的加法,可以把两个string直接拼接起来

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "abcd";
    string str2 = "efg";
    string str3 = str1 + str2;
    
    cout << str3;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:abcdefg

(2)compare operator

两个string类型可以直接使用==, !=, <, <=, >, >=比较大小,比较的规则是字典序

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "abcd";
    string str2 = "efg";
    
    if (str1 > str2) cout << "str1 > str2";
    else if (str1 == str2) cout << "str1 == str2";
    else cout << "str1 > str2";
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输出结果为:str1 > str2

(3)length()/size()

length()返回string的长度,即存放的字符数,时间复杂度为O(1),size()length()的用法基本相同

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "abcd";
    string str2 = "efg";
    
    cout << str1.size() << ' ' << str2.length();
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果为:4 3

(4)insert()

这里介绍insert()的两种写法
insert(pos, string)pos号的位置插入字符串string

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "abcd";
    string str2 = "efg";
    
    str1.insert(2, str2);
    //在str1[2]处插入str2
    cout << str1;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输出结果为:abefgcd

insert(it, it2, it3) it为原字符串的欲插入位置,it2it3为待插入字符串的首位迭代器,用来表示[it2, it3)将被插在it的位置上

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "abcd";
    string str2 = "efg";
    
    str1.insert(str1.begin() + 2, str2.begin(), str2.end());
    
    cout << str1;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输出结果为:abefgcd

(5)erase()

str.erase();有两种用法,删除单个元素,删除一个区间内的所有元素,时间复杂度均为O(N)
删除单个元素
str.erase(it);用于删除单个元素,it为需要删除的元素的迭代器

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    
    str.erase(str.begin() + 3);
    //删除str[3],即'd'
    cout << str;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:abcefg

删除一个区间内的所有元素
str.erase(first, last);,其中first为需要删除的区间的起始迭代器,last则为需要删除的区间的末尾迭代器的下一个地址,即删除[first, last)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    
    str.erase(str.begin() + 3, str.end());
    //删除str[3] ~ str[6]
    cout << str;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:abc
str.erase(pos, length);其中pos为需要开始删除的起始位置,length为删除的字符个数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    
    str.erase(3, 4);
    //删除str[3] ~ str[6]
    cout << str;
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:abc

(6)clear()

str.clear();用来清空string中的数据,时间复杂度一般为O(1)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    
    str.clear();
    
    cout << str.size();
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:0

(7)substr()

str.substr(pos, len);用来返回从pos号位开始,长度为len的子串,时间复杂度为O(len)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    
    cout << str.substr(3, 4);
        
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出结果为:defg

(8)string::npos

string::npos是一个常数,其本身的值为-1或者等于4294967295,两个值都被认为是正确的

#include <iostream>
#include <string>

using namespace std;

int main()
{
    if (string::npos == -1) 
        cout << "true" << endl;
    
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果为:true

(9)find()

str.find(str2),当str2str的子串时,返回其在str中第一次出现的位置,如果str2不是str的子串,返回string::npos
str.find(str2, pos),从strpos号位开始匹配str2,返回值和上述相同
时间复杂度为O(nm),其中n和m分别是strstr2的长度

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "chen must like C++";
    string str1 = "like";
    string str2 = "C++";
    string str3 = "not";
    
    if (str.find(str1) != string::npos) 
        cout << str.find(str1) << endl;
    if (str.find(str2) != string::npos) 
        cout << str.find(str2) << endl; 
    if (str.find(str3) != string::npos) 
        cout << str.find(str3) << endl;
    else cout << "Wrong";
    
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

输出结果为:
10
15
Wrong

(10)replace()

str.replace(pos, len, str2);strpos号位开始,长度为len的子串替换为str2
str.replace(it1, it2, str2)str的迭代器[it1, it2)范围的子串替换为str2
时间复杂度为O(str.length())

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "chen must like C++";
    string str2 = "algorithm";
    string str3 = "math";
    
    cout << str.replace(15, 3, str2) << endl;
    cout << str.replace(str.begin() + 15, str.end(), str3);
    
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

输出结果为:
chen must like algorithm
chen must like math

文章来源: chen-ac.blog.csdn.net,作者:辰chen,版权归原作者所有,如需转载,请联系作者。

原文链接:chen-ac.blog.csdn.net/article/details/116417774

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。