string类笔记

basic_string (const basic_string& str);//拷贝构造
from c-string (4) basic_string (const charT* s, const allocator_type& alloc = allocator_type());//转换构造

basic_string (const charT* s, size_type n/*截取长度,从头取前n个,可控,不会超*/, const allocator_type& alloc = allocator_type());
//string("123",3)

basic_string (size_type n, charT c,/*这个n在前,把后面的字符*n个*/const allocator_type& alloc = allocator_type());
//string str(10,'a') 10个a

basic_string (const basic_string& str, size_type pos, size_type len = npos,/**/const allocator_type& alloc = allocator_type());
//string str(str,3/*从第3个开始*/,3/*截取3个*/)

template <class InputIterator> basic_string  (InputIterator first, InputIterator last,const allocator_type& alloc = locator_type());


string str("12345", 10);    //截取前10个
string str1(str, 3);        //从第3+1个元素开始取
string str4("12322435", 3); //从[3]开始取
string str2(str, 3, 3);     //从[3]开始取3个
string str3(str,3,str.length());//推荐

//返回迭代器,里面某一个元素
str.begin()
str.size()//返回当前string的length
str.length()//返回值同上
str.empty()//返回ture为空,false为不空
str.max_size()//当前能存放最大长度
str.capacity()//动态改变,不增加长度所能存放的最大值
str.resize(100,'a'/*可选*/)//size重新分配 不足的部分
str.length()//
string str("123456");
std::size_t pos=str.find('1');//返回找到的位置
//未找到返回unsinged最大值-1 
//string::npos 未找到
str.find("45");//全匹配
str.find("12",4);//从第4个开始查找
str.find("12348888",04);//从第0个开始查找,查找待查找字符前4个
str.rfind()//尾部开始查找

string str("12345678");
std::size_t pos=str.find_first_of(‘8’);//第一个是8的值
str.find_first_not_of('8');//第一个不是8的值
str.find_first_not_of("123456");//任意一个不在里面的

"      sdfsgsgsrg 132456789    rwergresg"
std::size_t pos_begin = str.find_first_of("1234567890");
pos_end = str.find_first_not_of("123456789",pos);
string strNum(str.begin()+pos_begin,str.begin()+pos_end);
//提取"123456789"
str.compare()//比较,比str1==str2强大
str.compare(str);//==
str.compare(pos_begin,pos_end-pos_begin,strNum);//从begin到end 与strNum比较 ==0表示查到
str.substr(begin,end-begin);//截取
str.swap(str);//交换
str+=//char *char 或string
str.append();//连接字符串
str.append(10,'m');//拼接10个m
str.assign();//赋值 相当于 +=
str.at();//有检测
    try
    {
        std::cout<<str.at(100);
    }
    catch (std::out_of_range ex)
    {
    }
str.insert();//pos 位置,subpos子字符串位置
str.erase(pos,npos);//从pos到npos
str.c_str();//c风格字符串带\0
str.data();//返回data指针(const char*),没有\0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值