乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      將一個(gè)char類型字符添加到string的尾部的方法---C++

       KookNut39 2022-05-17 發(fā)布于北京

      文章目錄

      1. += 簡(jiǎn)單粗暴的方法

      	std::string s("hello");
      	const char c = 'N';//將要加到s后面的字符
      	s += c;
      	std::cout << s << std::endl;
      
      	return 0;
      

      在string內(nèi)部對(duì)于+=這個(gè)操作符是重新寫了的,如下, push_back其實(shí)就是往尾部增加一個(gè)字符,那這樣看來(lái),直接調(diào)用push_back豈不是更高效?

      _CONSTEXPR20 basic_string& operator+=(_Elem _Ch) {
              push_back(_Ch);
              return *this;
          }
      

      2. string內(nèi)置方法push_back

      	std::string s("hello");
      	const char c = 'N';//將要加到s后面的字符
      	//s += c;
      	s.push_back(c);
      	std::cout << s << std::endl;
      

      我們來(lái)看看push_back內(nèi)部是如何實(shí)現(xiàn)的:

       _CONSTEXPR20 void push_back(const _Elem _Ch) { // insert element at end
              const size_type _Old_size = _Mypair._Myval2._Mysize;
              if (_Old_size < _Mypair._Myval2._Myres) {
                  _Mypair._Myval2._Mysize = _Old_size + 1;
                  _Elem* const _Ptr       = _Mypair._Myval2._Myptr();
                  _Traits::assign(_Ptr[_Old_size], _Ch);
                  _Traits::assign(_Ptr[_Old_size + 1], _Elem());
                  return;
              }
      
              _Reallocate_grow_by(
                  1,
                  [](_Elem* const _New_ptr, const _Elem* const _Old_ptr, const size_type _Old_size, const _Elem _Ch) {
                      _Traits::copy(_New_ptr, _Old_ptr, _Old_size);
                      _Traits::assign(_New_ptr[_Old_size], _Ch);
                      _Traits::assign(_New_ptr[_Old_size + 1], _Elem());
                  },
                  _Ch);
          }
      

      3. string內(nèi)置方法insert

      std::string s("hello");
      	const char c = 'N';//將要加到s后面的字符
      	//s += c;
      	//s.push_back(c);
      	s.insert(s.length(), 1, c);
      	std::cout << s << std::endl;
      

      以上這段代碼的意思,就是往string的尾部,插入一個(gè)字符c,insert三參數(shù)。

      4. string內(nèi)置方法append

      std::string s("hello");
      	const char c = 'N';//將要加到s后面的字符
      	//s += c;
      	//s.push_back(c);
      	//s.insert(s.length(), 1, c);
      	s.append(1, c);
      	std::cout << s << std::endl;
      

      那么看看append是如何實(shí)現(xiàn)的,和push_back有啥區(qū)別,其實(shí)仔細(xì)閱讀兩段代碼是非常相似的,只是把a(bǔ)ppend中的參數(shù)count替換成1,就是push_back的實(shí)現(xiàn)了

       _CONSTEXPR20 basic_string& append(_CRT_GUARDOVERFLOW const size_type _Count, const _Elem _Ch) {
              // append _Count * _Ch
              const size_type _Old_size = _Mypair._Myval2._Mysize;
              if (_Count <= _Mypair._Myval2._Myres - _Old_size) {
                  _Mypair._Myval2._Mysize = _Old_size + _Count;
                  _Elem* const _Old_ptr   = _Mypair._Myval2._Myptr();
                  _Traits::assign(_Old_ptr + _Old_size, _Count, _Ch);
                  _Traits::assign(_Old_ptr[_Old_size + _Count], _Elem());
                  return *this;
              }
      
              return _Reallocate_grow_by(
                  _Count,
                  [](_Elem* const _New_ptr, const _Elem* const _Old_ptr, const size_type _Old_size, const size_type _Count,
                      const _Elem _Ch) {
                      _Traits::copy(_New_ptr, _Old_ptr, _Old_size);
                      _Traits::assign(_New_ptr + _Old_size, _Count, _Ch);
                      _Traits::assign(_New_ptr[_Old_size + _Count], _Elem());
                  },
                  _Count, _Ch);
          }
      

      5. 使用流的方式來(lái)操作,方式稍微有點(diǎn)怪,但也不難理解

      #include <iostream>
      #include <sstream>
      int main() {
      	std::string s("hello");
      	const char c = 'N';//將要加到s后面的字符
      	//s += c;
      	//s.push_back(c);
      	//s.insert(s.length(), 1, c);
      	//s.append(1, c);
      	std::stringstream ts;
      	ts << s << c;//將字符串s和字符c輸入到臨時(shí)字符串流ts中
      	ts >> s;//ts將自己內(nèi)部保存的值,反吐給s
      	std::cout << s << std::endl;
      
      	return 0;
      }
      

      “美言可以市尊,美行可以加人。”—《道德經(jīng)》(第六十二章)

        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多