zoukankan      html  css  js  c++  java
  • std::string与MFC的CString的比较

    关于CString的详细用法,参见:www.cnblogs.com/htj10/p/9341545.html

    关于std::string的详细用法,参见:http://www.cplusplus.com/reference/string/string/?kw=string

    1. 查找字符(串)-- 完全匹配,返回首次出现位置
    CString:
    int Find( TCHAR ch ) const;
    int Find( LPCTSTR lpszSub ) const;
    int Find( TCHAR ch, int nStart ) const;
    int Find( LPCTSTR pstr, int nStart ) const;
    查找字串,nStart为开始查找的位置。未找到匹配时返回-1,否则返回字串的开始位置
    std::string:

    string (1)
    size_t find (const string& str, size_t pos = 0) const;
    
    c-string (2)
    size_t find (const char* s, size_t pos = 0) const;
    
    buffer (3)
    size_t find (const char* s, size_t pos, size_t n) const;
    
    character (4)
    size_t find (char c, size_t pos = 0) const;
    

    2. 查找字符串 -- 匹配字符串中某一个字符就行,返回首次出现位置
    CString:
    int FindOneOf( LPCTSTR lpszCharSet ) const;
    查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置

    std::string:

    string (1)
    size_t find_first_of (const string& str, size_t pos = 0) const;
    
    c-string (2)
    size_t find_first_of (const char* s, size_t pos = 0) const;
    
    buffer (3)
    size_t find_first_of (const char* s, size_t pos, size_t n) const;
    
    character (4)
    size_t find_first_of (char c, size_t pos = 0) const;
    

     When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.

    3. 获取字符串的一部分
    CString:
    CString Left( int nCount ) const;    从左边取nCount个字符,当nCount=-1或0时,返回“”,如果 nCount 超过了字符串长度,则提取整个字符串。
    CString Right( int nCount ) const;  从右边取nCount个字符
    CString Mid( int nFirst ) const;       从nFirst位置到结尾 [nFirst, -1)
    CString Mid( int nFirst, int nCount ) const;  从nFirst位置后nCount个字符 [nFirst, nFirst+nCount)

    std::string:
    string substr (size_t pos = 0, size_t len = npos) const;    从pos位置开始后len个字符,[pos,pos+len)  ,若没指定len则到字符串结尾 即 [pos,npos) 
    The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

     4. 字符串连接
    std::string 和 CString 都有 + += 连接,当字符串想要保存二进制数据(其中会有字节),连接字符串最好要用 std::string,因为有时CString相加时遇见 会截断。

    std::string s("iidss",6);//size=6
    std::string s2("qqoo",4);//size=4
    s = s + s2;//s.size()=10
    
    // s: iidssqqo

    2020-4-18

    std::string 的使用

    常用方法:

    {//std::string 的使用 基于C++11
            //构造函数
            char* szCh = "12345678";
            string s(szCh);            //"12345678"
            string s2(szCh, 7);        //"1234567"
            string s3(szCh, szCh + 6);//"123456"
            //赋值(会清空之前的内容,与构造几乎一样)
            s.assign(szCh, 7);
            s.assign(szCh, szCh + 6);
            s.assign(s2.begin(), s2.end());
            //判断是否为空字符串
            bool b = s.empty();
            //字符串长度
            size_t n = s.size(); //等于 s.length();
            size_t max = s.max_size();//4294967294(0xfffffffe)  string 能存储元素的最大数量
            size_t cap = s.capacity();//容量 15
            s.reserve(25);//分配新的容量
            cap = s.capacity();//31
            /////////// 字符串操作 //////////
            char ch1 = s.front();//获取第一个字符
            char ch2 = s.back();//获取最后一个字符
            const char* szBuf = s.c_str();//获取C风格的const字符串,s.c_str()等于s.data();
            ch2 = s[3];//获取任意索引位置的字符(无越界检查)
            ch2 = s.at(5);//获取任意索引位置的字符(有越界检查)
            //在尾添加一个字符
            s.push_back('a');
            //在尾添加一个字符串
            s.append(szCh);
            s.append(szCh, 8);
            s.append(szCh, szCh + 2);
            s.append(s2);
            s.append(s2.begin(), s2.end());
            // + +=
            s = s2 + s3;
            s += "abc";        
            //删除字符串
            s.pop_back();//弹出最后一个字符
            s.erase(s.end()-1);//删除某一个位置的字符(以 Iterator 为参数)
            s.erase(s.begin(), s.begin() + 8);//删除某范围的字符(以 Iterator 为参数)
            s.erase(0, 3);//从索引为0开始,删除3个字符
            s.erase(1);//从索引为1开始,删除到结尾(默认参数是0,即删除所有)
            //插入字符串
            s.insert(1, 2, 'A');//在索引为1的位置插入2个'A'
            s.insert(1, "abc");//在索引为1的位置插入"abc"
            s.insert(1, "abc", 2);//在索引为1的位置插入"abc"的头2个字符
            s.insert(1, "abc", 4);//在索引为1的位置插入"abc"的头4个字符,可以包括''
            s.insert(6, string("aaa"));
            s.insert(6, string("abc"), 1, 3);//相当于插入 str.substr(index_str, count)
            s.insert(s.begin(), 2, 'A');
            s.insert(s.begin(), 'B');
            s.insert(s.begin() + 1, s2.begin(), s2.end());
            //比较字符串
            s = "1234567";
            b = (s == "1234");
            b = (s > "123abc");
            b = (s < "12345");
            //获取字符串的一部分 str.substr(index_str, count) 若 index_str + count 越界了,不报错,只返回到结尾(npos)的部分
            string sub = s.substr(5);//从索引5开始到最后(npos)
            sub = s.substr(5, 3);//从索引5开始的3个字符
            //替换(根据位置)
            s.replace(0, 3, "xxxx");//从索引为0开始,替换3个字符为 “xxxx”
            s.replace(0, 4, s2.substr(0, 3));
            s.replace(0, 4, s2, 0, 5);//从索引为0开始,替换4个字符为 s2的从索引0开始的5个字符
            s.replace(s.begin(), s.begin() + 5, "xx");//使用迭代器的方式表示范围,来替换
            s.replace(s.begin(), s.begin() + 2, s.end()-3, s.end());
            /////////////////////   迭代器   ////////////////////
            // begin() end() 开始结尾迭代器 iterator
            // cbegin() cend() 是 const_iterator C++11出现的 
            // auto 也是C++11出现的,auto会自动为需要的类型,此时auto 就是 string::iterator;
            for (auto it = s.begin(); it != s.end(); ++it)
            {// 这种可以修改 *it
                *it = *it + 1;
            }
            for (string::const_iterator it = s.begin(); it != s.end(); ++it)
            {// 这种不可以修改 *it
                //
            }
            for (const string::iterator it = s.begin(); it != s.end(); /*++it*/)
            {// 这种不可以使用,因为 ++it 报错
                break;
            }
            for (auto it = s.cbegin(); it != s.cend(); ++it)
            {// 这种不可以修改 *it
                char ch = *it;
            }
            /////////////////////   查找   ////////////////////
            s = "abcdefghijkabcdefghijk";
            int pos = s.find("def");//从索引0开始查找,若找到,返回第一个匹配的索引位置;若没找到,返回    string::npos 即 -1 。
            pos = s.find("123");
            pos = s.find("def", 8);//从索引8开始查找
            s2 = "jk";
            pos = s.find(s2, 8);//从索引8开始查找
            pos = s.find(s2);//从索引0开始查找
            pos = s.find('b', 2);
            //反向查找
            //s.rfind(..) 与 s.find(..) 结构一样
    
            //find或rfind 都是 整个字符串匹配,才算找到。
            //而 find_first_of 是 匹配到字符串中的任意字符,就算找到。
            pos = s.find_first_of("123dk");//3
            pos = s.find_first_of("123dk", 6);//10  //从索引6开始查找
            // find_first_not_of是匹配到非 "123dk" 的任意字符
            pos = s.find_first_not_of("123dk");// 0
            pos = s.find_first_not_of("123dk", 2);// 2
            // 反向查找 用     s.find_last_of();  s.find_last_not_of();
    
            //////////  数 <==> string   /////////////
            // stoi        string -> int
            // stol        string -> long
            // stoll    string -> long long
            // stoul    string -> unsigned long
            // stoull    string -> unsigned long long
            // stof        string -> float
            // stod        string -> double
            // stold    string -> long double
            // to_string()  integer -> string
            s = "12w3";
            int ns = stoi(s);//默认是10进制的,
            size_t pos2;//未转换字符的索引,可以传入 NULL
            ns = stoi(s, &pos2, 16);//十六进制 
            ns = stoi(s, nullptr, 10);
            s = "0xff";
            ns = stoi(s, NULL, 16);//255
            s = "-20";
            ns = stoi(s);//-20
            s = "+20";
            ns = stoi(s);//20
            string sss = to_string(-32);//-32
            sss = to_string(0xff);//255
    
            // C++ 14
            //using namespace std::string_literals;
            //std::string s2 = "abcdef"; // forms the string "abc"
            //std::string s1 = "abcdef"s; // form the string "abcdef"
    
    
            //清空字符串
            s.clear();
    
        }
    View Code

    1. 替换字符串 str 中所有的 old --> new  (Find and Replace all occurrences of a sub string in C++)

    void ReplaceAll(std::string& data, const std::string& oldStr, const std::string& newStr)
    {
        // Get the first occurrence
        size_t pos = data.find(oldStr);
        // Repeat till end is reached
        while (pos != std::string::npos)
        {
            // Replace this occurrence of Sub String
            data.replace(pos, oldStr.size(), newStr);
            // Get the next occurrence from the current position
            pos = data.find(oldStr, pos + newStr.size());
        }
    }

    2. 字符串分割(详细参见:https://www.cnblogs.com/htj10/p/11354618.html)

    3. 字符串转换(wstring <->string  ANSI <-> UTF8)参见:my functions.h

    ---------------

    常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

    昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
  • 相关阅读:
    pku2704 Pascal's Travels
    MFC中获取各种类指针的方法 (转)
    VC中MessageBox的常见用法
    直接通过ADO操作Access数据库(转)
    (转)基于MapWinGis开发探索(三)改善缩放、渲染、显示文本
    VC画线几个常见方法
    虚拟现实技术发展的新趋势(转)
    (转)基于MapWinGis开发探索(二)——注册、加载、基本操作
    基于MFC的对话框程序加启动进度条
    IT应用新动向 未来需要云计算
  • 原文地址:https://www.cnblogs.com/htj10/p/11216277.html
Copyright © 2011-2022 走看看