首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++那些事儿6.0 STL中的string

c++那些事儿6.0 STL中的string

作者头像
热心的社会主义接班人
发布2018-04-27 13:04:43
6620
发布2018-04-27 13:04:43
举报
文章被收录于专栏:cscs

知识点综述

c++,string 类
   string 是C++中的字符串对象,是一种特殊类型的容器,专门设计来操作的字符序列。
         有容量:
            length()长度,max_size()最大长度
            capacity存储空间大小,empty()判断字符串时候为空
         访问元素:
            operator[]得到字符串的字符
            at() 得到字符串的字符
         修改:
            operator(+) 追加
            append  追加
            insert 插入
            replace 替换
         其它:
            find 查找
            substr 生成字符串
            compare 比较

  和java的string有许多相似的功能,java应该借鉴与c++。

代码展示


#include<iostream>
#include<string>
#include<algorithm>
#include <functional>
using namespace std;
int main() 
{
    string st;
      // empty()函数判断string 是否为空
    std::cout << st << " " << st.empty() << endl;
    // string(int n,char c) ,n个字符串初始化
    string st1(5, 'a');
    std::cout << st1 << endl;
    std::cout << endl << "---------------------" << endl;

    string str("logadflx"); // string构造函数。
    std::cout << str << endl;
    //const char &operator[](int n)const;
    std::cout <<"第一个元素"<< str[0] << endl; //下标运算符,访问单个字符。
    //const char &at(int n)const;
    std::cout << "第3个元素" << str.at(2) << endl;

/*  operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,
    当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
        */
    
    std::cout << "str的容量:" << str.capacity() << endl;
    //  看看越界情况
    
    try
    {
        str.at(16);
    }
    catch (const std::out_of_range& o)
    {
        std::cout << "第16个元素:OutIndexArray" << endl;
    }
    /* string也有append()函数,和java的StringBuilder,StringBufferappend()函数相似。
      append()有许多重载函数
    */
 char ch[] = { 'a','z','s','d','j','\0' };  //字符数组结尾加上'\0'不然后面会乱码。
 str.append(ch);
 std::cout <<"追加字符串数组后:"<< str << endl;
 /*
 string 也有compare()函数 ,比较字符串的大小
  compare()也有不少重载函数
 与java中 compareTo(String anotherString) 相似。

 */
 if (str.compare(st1)) {
     cout << "str字符串大于str1字符串。"<<endl;
 }
 else {
     cout << "str字符串小于于str1字符串。"<<endl;
 }

 /*
 string substr(int pos = 0,int n = npos) const
   substr()函数取子串,和java中 substring()差不多。
 */
 cout << "取2到5的子串:" << str.substr(1, 4) << endl;

 /*
 int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
 find()函数也有不少重载函数。
 与 indexOf(int ch, int fromIndex) 相似。

 */
 cout << "d出现的索引:" << str.find('d', 0)<<endl;

 /*
 string &replace(int p0, int n0,const char *s);
 //删除从p0开始的n0个字符,然后在p0处插入串s
   replace也有许多重载函数。

 */
 cout << str.replace(0, 4, "\0");
 cout << "str:" << str << endl;
    /* c++ 字符串也有length属性,可以for循环遍历。
    与java的length()一样。
    */
    std::cout <<"str对象中可存放的最大字符串的长度:"<< str.max_size() << endl;
    std::cout <<str<< "字符串的长度的长度:" <<str.length()<< endl;

    /*
    string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
    */
    cout << "在末尾插入3个6:" << str.insert(str.length(), 3,'6')<<endl;

    /*
    iterator erase(iterator first, iterator last);
    //删除[first,last)之间的所有字符,返回删除后迭代器的位置
    同样许多重载函数。
    */
    cout << "删除6:" << str.erase(9,11)<<endl;
    
    /*
    c_str() ,返回 const 类型的指针
    data() ,将内容以字符数组的形式返回
    与java中 bytes()差不多。
    */
    str.c_str(); //转成字符数组
    int i = 0;
    while (str.c_str()[i] != '\0') {
        cout << str.c_str()[i] <<" ";
        i++;
    }
    std::cout << endl << "--------- for循环遍历-----------" << endl;
    for (int i = 0; i < str.length(); i++)  //for循环遍历
        cout << str[i] << "  ";

    cout << endl << "---------iterator遍历遍历------------" << endl;
    string::iterator it; //采用迭代器遍历 iterator
    for (it = str.begin(); it < str.end(); it++)
    {
        cout << *it <<"  ";
    }
    cout << endl << "---------foreach遍历遍历------------" << endl;
    for (char c : str) {
        cout << c <<"  ";
    }
    cout <<endl<< "----------升幂排序-----------" << endl;
    sort(str.begin(), str.end());
    cout << str << endl;

    cout << endl << "---------------------" << endl;
//  sort(str.begin(), str.end(), greater<string>());
    cout << endl << "----------降幂排序-----------" << endl;
    cout << str.c_str() << endl;
    

    string str1 = "asfg";
    str.insert(1, str1);
    cout << str << endl;


    system("pause");
    return 0;
}

结果如下

结果.PNG
结果.PNG

参考了几篇博客,以及工具手册


工具手册,C/C++语言参考

C/C++语言参考部分string截图.PNG
C/C++语言参考部分string截图.PNG

c++的string功能其实也很强大,完全不比java差。后面的stl,和java的Collections也差不多。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • c++的string功能其实也很强大,完全不比java差。后面的stl,和java的Collections也差不多。
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档