[THINKING IN C++]CHAPTER 03:EXERCISE 07

本文介绍了一个C++程序示例,该程序定义了两个函数来修改字符串对象:一个通过指针,另一个通过引用。演示了如何使用这些函数来改变字符串的内容,并展示了在main函数中创建、初始化字符串对象的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 7. Create two functions, one that takes a string* and one
that takes a string&. Each of these functions should
modify the outside string object in its own unique way.
In main( ), create and initialize a string object, print it,
then pass it to each of the two functions, printing the
results.
    定义两个函数,一个获取string*,一个获取string&。每一个函数
    以其特有的方式修改外部string对象。
    在main()函数里,创建并定义一个string对象,打印出来,然后将
    它传递给这两个函数,将结果打印出来。
*/
#include
<iostream>
#include
<string>
using namespace std;

void func1(string* a){
    
*a=*a+*a;
}

void func2(string& a){
    a
=a+a;
}

void main(){
    
string t="abc";
    func1(
&t);
    cout
<<t<<endl;
    func2(t);
    cout
<<t<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值