std:string的常用方法笔记

不等长比较字符串:string.compare

愿型 int compare(size_type __pos1, size_type __n1, const basic_string& __str,

size_type __pos2, size_type __n2)

unsigned char buf0[5] = {0xff, 0xfd, 1, 2, 3};

char *buf1 = new char[5];

memcpy(buf1, buf0, 5);

char buf2[] = {1, 2, 3};

std::string str2 = reinterpret_cast<const char *>(buf2);

int ret = str2.compare(0, 3, buf1, 2, 3); // 意思是取str2的0到3(不含下标3)也就是 1 2 3 与 buf1下标从2开始的3个升度对比

std::cout << ret << std::endl; / / 结果 ret = 0, 比较的结果相等。

compare 其它用法如下:

比较自己和str,

int compare( const basic_string &str );

int compare( const char *str );

比较自己的子串和str,子串以index索引开始,长度为length

int compare( size_type index, size_type length, const basic_string &str );

比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己

int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 );

比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

int compare( size_type index, size_type length, const char *str, size_type length2 );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值