我有一些关于从函数返回对局部变量的引用的问题:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
return a;
}
A getA3()
{
A a(5);
return a;
}
int main()
{
const A& newA1 = getA1(); //1
A& newA2 = getA2(); //2
A& newA3 = getA3(); //3
}
Run Code Online (Sandbox Code Playgroud)
我的问题是=>
执行getA1()
是否正确?我觉得它是错误的,因为它返回局部变量的地址或临时变量.
main
(1,2,3)中的哪些陈述会导致未定义的行为?
在const A& newA1 = getA1();
做标准的保证,暂时由const引用约束不会被破坏,直到引用超出范围是什么?
我有App A和App B.在App中我想向App B发送广播.这是App A的代码:
final Intent intent = new Intent();
intent.setAction("com.pkg.perform.Ruby");
intent.putExtra("KeyName", "code1id");
intent.setComponent(new ComponentName("com.pkg.AppB", "com.pkg.AppB.MainActivity"));
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
在App B - In中MainActivity
,我有MyBroadCastReceiver
Class.
public class MainActivity extends Activity {
private MyBroadcastReceiver MyReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Receive broadcast from External App
IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby");
MyReceiver = new MyBroadcastReceiver();
if(intentFilter != null)
{
registerReceiver(MyReceiver, intentFilter);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) { …
Run Code Online (Sandbox Code Playgroud) 我的问题是由stackoverflow上的这个答案激发的,/sf/answers/3365740731/.报价,
问:你如何转换
std::string_view
为const char*
?答:只需做一个
std::string(string_view_object).c_str()
获得保证的以空值终止的临时副本(并在行尾清理它).
不幸的是,它构建了一个新的string
.我想知道是否可以做到,
static_cast<string>(string_view_object).c_str()
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是:
这会构造一个新字符串吗?
是否保证返回以null结尾的char序列?
我有一小段代码用于演示.它似乎工作正常.(参见wandbox 结果)
#include <string>
#include <iostream>
#include <string_view>
#include <cstring>
int main()
{
std::string str{"0123456789"};
std::string_view sv(str.c_str(), 5);
std::cout << sv << std::endl;
std::cout << static_cast<std::string>(sv) << std::endl;
std::cout << strlen(static_cast<std::string>(sv).c_str()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我正在学习呼吸优先搜索算法。该图是一个完全二叉树。我想打印每个节点到根的距离。例如,d[0, 0] = 0, d[0, 1] = 1, d[0, 2] = 1, d[0, 3] = 2, d[0, 7] = 3
。
点文件在这里。
digraph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
9;
10;
11;
12;
13;
14;
0->1 ;
0->2 ;
1->3 ;
1->4 ;
2->5 ;
2->6 ;
3->7 ;
3->8 ;
4->9 ;
4->10 ;
5->11 ;
5->12 ;
6->13 ;
6->14 ;
}
Run Code Online (Sandbox Code Playgroud)
程序非常简单,make_bfs_visitor
用adistance_recorder
记录树边之间的距离。
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include …
Run Code Online (Sandbox Code Playgroud) 这个问题的灵感来自于 boost asio 文档(链接)中的异步计时器教程。代码稍作修改,使效果更加明显。
有一个相关的问题,Multiple async_wait from a boost Asio Deadline_timer。但我不确定该问题的答案是否适用于我的情况。
代码非常简单,如果注释掉重复的行,则可以按预期工作,如下所示。
A具有通话steady_timer
时长。1s
async_wait
当它过期时,处理程序被调用。在处理程序内部,计时器的生命周期又延长一秒,并且计时器async_wait
再次调用。
变量count
20 用于限制计时器可以触发的次数。
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace asio = boost::asio;
void bind_handler(const boost::system::error_code& ec,
asio::steady_timer& t,
int count) {
if (count > 0) {
std::cout << "getting " << count << "\n";
t.expires_at(t.expiry() + std::chrono::seconds(1));
t.async_wait(boost::bind(bind_handler, asio::placeholders::error,
boost::ref(t), --count));
}
}
int main() {
asio::io_context io_context(1);
asio::steady_timer t(io_context, std::chrono::seconds(1));
int count = 20; …
Run Code Online (Sandbox Code Playgroud) c++ ×3
boost ×2
android ×1
boost-asio ×1
boost-graph ×1
broadcast ×1
reference ×1
static-cast ×1
string-view ×1
undefined ×1