C++入门:字符串

C++ 提供了以下两种类型的字符串表示形式:

  • C 风格字符串

  • C++ 引入的 string 类类型

string类提供了一系列针对字符串的操作,比如:

1. append() -- 在字符串的末尾添加字符

2. find() -- 在字符串中查找字符串

4. insert() -- 插入字符

5. length() -- 返回字符串的长度

6. replace() -- 替换字符串

7. substr() -- 返回某个子字符串

8. ...

下面是关于string类的实例:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //定义一个string类对象
    string http = "www.runoob.com";

   //打印字符串长度
   cout<<http.length()<<endl;

    //拼接
    http.append("/C++");
    cout<<http<<endl; //打印结果为:www.runoob.com/C++

    //删除
    int pos = http.find("/C++"); //查找"C++"在字符串中的位置
    cout<<pos<<endl;
    http.replace(pos, 4, "");   //从位置pos开始,之后的4个字符替换为空,即删除
    cout<<http<<endl;

    //找子串runoob
    int first = http.find_first_of("."); //从头开始寻找字符'.'的位置
    int last = http.find_last_of(".");   //从尾开始寻找字符'.'的位置
    cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并打印

    return 0;
}

之前一直搞不清 sizeof 和 strlen 到底该怎么区分,最近查了资料:

1、sizeof 操作符的结果类型是 size_t,它在头文件中 typedef 为 unsigned int 类型。该类型保证能容纳实现所建立的最大对象的字节大小。

2、sizeof 是运算符,strlen 是函数。

3、sizeof 可以用类型做参数,strlen 只能用 char* 做参数,且必须是以 \0 结尾的。

sizeof 还可以用函数做参数,比如:

short f();
printf("%d\n", sizeof(f()));

输出的结果是 sizeof(short),即 2。

4、数组做 sizeof 的参数不退化,传递给 strlen 就退化为指针了。

5、大部分编译程序在编译的时候就把 sizeof 计算过了,是类型或是变量的长度,这就是 sizeof(x) 可以用来定义数组维数的原因。

char str[20]="0123456789";
int a=strlen(str); // a=10;
int b=sizeof(str); // 而 b=20;

6、strlen 的结果要在运行的时候才能计算出来,是用来计算字符串的长度,不是类型占内存的大小。

7、sizeof 后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为 sizeof 是个操作符不是个函数。

8、当适用一个结构类型或变量时, sizeof 返回实际的大小;当适用一静态地空间数组, sizeof 归还全部数组的尺寸;sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸。

数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址, 如:

fun(char [8])
fun(char [])

都等价于

fun(char *) 

在 C++ 里参数传递数组永远都是传递指向数组首元素的指针,编译器不知道数组的大小。

如果想在函数内知道数组的大小, 需要这样做:

进入函数后用memcpy拷贝出来,长度由另一个形参传进去

fun(unsiged char *p1, int len)
{
    unsigned char* buf = new unsigned char[len+1]
    memcpy(buf, p1, len);
}

看了上面的详细解释,发现两者的使用还是有区别的,从这个例子可以看得很清楚:

char str[20]="0123456789";
int a=strlen(str);         // a=10; >>>> strlen 计算字符串的长度,以结束符 0x00 为字符串结束。
int b=sizeof(str);         // 而 b=20; >>>> sizeof 计算的则是分配的数组 str[20] 所占的内存空间的大小,不受里面存储的内容改变。  

上面是对静态数组处理的结果,如果是对指针,结果就不一样了。

char* ss = "0123456789";
sizeof(ss) 结果 4 ===》ss 是指向字符串常量的字符指针,sizeof 获得的是一个指针的之所占的空间,应该是长整型的,所以是 4。
sizeof(*ss) 结果 1 ===》*ss 是第一个字符 其实就是获得了字符串的第一位 '0' 所占的内存空间,是 char 类型的,占了 1 位
strlen(ss)= 10      ===》 如果要获得这个字符串的长度,则一定要使用 strlen。strlen 用来求字符串的长度;而 sizeof 是用来求指定变量或者变量类型等所占内存大小。

<cstring> 创建详解

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{

    //1. 字符串的创建
    cout<<"第一:字符串的创建!\n\n";
    string a(4,'a');
    cout<<"1.以 a 为原字符 4单位大小\n\n";
    cout<<"string a(4,'a');\ncout<<a<<endl;\n";
    cout<<a<<endl<<endl;

    cout<<"2.任意大小的字符串\n\n";
    cout<<"string b(\"bbbbbb\");\ncout<<b<<endl;\n";
    string b("bbbbbb");
    cout<<b<<endl<<endl;

    cout<<"3.把某一字符串的某一部分\n(0位置开始4个长度)给c\n\n";
    cout<<"string c(a,0,4) ;\ncout<<c<<endl;\n";
    string c(a,0,4) ;
    cout<<c<<endl<<endl;

    cout<<"4. 10长度原长度不足补*;\n\n";
    cout<<"c.resize(10,'*');\ncout<<c<<endl;\n";
    c.resize(10,'*');
    cout<<c<<endl<<endl;

    system("pause");
    system("cls");
    return 0;
}

