zoukankan      html  css  js  c++  java
  • C++primer_顺序容器之string操作

    本例以代码形式整理了C++primer第九章string操作的基本函数方法,其中每个函数 的作用在代码注释中解释到位,若是仍有疑惑,请仔细阅读课本习题

    #include <iostream>
    #include<string>
    #include<vector>
    using namespace  std;
    void replace_string(string &s, const string &oldVal, const string &newVal)//最原始的字符串迭代器进行对比替换
    {
        auto l = oldVal.size();
        if (!l)
            return;
        auto iter = s.begin();
        while (iter <= s.end() - 1)
        {
            auto iter1 = iter;
            auto iter2 = oldVal.begin();
            while (iter2 !=oldVal.end()&& *iter1==*iter2)
            {
                iter1++;
                iter2++;    
            }
            if (iter2 == oldVal.end())
            {
                iter = s.erase(iter, iter1);
                if (newVal.size())
                {
                    iter2 == newVal.end();//出现错误直接崩溃
                    do 
                    {
                        iter2--;
                        iter = s.insert(iter, *iter2);
                    } while (iter2>newVal.begin());
                }
                iter += newVal.size();
            }
            else iter++;
        }
    }
    
    void replacestring(string&s, const string &oldVal, const string &newVal)//直接利用标准库里面的函数操作字符串极大地简化了string的操作
    {
        int p = 0;
        while ((p=s.find(oldVal,p))!=string::npos)
        {
            s.replace(p, oldVal.size(), newVal);
            p += newVal.size();
        }
    }
    
    
    void name_string(string&name, const string &prefix, const string &suffix)//利用迭代器来进行插入和尾增操作
    {
        name.insert(name.begin(), 1, ' ');
        name.insert(name.begin(), prefix.begin(), prefix.end());
        name.append(" ");
        name.append(suffix.begin(), suffix.end());
    }
    void namestring(string &name, const string&prefix, const string &suffix)//按长度和位置进行string管理
    {
        name.insert(0, " ");
        name.insert(0, prefix);
        name.insert(name.size(), " ");
        name.insert(name.size(), suffix);
    
    }
    void find_char(string&s, const string&chars)//直接利用find_first_of(s,chars)函数进行查找返回值为一个string::npos的特殊值,有返回字符串,没有返回npos
    {
        cout << "在" << s << "中查找" << chars << "中字符" << endl;
        string::size_type pos = 0;
        while ((pos = s.find_first_of(chars,pos))!=string::npos)
        {
            cout << "pos: " << pos<<",char: " << s[pos] << endl;
            pos++;
        }
    }
    
    void find_not_char(string&s, const string&chars)//直接利用find_first_of(s,chars)函数进行查找返回值为一个string::npos的特殊值,有返回字符串,没有返回npos
    {
        cout << "在" << s << "中查找不在" << chars << "中字符" << endl;
        string::size_type pos = 0;
        while ((pos = s.find_first_not_of(chars, pos)) != string::npos)
        {
            cout << "pos: " << pos << ",char: " << s[pos] << endl;
            pos++;
        }
    }
    float SumStringInt(const vector<string> &sub)//无限整数相加,不牵扯整数大小可以是stoi(string)到int,也可以是stof(string)是float
    {
        auto sum=0;
        for (auto iter = sub.begin(); iter != sub.end(); iter++)
            sum += stof(*iter);
        return sum;
    }
    
    
    int main(int argc, char **argv)
    {
    
        string s = "tho thru  tho !";
        //replacestring(s, "thru", "through");
        //replace_string(s, "thru", "through");
        name_string(s, "nihao", "wobuhao");
        cout << s << endl;
        string p = "123sdv546dbjy64vs4";
        find_char(p, "1234567890");
        find_not_char(p, "1234567890");
        vector<string>sumint{"12","3456","+102","-129","+12"};
        cout << SumStringInt(sumint)<<endl;
        system("pause");
        return 0;
    }

    测试效果如下
    这里写图片描述

  • 相关阅读:
    032 Gradle 下载的依赖jar包在哪?
    031 can't rename root module,Android Studio修改项目名称
    030 Cannot resolve symbol'R' 问题解决汇总大全
    029 Android Studio层级显示目录文件
    028 You are about to commit CRLF line separators to the Git repository.It is recommended to set the core. autocrlf Git attribute to true to avoid line separator issues If you choose Fix and Comit ,
    027 【Android基础知识】Android Studio 编译慢及 Adb connection Error:远程主机强迫关闭了一个现有的连接
    026 Android Studio 和Gradle版版本对应关系
    025 Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm
    024 Android Studio上传项目到Github 最全记录
    023 解决AndroidStudio下载gradle慢的问题
  • 原文地址:https://www.cnblogs.com/VCctor/p/5100689.html
Copyright © 2011-2022 走看看