字符串
字符串是最常用的一种数据类型了,在python中声明字符串和声明其他类型的数据一样,都非常的简单。但是在c++中,对于字符串的操作,相对来说要稍微复杂一些。
C++ 提供了以下两种类型的字符串表示形式:
-
C 风格字符串
-
C++ 引入的 string 类类型
C风格字符串
C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 \0 终止的一维字符数组。如下面的声明和初始化创建了一个 “Hello” 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 “Hello” 的字符数多一个。
int main(){
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
//可以简写成:
char greeting2[] = "Hello";
return 0 ;
}
c风格字符串常见操作
1.遍历字符串
#include<iostream>
int main(){
//最后总会跟着一个\0的空字符,此时括号中如果写长度,必须大于等于6
char name[] = "hello";
for (int i = 0; i < sizeof(name ) / sizeof(char); ++i) {
std::cout << name[i] << std::endl;
}
}
2.字符串的其他操作
C语言中提供了针对字符串操作的大量函数,不过在使用之前,需要先引入 #include
以下函数的使用,需要引入 #include<ctype.h> 头文件
拷贝、拼接字符串
#include <iostream>
int main(){
//拷贝字符串
char name[] = "hello";
char name2[6];
//参数一: 目标字符串, 参数二:源字符串
strcpy(name2 , name);
std::cout << name2 << std::endl;
//拼接字符串
strcat(name2 , " , 张三");
std::cout << name2 << std::endl;
// 返回字符串长度
int len = strlen(name2);
std::cout << "name2的长度:" << len << std::endl;
return 0 ;
}
C++风格字符串
C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。需要引入 #include ,由于string类声明在命名空间 std ,所以在使用的首要注意 命名空间的联合使用
#include <iostream>
//必须引入string库
#include <string>
using namespace std;
int mian(){
string s1;
string s2 {"北京"};
string s3{s2};
string s4 = "你好";
s1 = s3;
return 0 ;
}
C++风格字符串常见操作
1.拼接字符串
c++字符串的拼接跟Python一样,直接用+拼接即可。
#include <iostream>
#include<string>
using namespace std;
int main(){
string part1 {"c++"};
string part2 {" is a powerful"};
string sentence ;
sentence = part1 + part2 ;
cout << sentence << endl; // 结果是 c++ is a powerful
return 0 ;
}
2.获取指定位置的字符
可以使用[]和 at()操作字符串
#include <iostream>
#include<string>
using namespace std;
int main(){
string s1 {"i love c++"};
cout << s1[3]<<endl; // o
cout << s1.at(0) << endl; // i
return 0 ;
}
3.遍历字符串
#include <iostream>
#include<string>
using namespace std;
int main(){
string s1 {"abcdef"};
for(char s : s1){
cout << s << " "; // 打印的结果 :a b c d e f
}
cout << endl;
for(int s : s1){
// 因为s1是字符串类型,指定的s又为int类型,所以这里面打印出来是字符所对应的ascii码
cout << s << " "; // 打印的结果:97 98 99 100 101 102
}
cout << endl;
return 0 ;
}
4.字符串比较
字符串也是可以比较大小的。
#include <iostream>
#include<string>
using namespace std;
int main(){
string s1{"Apple"};
string s2{"Banana"};
string s3 {"kiwi"};
string s3 {"apple"};
string s3 {s1};
s1 == s5 // true
s1 == s2 // false
s1 != s2 // true
s1 < s2 // True
s1 > s2 // false
s1 == "Apple" // false
return 0 ;
}
5.截取字符串
#include <iostream>
#include<string>
using namespace std;
int main(){
substr(开始索引, 截取长度);
string s1 {"This is a test"};
cout << s1.substr(0 , 4) ; // This
return 0 ;
}
6.获取字符(字符串)在字符串中的索引
#include <iostream>
#include<string>
using namespace std;
int main(){
find(搜索的字符)
string s1 {"This is a test"};
cout << s1.find("This") ; // 0
cout << s1.find("is") ; // 2
cout << s1.find("test") ; // 10
return 0 ;
}
7.获取字符串长度
length() : 返回字符串长度
size():返回字符串长度
#include <iostream>
#include<string>
using namespace std;
int main(){
string s1 {"abcd"};
cout << "s1的字符串长度为" << s1.length() << endl; // 4
cout << "s1的字符串长度为" << s1.size() << endl; // 4
return 0 ; }
C++中string类的size和length到底有没有区别?
这是c++标准库中的string中length和size的源码
_NODISCARD size_type length() const noexcept { // return length of sequence
return _Get_data()._Mysize;
}
_NODISCARD size_type size() const noexcept { // return length of sequence
return _Get_data()._Mysize;
}
由此可以看出,length()与size()没有区别,都是返回string对象中元素数量,即返回std::distance(begin(),end()) 。length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。