这篇文章主要介绍了C++的系统String类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了C++的系统String类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了C++的系统String类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
除了使用字符数组来处理字符串以外,c++引入了字符串类型。可以定义字符串变量。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str;
str = "china";
string str2 = " is great ";
string str3 = str2;
cout<<str<<str2<<endl<<str3<<endl;
return 0;
}

cout<<"sizeof(string) = "<<sizeof(string)<<endl; cout<<"sizeof(str) = "<<sizeof(str)<<endl;

string str3 = str
string combine = str + str2; cout<<combine<<endl;
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string s1 = "abcdeg";
string s2 = "12345";
if(s1>s2)
cout<<"s1>s2"<<endl;
else
cout<<"s1<s2"<<endl;
string s3 = s1+s2;
cout<<s3<<endl;
return 0;
}

char & operator[](int n) ;
int size();
char *c_str();
int find(char c, int pos = 0); int find(char * s, int pos = 0); //返回下标值,没有找到返回-1,默认从 0 下标开
string &erase(int idx=0,int n = npos); //作用是删除从 idx 开始,往后数 n 位的字符串
void swap(stirng &s2);
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string sArray[10] = {
"0",
"1",
"22",
"333",
"4444",
"55555",
"666666",
"7777777",
"88888888",
"999999999",
};
for(int i=0; i<10; i++)
{
cout<<sArray[i]<<endl;
}
return 0;
}
string 数组是高效的,如果用二维数组来存入字符串数组的话,则容易浪费空间,此时列数是由最长的字符串决定。如果用二级指针申请堆空间,依据大小申请相应的空间,虽然解决了内存浪费的问题,但是操作麻烦。用 string 数组存储,字符串数组的话,效率即高又灵活。
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注米米素材网的更多内容!
原文链接:https://blog.csdn.net/qq_43414070/article/details/121029180
发表评论