奇怪,非常奇怪的情况.考虑一下代码:
int n = 50;
auto p1 = new double[n][5]; //OK
auto p2 = new double[5][n]; //Error
Run Code Online (Sandbox Code Playgroud)
main.cpp:在函数'int main()'中:
main.cpp:17:26:错误:new-expression中的数组大小必须是常量
auto p2 = new double [5] [n]; //错误main.cpp:17:26:错误:'n'的值在常量表达式
main.cpp中不可用:15:8:注意:'int n'不是const
任何人都可以解释为什么我在第二个上得到编译错误但第一个工作完美?
我正在尝试使用C++中的数字配方实现的Romberg方法进行数值积分.
我的问题是我不知道如何处理多变量函数的1D积分中的常量变量,即f(x)dx
作品的积分,但我不知道计算积分的语法f(x,y=2,z=5,...)dx
.
我相信它与类和模板有关,我根本不熟悉它,我玩指针,额外的功能等没有成功.
#include <iostream>
#include <math.h>
#include <NR/nr3.h>
#include <NR/interp_1d.h>
#include <NR/quadrature.h>
#include <NR/romberg.h>
double fone (double x) { return 2*x*x*x; }
double ftwo (double x, double y){ return 2*x*x*x*y; }
int main()
{
double Ione = qromb(fone, 1, 3);
std::cout << Ione << std::endl;
//double Itwo = qromb(ftwo(x, 2.0), 1, 3);
//std::cout << Itwo << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这里我fone(x)
从x=1
3 集成.这段代码有效,但如果我取消注释上面的3行,即尝试ftwo(x, 2)
从x=1
3 集成,我得到
invalid initialization of non-const reference …
Run Code Online (Sandbox Code Playgroud) 我需要在其中组织带有符号分析的无限循环.在使用的CI中fgets(buf, N, stdin)
,假设buf
是buf[10]
.用户可以输入任意长度的字符串,我可以通过分解输入并检查长度为10的部分来分析它.如何在不使用C库的情况下在C++中实现它.如果你不明白我的意思,抱歉我的英语
任何人都可以帮我理解为什么这段代码片段无法编译?
#include <iostream>
#include <tuple>
#include <string_view>
constexpr auto Fields()
{
using namespace std::string_view_literals;
return std::tuple(
std::tuple("campo_1"sv, 123),
std::tuple("campo_2"sv, 456),
std::tuple("campo_3"sv, 890),
std::tuple("campo_4"sv, 136));
}
template<typename Tuple>
constexpr auto ProcessTupleElement(Tuple &&tuple)
{
//constexpr auto ccb = std::get<1>(tuple); // 1 uncomment to get an error
//char sx[ccb]; // 1 uncomment to get an error
//(void)sx;
return std::get<1>(tuple);
}
template<typename Tuple>
constexpr auto IterateTupleImpl(Tuple &&t)
{
return ProcessTupleElement(std::get<0>(std::forward<Tuple>(t)));
}
template<typename Tuple>
constexpr auto IterateTuple(Tuple &&t)
{
return IterateTupleImpl(std::forward<Tuple>(t));
}
int main()
{ …
Run Code Online (Sandbox Code Playgroud) I’m in need of some help and guidance on the design of my code. I want to run tests with multiple variables set to multiple values, without creating insane amounts of nested loops. I got a struct which holds various variables like this (only three integers as an example, but the real deal will hold a lot more, including booleans, doubles etc):
struct VarHolder
{
int a;
int b;
int c;
// etc..
// etc..
};
Run Code Online (Sandbox Code Playgroud)
The struct get passed …
最近,我在一个项目中看到他们将defint int键入为BOOL并用它代替了bool。这样做有什么好处吗?
typedef int BOOL;
Run Code Online (Sandbox Code Playgroud) 我编写了一个小程序,以检查异常情况下创建shared_ptr
via new
和make_shared()
函数之间的区别。我到处都读到,通过make_shared()
它是异常安全的。
但是,这两种情况的有趣之处在于,两种情况下的析构函数都不会在堆栈展开后调用?我错过了什么吗?提前致谢。
#include <iostream>
#include <memory>
class Car
{
public:
Car() { cout << "Car constructor!" << endl; throw std::runtime_error("Oops"); }
~Car() { cout << "Car destructor!" << endl; }
};
void doProcessing()
{
// std::shared_ptr<Car> sp(new Car());
std::shared_ptr<Car> sp2 = std::make_shared<Car>();
}
int main()
{
try
{
doProcessing();
}
catch(...)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在工作中看到了这个,它是我加入之前由人们写的。使用静态变量代替类的静态成员。就目前而言,我看不出为什么不应在此处使用类的静态成员的原因。如果我想说服这里的人们改变它,是否有什么好的借口说服他们?
我试图找到静态成员和静态变量之间的区别,似乎人们倾向于静态成员,除非有充分的理由,否则应该始终使用静态成员,但是没有提到非常现实的情况。
当前代码:
class Foo {
public:
static Foo *get() {
static Foo _instance;
return &_instance;
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
如何使用此功能:
int XXX_loadxxx(const char xxx, foo_handle *handle) {
// just get foo ptr and return
xxx::foo *ptr = xxx::foo::get();
int ret = ptr->init();
if (ret != 0) {
return -1;
}
*handle = ptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我心中的代码应如何定义该类:
class Foo {
static Foo _instance;
public:
static Foo *get() {
return &_instance;
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我将其更改为静态成员是否有任何不同,以及原因,我将非常感谢。
我有一个构建器类,我想将参数存储为引用,以便在后续构建中使用。
我想将可变数量的参数传递给我的类,使用类模板参数推导来推断模板参数,并将这些传递的参数作为引用存储在std :: tuple中。
从参数包转换为引用的std :: tuple的最简单方法是什么?
我发现std :: forward_as_tuple的功能与我想要的类似,但是我不想要转发引用,而且它在成员元组的初始化期间提供了语法错误。
template <typename... Ts>
struct Builder
{
using ArgsT = decltype(std::forward_as_tuple(Ts{}...));
ArgsT args_;
Builder(Ts&... ts) : args_(ts...) {}
};
int main()
{
struct Foo
{
int a;
double b;
};
Foo foo{};
Builder fooBuilder{foo.a, foo.b};
}
Run Code Online (Sandbox Code Playgroud)
语法错误是:
错误:没有匹配的调用函数
std::tuple<int&&, double&&>::tuple(int&, double&)
我是 C++ 新手,我刚刚编写了一个函数来告诉我字符串中的某些字符是否重复:
bool repeats(string s)
{
int len = s.size(), c = 0;
for(int i = 0; i < len; i++){
for(int k = 0; k < len; k++){
if(i != k && s[i] == s[k]){
c++;
}
}
}
return c;
}
Run Code Online (Sandbox Code Playgroud)
...但我忍不住认为它对于它应该做的事情来说有点拥挤。有什么办法可以用更少的行数编写这样的函数吗?