1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[tips][cpp] stringのconstructor

Last updated at Posted at 2024-11-01

cppのtipsです。int main(){}やincludeヘッダなどは省略します。

std::basic_string::constructor

stringのconstructorからよく使うものをピックアップしてメモ。

vector<char> からstringに変換

    vector<char> c_vec = {'h', 'e', 'l', 'l', 'o'};
    std::string s99(c_vec.begin(), c_vec.end());
    std::cout << "s99 : " << s99 << std::endl; // "hello"
hello

文字をN回繰り返して構築 by refs

  // 文字をN回繰り返して構築
  std::string s7(3, 'a');
  std::cout << "s7 : " << s7 << std::endl;
aaa

文字列の範囲から構築 by refs

  // 文字列の範囲から構築
  std::string s4 = "hello";
  std::string s8(s4.begin(), s4.end());
  std::cout << "s8 : " << s8 << std::endl;
aaa

文字の初期化子リストから構築 by refs

  // 文字の初期化子リストから構築
  std::string s9 = {'h', 'e', 'l', 'l', 'o'};
  std::cout << "s9 : " << s9 << std::endl;

output

hello

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?