用函数更改std :: string的内容 [英] Changing contents of a std::string with a function

查看:107
本文介绍了用函数更改std :: string的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这个答案将是快速而简单的,但是我现在似乎无法弄清楚.

I feel like this answer will be quick and simple, but I cannot seem to figure it out right now.

 #include <string>
 #include <iostream>
 using namespace std;
 void change_thing (string x) {
      x="not thing";
 }
 int main() {
     string maybe_thing;
     maybe_thing="thing";
     change_thing(maybe_thing);
     cout << maybe_thing << endl;
     return 0;
 }

想要 maybe_thing在打印时是不是东西".我尝试了很多不同的指针策略,但是似乎没有任何效果(我很容易就做错了;由于我是C ++的新手,所以我对指针的知识还不完整).

I want maybe_thing to be "not thing" when it prints. I've tried a bunch of different pointer strategies, but nothing seems to work (which I could easily just be doing wrong; my knowledge of pointers is incomplete anyway because I'm new to c++).

提前谢谢!

推荐答案

您不需要指针,只需

这篇关于用函数更改std :: string的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