在C++中,可以使用以下几种方法对string类型进行赋值:
- 使用赋值运算符"="进行赋值,例如:
std::string str1 = "Hello";std::string str2;str2 = str1; // 将str1的值赋给str2- 使用
assign()函数进行赋值,例如:std::string str1 = "Hello";std::string str2;str2.assign(str1); // 将str1的值赋给str2- 使用
append()函数进行赋值,例如:std::string str1 = "Hello";std::string str2 = "World";str2.append(str1); // 将str1的值追加到str2的末尾- 使用
insert()函数进行赋值,例如:std::string str1 = "Hello";std::string str2 = "World";str2.insert(0, str1); // 将str1的值插入到str2的指定位置- 使用
replace()函数进行赋值,例如:std::string str1 = "Hello";std::string str2 = "World";str2.replace(0, str2.length(), str1); // 将str1的值替换str2的值这些方法可以根据具体的需求选择适合的方式对string类型进行赋值。
上一篇:c语言怎么输出字符串中单个字符
下一篇:c++中参数传递的方式有哪些
C++









