C++字符串处理总结

C++字符串处理有最原始的char以及string两种方式

1.给出一个string字符串,统计里面出现的字符的个数

解决方案:使用算法库里面的count函数,使用方法是count(begin,end,‘a’),其中begin指的是起始地址,end指的是结束地址,第三个参数指的是需要查找的字符。

2.截取、拼接字符串

substr append(插入位置,字符串)
###3.字符串中不同字符的个数
用set容器去重 统计set大小

4.字符串读入int

string res;
res+=n+'0';

5.整形转string

to_string 库函数

6.找出字符串中第一个只出现一次的字符

若没有,则输出-1
可设置标志 bool flag=false;找到则置为true 若循环结束仍为false 则输出-1;

7.find函数

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find (charT c, size_type pos = 0) const noexcept;
    string st1("babbabab");
    cout << st1.find('a') << endl;//1   由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找
    cout << st1.find('a', 0) << endl;//1
    cout << st1.find('a', 1) << endl;//1   
    cout << st1.find('a', 2) << endl;//4   在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
    cout << st1.rfind('a',7) << endl;//6   关于rfind,后面讲述
    cout << st1.find('c', 0) << endl;//4294967295
    cout << (st1.find('c', 0) == -1) << endl;//1
    cout << (st1.find('c', 0) == 4294967295) << endl;//1   两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
    cout << st1.find('a', 100) << endl;//4294967295   当查找的起始位置超出字符串长度时,按查找失败处理,返回npos
    //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept;
    string st2("aabcbcabcbabcc");
    string str1("abc");
    cout << st2.find(str1, 2) << endl;//6   从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos
    //测试size_type find (const charT* s, size_type pos = 0) const;
    cout << st2.find("abc", 2) << endl; //6   同上,只不过参数不是string而是char*
    //测试size_type find (const charT* s, size_type pos, size_type n) const;
    cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
    cout << st2.find("abcbc", 0, 5) << endl;//1   相当于st2.find("abcbc", 0)
    cout << st2.find("abcbc", 0, 6) << endl;//4294967295   第3个参数超出第1个参数的长度时,返回npos
    return 0;
}

8.非负字符串大整数相加

    int carry=0;//进位
    for(int i=nums1.size()-1,j=nums2.size()-1;i>=0||j>=0;i--,j--)
    {
        int sum=carry;
        sum+=(i>=0)?nums1[i]-'0':0;
        sum += (j >= 0) ? num2[j] - '0' : 0;
        ret.insert(ret.begin(), '0' + sum % 10);
        carry = sum / 10;

    }
    if(carry==1)
        ret.insert(ret.begin(),'1');

![图片说明](https://uploadfiles.nowcoder.com/images/20200517/122139957_1589702304615_D20DD759984AA66A3ED9BE1CDC6197BB "图片标题")

![图片说明](https://uploadfiles.nowcoder.com/images/20200517/122139957_1589702400596_FAB49843E434E35FE1A9217961D4A4B0 "图片标题")

![图片说明](https://uploadfiles.nowcoder.com/images/20200517/122139957_1589702474040_831EFE02D27363DAA2CD19B8310D4017 "图片标题")

![图片说明](https://uploadfiles.nowcoder.com/images/20200517/122139957_1589702517954_324BF6533215CD1239EB7013AC86F37B "图片标题")

9.输出质数因子

while (input != 1)
        {
            for (int i = 2; i <= input; i++)
            {
                if (input % i == 0)
                {
                    input /= i;
                    cout << i << ' ';
                    break;
                }
            }
        }

10.分割字符串

vector<string> testSplit11(const string& in, const string& delim)
{
    vector<string> ret;
    try
    {
        regex re{ delim };
        return vector<string>{
            sregex_token_iterator(in.begin(), in.end(), re, -1),
                sregex_token_iterator()
        };
    }
    catch (const std::exception& e)
    {
        cout << "error:" << e.what() << std::endl;
    }
    return ret;
}

void SplitString(const std::string& s, std::vectorstd::string& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));

    pos1 = pos2 + c.size();
    pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
    v.push_back(s.substr(pos1));

}

std::vector<std::string> split(std::string str, std::string pattern)
{
    std::string::size_type pos;
    std::vector<std::string> result;

    str += pattern;//扩展字符串以方便操作
    int size = str.size();

    for (int i = 0; i<size; i++)
    {
        pos = str.find(pattern, i);
        if (pos<size)
        {
            std::string s = str.substr(i, pos - i);
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}
int main()
{
    std::string str = "C:/Users/EHome_LJG/Desktop/images/3.jpg";
    std::string pattern = ".";

    std::vector<std::string> result = split(str, pattern);
    std::cout << "The result:" << std::endl;
    for (int i = 0; i<result.size(); i++)
    {
        std::cout << result[i] << std::endl;
    }

    return 0;
}
全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务