溫馨提示×

c++中string類型賦值的方法是什么

c++
小億
394
2024-03-22 18:59:45
欄目: 編程語言

在C++中,可以使用以下幾種方法對string類型進行賦值:

  1. 使用賦值運算符"="進行賦值,例如:
std::string str1 = "Hello";
std::string str2;
str2 = str1; // 將str1的值賦給str2
  1. 使用assign()函數進行賦值,例如:
std::string str1 = "Hello";
std::string str2;
str2.assign(str1); // 將str1的值賦給str2
  1. 使用append()函數進行賦值,例如:
std::string str1 = "Hello";
std::string str2 = "World";
str2.append(str1); // 將str1的值追加到str2的末尾
  1. 使用insert()函數進行賦值,例如:
std::string str1 = "Hello";
std::string str2 = "World";
str2.insert(0, str1); // 將str1的值插入到str2的指定位置
  1. 使用replace()函數進行賦值,例如:
std::string str1 = "Hello";
std::string str2 = "World";
str2.replace(0, str2.length(), str1); // 將str1的值替換str2的值

這些方法可以根據具體的需求選擇適合的方式對string類型進行賦值。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女