C++之string定义、用法详解

版权声明:转载留名即可 ^_^ https://blog.csdn.net/qq_33375598/article/details/88218117

string用法

1,定义

string str;

初始化 ,可以直接给string类型的变量赋值;

string str = “abcd”;

⚠️:使用时,需要加上头文件

#include<string>

using namespace std;

2,string中内容访问

一般来说,可以直接像字符串数组一样取访问

示例:

#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcd";
  for (int i = 0; i < str.length(); ++i)
  {
    printf("%c", str[i]);
  }
  printf("\n");
  return 0;
}

如果需要读入和输出整个字符串,则只能用cin和cout

示例:

#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str;
  cin>> str;
  cout << str << endl;
  return 0;
}

⚠️:printf也可以输出string,使用c_str()将string类型转为字符串数组进行输出

示例:

#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcd";
  printf("%s\n", str.c_str());
  return 0;
}

2)通过迭代器访问

string不像其他STL容器那样需要参数,因此可以直接如下定义:

string::iterator it;

这样就得到了迭代器it,并且可以通过*it来访问string里的每一位

string和vetor一样,支持直接对迭代器进行减某个数字,如str.begin()+3的写法是可行的

#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcd";
  for (string::iterator it = str.begin(); it < str.end(); ++it)
  {
    printf("%c", *it);
  }
  printf("\n");
  return 0;
}

3)string常用函数实例解析

1)operator+=

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

示例:

#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str1 = "abcd", str2 = "efg", str3;
  str3 = str1 + str2;
  str1 += str2;
  cout<<str3<<endl;
  cout<<str1<<endl;
  return 0;
}

2)compare operator

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

#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
  if(str1 < str2) printf("OK1\n");
  if(str2 != str3) printf("OK2\n");
  if(str4 >= str3) printf("OK3\n");
  return 0;
}

3)length()/size()

length返回string的长度,即存放的字符数,时间复杂度为O(1)

size()和length()基本相同

⚠️:strlen()和sizeof()区别

sizeof()是运算符,其值在编译时即计算好了

获得保证能容纳实现所建立的最大对象的字节大小。

strlen()是函数,字符串的实际长度,它求得方法是从开始到遇到第一个'\0’。

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcd";
 // printf("%d %d\n", str.length(), str.size());
  cout<< str.size()<<" "<<str.length()<<endl;
  return 0;
}

4)insert()

下面几种写法时间复杂度为O(N)

a,insert(pos, string) 在pos号位置插入字符串string

示例:

  string str1 = "abcxyz", str2 = "def";

  str1.insert(3,str2);            

//往str1[3]里插入def,str2的位置也可以直接写成“def”

  printf("%s\n", str1.c_str());

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

示例:

#include<cstdio>
//#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str1 = "abcxyz", str2 = "def";
  //str1.insert(3,str2);            //往str1[3]里插入def,str2的位置也可以直接写成“def”
  str1.insert(str1.begin() + 3, str2.begin(), str2.end());
  printf("%s\n", str1.c_str());
  return 0;
}

5)erase()

erase()有两种用法,删除单个元素,删除一个区间内的所有元素,时间复杂度为O(n)

a,删除单个元素

str.erase(it)用于删除单个元素,it为需要删除的元素迭代器

b,删除一个区间内的所有元素

方法一:

str.erase(first,last),其中first为需要删除的区间的起始迭代器,而last为需要删除区间额末尾迭代器的下一个地址,也即为[first, last]

方法二:

str.erase(pos,length),其中pos为需要开始删除的其实位置,length为删除的字符个数

示例:

#include<cstdio>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcdefg";
  //删除单个元素
  //str.erase(str.begin()+4);     
  //删除区间内元素
  //str.erase(str.begin() + 2, str.end() - 1);
  str.erase(2,4);
  printf("%s\n", str.c_str());
  return 0;
}

6)clear()

作用:用以清空string中的数据,时间复杂度为O(1)

示例:

//#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "abcdefg";
  str.clear();
  cout<<str.size()<<endl;
  return 0;
}

7)substr()

格式

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

示例:

//#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
  string str = "Thank you for your smile.";
  cout<<str.substr(0,5)<<endl;
  cout<<str.substr(14,4)<<endl;
  cout<<str.substr(19,5)<<endl;
  return 0;
}

8)string::npos

string::npos是一个常数,但由于是unsign_int类型,因此实际上也可以认为是unsigned_int类型的最大值。

string::npos用作find函数失配时的返回值。

示例:

//#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
 if(string::npos == -1){
  cout<<"-1 is true!"<<endl;
 }
 if(string::npos == 4294967295){
  cout<<"4294967295 is also true!"<<endl;
 }
  return 0;
}

9)find()

str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置;如果str2不是str的子串,那么返回str::npos

str.find(str2,pos),从str的pos号位开始匹配str2,返回值与上相同。

时间复杂度为O(nscm),其中n和m分别为str和str2的长度。

示例:

//#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
 string str = "Thank you for your smile.";
 string str2 = "you";
 string str3 = "me";
 if(str.find(str2) != string::npos){
  cout<<str.find(str2)<<endl;
 }
 if(str.find(str2,7) != string::npos){
  cout<<str.find(str2,7)<<endl;
 }
 if(str.find(str3) != string::npos){
  cout<<str.find(str3)<<endl;
 }else{
  cout<<"No position!"<<endl;
 }
  return 0;
}

10)replace()

Str.replace(pos,len,str2)把str从pos号位开始、长度为len的子串替换为str2

str.replace(it1,it2,str2)把str的迭代器(it1,it2)范围的子串替换为str2

示例:

//#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
string str = "Maybe you will turn around.";
 string str2 = "will not";
 string str3 = "Surly";
cout<<str.replace(10,4,str2)<<endl;
cout<<str.replace(str.begin(),str.begin() + 5, str3)<<endl; 
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33375598/article/details/88218117