我知道这是很常见的问题,但对我来说还是新的!
我不明白悬挂指针的概念,谷歌搜索,并编写测试方法找到一个.
我只是想知道这是一个悬垂的指针吗?无论我发现什么样的东西都回来了,我在这里尝试类似的东西!
谢谢!
void foo(const std::string name)
{
// will it be Dangling pointer?!, with comments/Answer
// it could be if in new_foo, I store name into Global.
// Why?! And what is safe then?
new_foo(name.c_str());
}
void new_foo(const char* name)
{
// print name or do something with name...
}
Run Code Online (Sandbox Code Playgroud) 我想从函数返回 Eigen::Vector 并想知道正确的方法是什么。就像是
Eigen::VectorXd& getMesh(int N) {
Eigen::VectorXd mesh(N + 1); // nb. of nodes
// Nodes are equally spaced
for (int i = 0; i < N + 1; i++) {
mesh[i] = i * (1.0 / N);
}
return mesh;
}
int main() {
// Mesh with 100 cells
Eigen::VectorXd mesh = getMesh(100);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,现在我们可能会遇到问题,因为用于在 getMesh() 中创建网格向量的内存可能不会动态分配,即当我们从函数返回时,out 引用“指向”已删除的内存。
我可以在 main 函数内分配内存,然后将其传递给 getMesh 函数,但这可以吗?我还有哪些其他的可能性?
我正在研究左值和右值,但我对此感到困惑:
#include<iostream>
int& get_val(){
return 10;
}
int main(){
get_val() = 5;
int a = get_val();
std::cout<<a<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道int a = get_val();(即它将返回的值分配给变量),但我想知道这是做什么的:get_val() = 5;。有什么int& get_val()作用?
我知道我们无法运行这段代码,但我想了解其背后的概念。
例如:当我编写这段代码时,我收到了正确的结果。
boost::asio::thread_pool t(3);
std::vector<int> vec = {10,20,30};
boost::asio::post(t, [&]{ foo(vec[0]);});
boost::asio::post(t, [&]{ foo(vec[1]);});
boost::asio::post(t, [&]{ foo(vec[2]);});
t.join();
Run Code Online (Sandbox Code Playgroud)
但是当我想在 for-cicle 中使用 boost::asio::post 时,我收到错误 Microsoft Visual C++ Runtime Library:“表达式:向量下标超出范围”
boost::asio::thread_pool t(3);
std::vector<int> vec = {10,20,30};
for (int i = 0; i < 3; ++i) {
boost::asio::post(t, [&]{ foo(vec[i]);});
}
t.join();
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让我的最后一种方法返回正确的答案?
我一直在检查我的代码并摆弄,但我似乎无法弄清楚为什么我没有得到预期的输出,而是得到随机符号。
预期输出是:JoeUPSReminderPick up your package! 54.23
我得到了 < 但之后的任何内容都是胡言乱语。任何帮助,将不胜感激。
#include <cstddef> // for std::size_t
#include <iostream>
#include <memory>
#include <ostream>
#include <string>
#include <utility> // for std::move, std::forward
#include <vector>
class xml_node_base
{
public:
virtual ~xml_node_base() = default;
void output(std::ostream& os) const
{
do_output_open(os);
do_output_body(os);
do_output_close(os);
}
protected:
virtual void do_output_open(std::ostream& os) const = 0; // abstract
virtual void do_output_body(std::ostream&) const { } // not abstract
virtual void do_output_close(std::ostream& os) const = 0; // abstract
};
using xml_node_base_t = std::shared_ptr<xml_node_base>; …Run Code Online (Sandbox Code Playgroud) 我想返回多个值并使用 声明该函数auto。
但这效果并不好。无法正确返回值。它被覆盖了。
我尝试执行以下功能f1〜f3。这些函数应该返回元组中的向量和字符串。但只是f3效果很好。
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
auto f1(){
std::vector<double> v(10, 0);
std::string s = "hello";
return std::forward_as_tuple(v, s);
}
auto f2(){
std::vector<double> v(10, 0);
return std::forward_as_tuple(v, "hello");
}
std::tuple<std::vector<double>, std::string> f3(){
std::vector<double> v(10, 0);
std::string s = "hello";
return std::forward_as_tuple(v, s);
}
int main(void){
//change the function
//auto [vec, str] = f1();
//auto [vec, str] = f2();
auto [vec, str] = f2();
for (auto e : …Run Code Online (Sandbox Code Playgroud)