<cstring> assign() 、 copy() 详解:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{

    cout<<"第二: 字符串的赋值 assign();"<<endl;
    cout<<"1.感觉就像是append不过是抹除-覆盖\n";
    cout<<"string e;\nchar f[10]=\"123456\"\ne.assign(f);\ne+=' ';\ncout<<e<<endl<<endl;\n";
    string e;
    char f[10]="123456";
    e.assign(f);
    e+=' ';
    cout<<e<<endl<<endl;

    cout<<"2.string区间 赋值都类似吧\n";
    cout<<"e.assign(f,3,3);\ne+=' ';\ncout<<e<<endl<<endl;\ne.assign(f,3);\ncout<<e<<endl;\n";
    e.assign(f,3,3);
    e+=' ';
    cout<<e<<endl;
    e.assign(f,3);
    cout<<e<<endl<<endl;

    cout<<"3.某字符串char型 全部\n";
    cout<<"char ssr[10]=\"asdqwezxc\";\ne.assign(ssr);\ncout<<ssr<<endl;\n";
    char ssr[10]="asdqwezxc";
    e.assign(ssr);
    cout<<ssr<<endl<<endl;

    cout<<"4.某字符串char型 前num个\n";
    cout<<"e.assign(ssr,4);\ncout<<e<<endl;\n";
    e.assign(ssr,4);
    cout<<e<<endl<<endl;

    cout<<"5.某字符赋值\n";
    cout<<"赋值3个6\n";
    e.assign(3,'6');
    cout<<e<<endl<<endl;

    cout<<"copy() 将d中的2位置开始的12个字符覆盖到char型数组ss上\n 必须为-> char型 <-否则报错";
    cout<<" char ss[10]=\"123\";\n string dd;\nd.copy(ss,12,2);\ncout<<ss<<endl;\n";
    char ss[15]="123";
    string dd("abcdefghijklmn");
    dd.copy(ss,12,2);
    cout<<ss<<endl<<endl;

    system("pause");
    system("cls");
        return 0;
}

<cstring> append() 详解及其扩展(int, char):

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{   cout<<"第三: 字符串的添加与复制 append();\nstring d(a);\ncout<<d<<endl;\n//或者 d=d+a;/nd.append(b);\n";

    cout<<"1.在d的末尾添加字符串a\n\n";
    string d(a);
    d.append(b);
    cout<<d<<endl<<endl;

    cout<<"2.在d的末尾添加字符串/nb(0位置开始,2个长度)的数据\n\n";
    cout<<"d.append(b,0,2);\ncout<<d<<endl;\n";
    d.append(b,0,2);
    cout<<d<<endl<<endl;

    cout<<"3.添加4个 ~ 字符\n\n";
    cout<<"d.append(4,'~');\n";
    cout<<"cout<<d<<endl<<endl;\n";
    d.append(4,'~') ;
    cout<<d<<endl<<endl;

    system("pause");
    system("cls");

    cout<<"4. int 与 char 型添加 (好高兴自己想到的int ^_^ )\n";
    cout<<"char app[100]=\"aaabbb\";";
    cout<<"\nstring charr(\"-_-\");\n";
    cout<<"charr.append(app);\ncout<<charr<<endl\n";
    char app[100]="aaabbb";
    string charr("-_- ");
    charr.append(app);
    cout<<charr<<endl<<endl;

    cout<<"charr.append(app,4);\ncout<<charr<<endl<<endl;\n";
    charr.append(app,4);
    cout<<charr<<endl<<endl;

    cout<<"char型数组全部,char型数组的前4个\n\n****如果要添加中间***\n";
    cout<<"string tmp;\nstring tmp;tmp.assign(app);\ncharr.assign(\"\");\ncharr.append(tmp,1,4);\ncout<<charr<<endl<<endl;\n";
    string tmp;
    tmp.assign(app);
    charr.assign("");
    charr.append(tmp,1,4);
    cout<<charr<<endl<<endl;

    cout<<"5.int  double 等等 通过 sprintf() <cstdio>作为转接\n";
    cout<<"int aaa=15314;\ndouble bbb=3.1415;\nchar aa[10];\nsprintf(aa,\"%d\",aaa);\ncharr.append(aa,0,4);\nsprintf(aa,\"%f\",bbb);\ncharr.append(aa,0,4);\ncout<<charr<<endl;\n";
    int aaa=15314;
    double bbb=3.1415;
    char aa[10];
    sprintf(aa,"%d",aaa);
    charr.append(aa,0,4);
    sprintf(aa,"%f",bbb);
    charr.append(aa,0,4);
    cout<<charr<<endl<<endl;

    system("pause");
    system("cls");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰度少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值