当前位置:   article > 正文

string___assign

string mutable assign
#include <iostream>
#include <iterator>
#include <string>
 
int main()
{
  std::string s;
  // assign(size_type count, CharT ch)
  s.assign(4, '=');
  std::cout << s << '\n'; // "===="
 
  std::string const c("Exemplary");
  // assign(basic_string const& str)
  s.assign(c);
  std::cout << c << "==" << s <<'\n'; // "Exemplary == Exemplary"
 
  // assign(basic_string const& str, size_type pos, size_type count)
  s.assign(c, 0, c.length()-1);
  std::cout << s << '\n'; // "Exemplar";
 
  // assign(basic_string&& str)
  s.assign(std::string("C++ by ") + std::string("example"));
  std::cout << s << '\n'; // "C++ by example"
 
  // assign(charT const* s, size_type count)
  s.assign("C-style string", 7);
  std::cout << s << '\n'; // "C-style"
 
  // assign(charT const* s)
  s.assign("C-style\0string");
  std::cout << s << '\n'; // "C-style"
 
  char mutable_c_str[] = "C-style string";
  // assign(InputIt first, InputIt last)
  s.assign(std::begin(mutable_c_str), std::end(mutable_c_str)-1);
  std::cout << s << '\n'; // "C-style string"
 
  // assign(std::initializer_list<charT> ilist)
  s.assign({ 'C', '-', 's', 't', 'y', 'l', 'e' });
  std::cout << s << '\n'; // "C-style"
}
Output:
====
Exemplary==Exemplary
Exemplar
C++ by example
C-style
C-style
C-style string
C-style

 

转载于:https://www.cnblogs.com/lipenglin/p/4378834.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/566638
推荐阅读
相关标签
  

闽ICP备14008679号